Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to present something as follows which is mainly under math-mode:

Label1...
    name1 ::= choice11...   comment11.
            | choice12..    comment12..
            | chocie13.     comment13...

Label2..
  name2.. ::= choice21...   comment21.
            | choice22..    comment22..
            | choice23.     comment23...

So I want all the labels to be left justified, and all the comments to be left justified. ... ::= ... and | ... are positioned such that = and | are aligned.

Could anyone tell me how to realize this? Thank you very much!

share|improve this question
    
Do you want = and | to be aligned across both blocks - name1 and name2 and... Also, by 'left adjusted', do you mean \raggedright (flush left) with the text margin? –  Werner Aug 9 '11 at 4:05
    
Sure, across both blocks... by "left adjusted", I mean "left justified"... I have modified the post... –  SoftTimur Aug 9 '11 at 4:19
add comment

5 Answers 5

up vote 2 down vote accepted
\documentclass{article}
\begin{document}

\(
\begin{array}{llcll}
  \multicolumn{3}{l}{label1}\\
  &name1&  ::=& 1+1& \textsf{one plus one}\\
  && | & 2\times 2& \textsf{two times two}\\
  && | & 3^3 & \textsf{three raised to the power of three}\\
  \multicolumn{3}{l}{label2}\\
  &name2&  ::=& choice2 & \textsf{foo}\\
  && | & choice2 & \textsf{bar}\\
  && | & choice3 & \textsf{qux}\\
\end{array}
\)

\end{document}

enter image description here

share|improve this answer
add comment

The amsmath package provides the command \intertext{...} which typesets its argument flush left while still preserving subsequent mathematical alignment. From the package documentation:

The command \intertext is used for a short interjection of one or two lines of text in the middle of a multiple-line display structure... Its salient feature is preservation of the alignment, which would not happen if you simply ended the display and then started it up again afterwards. \intertext may only appear right after a \\ or \\* command.

\intertext{...} does not work in an array environment. I'm using the align* environment here (to suppress equation numbering):

\documentclass[10pt]{article}
\usepackage[showframe]{geometry}% Show page layout frames
\usepackage{calc}% For width calculations
\usepackage{amsmath}
\begin{document}

\newcommand*{\also}[1][|]{\makebox[\widthof{$=$}][c]{$#1$}}%

\begin{align*}
  \intertext{Label 1.}% Text flush left
  X_{\text{long subscript}} ::=&\ 1 &&\text{This is}  \\
    |&\ 2+3 && \text{a comment on the numbers that} \\
    |&\ 4+5\times 6 &&\text{stretch on forever.} \\
\intertext{Label 2..}% Text flush left
  \text{name2..} ::=&\ choice21... && \text{Here are some more comments}  \\
    \also&\ choice22.. && \text{that describe the numbers} \\
    \also&\ \textstyle 1+3+5!+\binom{n}{n-1} && \text{in detail.}
\end{align*}
\end{document}

Align* environment from AMSmath with \intertext

Since you are interested in a horizontal alignment across the = and |, I've made a new command \also[<operator>] that you can use to center an optional <operator> (default is |). The first block did not use \also, while the second block does, aligning the two operators. The use of the geometry package is merely to show the horizontal alignment within the text block.

share|improve this answer
    
In my experience, \intertext works fine in an array environment, and amsmath is just being overcautious in disallowing it. --- Executing the \intertext@ macro will redefine \intertext to just 'do its thing'. I myself patched the array and tabular environments to call \intertext@ locally. But you can also execute it globally. You'll be fine as long as you don't do anything particularly weird with \intertext. –  mhelvens Aug 18 '13 at 17:17
add comment
\documentclass{article}
\usepackage{array,mathtools}

\newcolumntype{L}{>{$}l<{$}}
\newcolumntype{R}{>{$}r<{$}}
\def\V{\makebox[\widthof{=}]{$|$}}
\def\mc#1{\multicolumn{2}{@{}l@{}}{#1}}

\begin{document}

\begin{tabular}{@{} p{5mm} R @{\,} L L}
\mc{Label1...}                             \\
  & name1 ::= & choice11...  &comment11.   \\
  &         \V & choice12..   &comment12.. \\
  &         \V & chocie13.    &comment13...\\[10pt]
 %
\mc{Label2...}                             \\
  & name1 ::= & choice21...  &comment21.   \\
  &         \V & choice22..   &comment22.. \\
  &         \V & chocie23.    &comment23...\\
\end{tabular}

\end{document}

enter image description here

share|improve this answer
add comment

Here is a plain-version for good measure:

\def\mylabel#1{\noalign{\hbox{#1}}}
\def\mymid{\setbox0\hbox{$=$}\mathrel{\hbox to\wd0{\hfil$|$\hfil}}}
\catcode`@=11
\def\myalign#1{\displ@y\ialign{\strut\qquad
  \@lign\hfil$\m@th\displaystyle##$&
  \@lign$\m@th\displaystyle##$\hfil&
  \@lign\qquad\ignorespaces##\ignorespaces\hfil\crcr
  #1}}
\catcode`@=12
$$\myalign{
  \mylabel{Label1...}
    name1   ::&= choice11...       & comment11.  \cr
              &\mymid choice 12..  & comment12.. \cr
              &\mymid choice 13.   & comment13...\cr
  \mylabel{Label2..}
    name2.. ::&= choice21...       & comment21.  \cr
              &\mymid choice22..   & comment22.. \cr
              &\mymid choice23.    & comment23...\cr
}$$
\bye

myalign

share|improve this answer
add comment

Is this sort of what you are looking for? The second solution uses the newcolumnstypes from the array package to make the tabular definition easier to read.

\documentclass{article}
\usepackage[fleqn]{amsmath}
\usepackage{booktabs}
\usepackage{array}

\newcolumntype{L}{>{\begin{math}}l<{\end{math}}}%
\newcolumntype{R}{>{\begin{math}}r<{\end{math}}}%

\begin{document}
\[
\begin{array}{llr@{}llll}
\multicolumn{2}{l}{\text{Label1...}}\\
  &name1   &::=  &\,choice11...  &comment11.\\
  &          &|  &\,choice12..   &comment12..\\
  &          &|  &\,chocie13.    &comment13...\\
%
\multicolumn{2}{l}{\text{Label2...}}\\
  &name2.. &::= &\,choice21...   &comment21.\\
  &          &| &\,choice22..    &comment22..\\
  &          &| &\,choice23.     &comment23...
\end{array}
\]

If it is desired that the label be flushleft, then can use the following

\begin{tabular}{lLR@{}LLLL}
\multicolumn{2}{l}{\text{Label1...}}\\
  &name1   &::=  &\,choice11...  &comment11.\\
  &          &|  &\,choice12..   &comment12..\\
  &          &|  &\,chocie13.    &comment13...\\
 %
\multicolumn{2}{l}{\text{Label2...}}\\
  &name2.. &::= &\,choice21...   &comment21.\\
  &          &| &\,choice22..    &comment22..\\
  &          &| &\,choice23.     &comment23...
\end{tabular}
\end{document}
share|improve this answer
    
I think the OP wants LabelX... to be 'left adjusted', as in flushleft (or \raggedright). –  Werner Aug 9 '11 at 4:03
    
Huh? I made the first column l, won't that do it? –  Peter Grill Aug 9 '11 at 4:12
1  
The l will typeset the column on the left _within the array, not flushleft with the text block. –  Werner Aug 9 '11 at 4:29
    
Ok, have updated to use tabular and the array package to allow for custom column definitions. –  Peter Grill Aug 9 '11 at 4:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.