Text FormattingLabels in array environments

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
yoriz
Posts: 8
Joined: Sun Nov 21, 2010 8:39 pm

Labels in array environments

Post by yoriz »

Hi,

I've been thinking about this problem for a while and haven't been able to find a satisfying solution: suppose I have an array environment inside an equation and the equation does not contain anything else. Is it possible to add labels to the lines of the array? The behavior should be similar to the labels of, say, equations: they are right-aligned with the right margin, while not touching the alignment of the main contents.

Currently, I implement this behavior by first generating an array environment with the main contents, then moving up by the exact size of the equation that has been generated (by using \vspace and measuring the box etc), and then generating an array with the right-aligned labels.

Thanks,

Yori
Last edited by yoriz on Wed Nov 24, 2010 4:58 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
michaels
Posts: 2
Joined: Mon Nov 22, 2010 11:37 pm

Re: Labels in array environments

Post by michaels »

Hi Yori,

so I suppose the align environment or similar from the amsmath package won't do it? At the moment I am not sure what a formula could look like which would not be solvable via align and so on.

Could you maybe post your column specification you use for the array environment?

I guess the subeqn package won't help you since there is no aligning across the equations...

Or does this maybe help you: http://www.latex-community.org/forum/vi ... f=4&t=1950 ?

Cheers,

Michael
yoriz
Posts: 8
Joined: Sun Nov 21, 2010 8:39 pm

Re: Labels in array environments

Post by yoriz »

Hi Michael,

thanks for your reply. No, for as far as I know, align or a similar amsmath, or subeqn, environment does not work. The issue is that I want to use quite a few columns, some of which are right-aligned, others are to be centered.

I am attaching an example pdf output which I generated in a very tedious way by superimposing two arrays. The problem is that it is not very robust. The linear program I wrote here (where the variables are vertically aligned) is an example of an equation that does not fit well in an align environment (for as far as I know).

As you can see, I want some right-aligned entries as well, which one can think of as labels, but they are not necessarily labels (sometimes, I may want to write 'for all i=1,2,...,n' or something like that). Also notice that the linear program itself is nicely centered.

Thanks again,

Yori
Attachments
lptest.pdf
(41.02 KiB) Downloaded 348 times
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Labels in array environments

Post by frabjous »

Could you just use a tabular or array environment?

A quick suggestion:

Code: Select all

\documentclass{article}
\usepackage{array}
\newcounter{constraint}
\setcounter{constraint}{0}
\newcommand{\constraintlabel}[1]{\refstepcounter{constraint}\label{#1}(constraint \theconstraint)}
\begin{document}
\noindent This is an example document that contains a linear program. The objective of
the linear program is:
\[ \max 3x_{1} + 2x_{2} \]
The full linear program read as follows:

\smallskip
\hfill\begin{tabular}{l@{\ }%
>{\begin{math}}r<{\end{math}}@{\ }%
>{\begin{math}}c<{\end{math}}@{\ }%
>{\begin{math}}r<{\end{math}}@{\ }%
>{\begin{math}}c<{\end{math}}@{\ }%
>{\begin{math}}r<{\end{math}}@{\hspace{0.9in}}%
r@{}}
max  & 3x_1 &+& 2x_2&      &    & \\
s.t. & x_1  &+& x_2 & \leq & 9  & \constraintlabel{first}\\
     & 3x_1 &+& x_2 & \leq & 18 & \constraintlabel{second}\\   
     & x_1  & &     & \leq & 7  & \constraintlabel{third}\\
     &      & & x_2 & \leq & 6  & \constraintlabel{fourth}\\
     & \multicolumn{4}{c}{$x_1, x_2 \geq 0$} & & \\
\end{tabular}

\smallskip\noindent Clearly, constraint \ref{first} is fun than constraint \ref{third}.
\end{document}
Yields:
constraint.png
constraint.png (12.27 KiB) Viewed 11502 times
yoriz
Posts: 8
Joined: Sun Nov 21, 2010 8:39 pm

Re: Labels in array environments

Post by yoriz »

Hi frabjous,

thanks for your reply. Yes, I thought of that as well. The labels are obviously right-aligned now, which is good, but how do you guarantee that the content is centered? As I understand it, currently, one has to fiddle around with the value of what is now 0.9in.

Thanks again,

Yori

P.S. I didn't know this \begin{math} ... \end{math} trick in the tabular definitions, thanks for that too!
yoriz
Posts: 8
Joined: Sun Nov 21, 2010 8:39 pm

Labels in array environments

Post by yoriz »

Hi,

I managed to do it using TikZ, which I think is quite some overkill, but for reference, here is the code:

Code: Select all

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{calc}

\begin{document}

\newlength{\myboxlena}
\newlength{\myboxlenb}
\newlength{\myboxlenc}
\settowidth{\myboxlena}{objective}
\settowidth{\myboxlenb}{constraint 1}
\settowidth{\myboxlenc}{constraint 2}
\newcommand\cell[1]{\makebox[16pt][r]{$#1$}}

\noindent
\begin{tikzpicture}
 \matrix (m) [anchor=base, xshift=.5\textwidth, matrix of math nodes, nodes = { anchor=base }, text centered] {
 \max & \cell{5 x_1}  & + & \cell{x_2} & + & \cell{x_3} &      & \\
 \    & \cell{-2 x_1} &   &     &   &     & \leq & 1 \\
 \    &     &   & \cell{x_2} & + & \cell{x_3} & \leq & 1 \\
      };
    \draw let \p1 = (m-1-1.base) in (\textwidth, \y1) node[text ragged, anchor=base, xshift=-0.5\myboxlena] {objective};
    \draw let \p1 = (m-2-1.base) in (\textwidth, \y1) node[text ragged, anchor=base, xshift=-0.5\myboxlenb] {constraint 1};
    \draw let \p1 = (m-3-1.base) in (\textwidth, \y1) node[text ragged, anchor=base, xshift=-0.5\myboxlenc] {constraint 2};
    \draw[dotted] let \p1 = (m-1-1.base) in (0, \y1)--(\textwidth, \y1);
    \draw[dotted] let \p1 = (m-2-1.base) in (0, \y1)--(\textwidth, \y1);
    \draw[dotted] let \p1 = (m-3-1.base) in (0, \y1)--(\textwidth, \y1);
 \path[use as bounding box] (0,0) rectangle (\textwidth, 1);
\end{tikzpicture}

\end{document}
(there are dotted lines in the picture to check that the baselines line up)

There are a few issues with this though:
  • It is quite ugly because it requires a length variable for every row (which would be not so much of an issue for me, because I am generating all of this using the python.sty package). The reason for this is that the fact that the \settowidth command does not work inside the tikzpicture environment.
  • Since I want my columns to be right-aligned, I need to specify the width of each column, which is alright-ish. This could be fixed if either TikZ had right-alignment support, or there would exist an anchor=east base or something like that.
  • The \cell{} construction is a bit impractical.
If you guys have any suggestion to improve this (especially the first point), I'd be happy to hear them. Otherwise, I'll set the topic to resolved later today.

Yori
Post Reply