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'd often like to set formulas like

enter image description here

Is there some other way than doing this by manually inserting whitespace (with \quad etc.), i.e., something like

\begin{align*}
f( & 1 + g( & 2 + \\
   &        & 3), \\
   & 4 + 5 + 6 + 7)
\end{align*}

So in the first line which contains an nth & symbol, its horizontal position should be remembered. When a subsequent line contains an nth & symbol, the text should continue at the remembered position.

Afaik align etc. can't do this because 2 + and 3 would be moved to the right because 4 + 5 + 6 + 7) occupies too much space.

share|improve this question
2  
I'm curious, can you say why you'd like to do that? –  Hendrik Vogt Jun 18 '12 at 17:09
1  
I occasionally have long (~3 lines) logical formulas with large terms. One example contains an if-then-else with conjunctions with a \max with a \sum with a large term. Thank you so much for all the answers! –  chs Jun 19 '12 at 7:59
    
@HendrikVogt I know it's been a year since you've asked this question but let me still add this: the example might be too minimalistic to see the benefit but it's actually just the indentation scheme most used for programming languages so it makes sense to me to structure equations the same way. Too bad LaTeX has such a poor support for this as of know :/ –  Christian Jun 16 '13 at 17:55
add comment

3 Answers

up vote 6 down vote accepted

Yes, it's possible. Use an array for stacking, and drop the intercolumn space, as needed using @{}:

enter image description here

\documentclass{article}
\begin{document}
\[
  \begin{array}{r@{}l@{}l}
    f( & 1 + g( & 2 + \\
       &        & 3), \\
       & \multicolumn{2}{@{}l}{4 + 5 + 6 + 7)}
  \end{array}
\]
\end{document}

If you wish for a more breathable presentation, use \\[\jot] instead of just \\.

share|improve this answer
    
You should mention that this switches all content to inline mode so anything a bit more complex than the MWE suddenly becomes tiny. –  Christian Jun 16 '13 at 18:20
    
@Christian: Yes, elements within the array environment default to \textstyle (which is similar to what you get when using inline math mode $..$). You can switch back by inserting \displaystyle wherever needed. –  Werner Jun 17 '13 at 0:53
add comment

You can also do this with either:

  1. the alignat environment along with \matrlap{}, or
  2. with the align and use a \hphantom{} to obtain:

enter image description here


1. Using \alignat and \mathrlap:

The alignat provides multiple rl align pairs. Since your second alignment is intended to be l aligned, you use a double &&. The first & skips over the r aligned column of the rl align pair.

\mathrlap and related macros are discussed in detail in this TuGboat paper.

Code:

\documentclass{article}
\usepackage{mathtools}

\begin{document}
\begin{alignat*}{3}
    f( & 1 + g( && 2 + \\
       &        && 3), \\
       & \mathrlap{4 + 5 + 6 + 7)}
\end{alignat*}
\end{document}

2. Using \align and \hphantom:

You could also just use a \hphantom{} to insert the exact horizontal spacing as is required by its parameter.

Code:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\begin{align*}
    f( & 1 + g( 2 + \\
       & \hphantom{1 + g(} 3), \\
       & 4 + 5 + 6 + 7)
\end{align*}
\end{document}
share|improve this answer
add comment
\documentclass{article}
\usepackage{mathtools}

\begin{document}

\[
  \begin{array}{ r @{} l @{\mkern\thinmuskip} r }
    f( & 1 + g( & 2 + \\
       &        & 3), \\
       & \mathrlap{4 + 5 + 6 + 7)}
  \end{array}
\]
\end{document}
share|improve this answer
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.