# Math indentation environment

I'd often like to set formulas like

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.

• I'm curious, can you say why you'd like to do that? – Hendrik Vogt Jun 18 '12 at 17:09
• 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

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

\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 \\. • 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 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: ## 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}  \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}


This is an excellent question. I face the same problem when typesetting page-long formulas. In the absence of a math display environment with tabbing capabilities, I typeset stuff such as this:

\documentclass{article}\pagestyle{empty}
\usepackage{mathtools}\mathtoolsset{mathic=true}
\begin{document}
\begin{tabbing}
$$f($$\=$$1 + g($$\=\kill
$$f($$\>$$1 + g($$\>$$2 \,+$$\$\jot] \>\>$$3),$$\\[\jot] \>$$4 + 5 + 6 + 7)$$ \end{tabbing} %%% Won't work: % \[ % \begin{tabbing} % f( \= 1 + g( \=\kill % f( \> 1 + g( \> 2 \,+\\[\jot] % \>\> 3),\\[\jot] % \>4 + 5 + 6 + 7) % \end{tabbing} %$
% \begin{mathtabbing}
% f( \= 1 + g( \=\kill
% f( \> 1 + g( \> 2 \,+\\
% \>\> 3),\\
% \>4 + 5 + 6 + 7)
% \end{mathtabbing}
%%% How much effort is it to produce such a mathtabbing environment?
\end{document}


Result after running latex and converting DVI into a PNG:

• Welcome to TeX.SE! Can you please explain your used code? – Mensch Dec 19 '19 at 15:11
• @Mensch Yes, but not everything. Let us concentrate on the stuff you don't understand. What is the first command in my code that you don't understand? – user203414 Dec 19 '19 at 15:31
• You missunderstood me: it is usual here to explain the used code instead of only showing some code. Suppose, there are users not knowing your used commands, they have at the moment no change to understand what is going on ... – Mensch Dec 19 '19 at 22:02
• @Mensch No, you are wrong. Folks do have a freedom to leave their code unexplained. As an example, consider the answer tex.stackexchange.com/a/521068 without any explanations whatsoever. Complain there first. – user203414 Dec 20 '19 at 11:14