7

In many cases I want to write some aligned equation that goes through multiple lines and add a comment for some of the lines (I think if the comments are short enough then it's clearer than explaining everything at the end of the equation). Ideally, all the comments should start on the same vertical line. This is achieved by what's shown in the following example:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
    This is just some text to show where the margin is. Some more and more text yay text is good.
    \begin{align*}
    a + b + c + d + e &= an-expression \\
    &= some-very-longgggggggggg-expression && \text{comment} \\
    &= another-expression && \text{a little longer comment} \\
    &= 0
    \end{align*}
\end{document}

The problem is when some of the equation's lines are too long:

enter image description here

The second comment is inside the right margin, which should not be allowed.

I know it happens because the align environment creates a table and each & sign adds a column, so the second comment cannot be on a different column than the first comment (which is pushed by the equation on its row).

But still, what I would like to happen is that the second comment would end by the right margin. Is there a simple way to achieve that? If possible, I would prefer to not define too complicated commands, even if it means I'll not get exactly what I asked for, but something else that kinda solves the problem.

4
  • 1
    Welcome to TeX.SX! Could you provide the definition of the macros you are using, or, better, a MWE?
    – GuM
    Dec 14, 2018 at 20:24
  • where is define \condexp[1]{...}?
    – Zarko
    Dec 14, 2018 at 20:25
  • @GuM, Zarko, Thanks for the feedback. I edited the question, the example is now fully working. Dec 14, 2018 at 20:36
  • @Zarko I made a different example which is a MWE like GuM suggested. Dec 14, 2018 at 20:59

4 Answers 4

6

You cannot have all the comments vertically aligned at their left margin, because the wide equation and the wide comment simply don't fit the line.

You can save space by setting the first term on a line by itself and still have the look of a display, avoiding to go to the left margin:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

This is just some text to show where the margin is.
Some more and more text yay text is good.
This is just some text to show where the margin is.
Some more and more text yay text is good.
\begin{align*}
\lefteqn{a + b + c + d + e}\quad \\
&= an-expression \\
&= some-very-longgggggggggg-expression && \text{comment} \\
&= another-expression && \text{a little longer comment} \\
&= 0
\end{align*}

\end{document}

With \lefteqn you hide the width of the term; I added \quad in order not to have the=` signs at the left margin and it turns out that still a tad of space is available.

enter image description here

Another possibility you can explore, but this depends on the actual equations and comments, is breaking the long comment across lines.

\documentclass{article}
\usepackage{amsmath}

\begin{document}

This is just some text to show where the margin is. 
Some more and more text yay text is good.
This is just some text to show where the margin is. 
Some more and more text yay text is good.
\begin{align*}
a + b + c + d + e
&= an-expression \\
&= some-very-longgggggggggg-expression && \text{comment} \\
&= another-expression && \begin{tabular}[t]{@{}l@{}}
                         a little \\
                         longer \\
                         comment
                         \end{tabular} \\
&= 0
\end{align*}

\end{document}

enter image description here

1
  • Thank you, for my case this is the best solution. Except I've just read about \MoveEqLeft and I think it might be a tiny bit better. What do you think? I posted a separate solution that explains it, hope it's OK. tex.stackexchange.com/a/465987/175237 Dec 15, 2018 at 15:43
1

Well, the following is a very simple trick that “kinda solves the problem”:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{amsmath}



\begin{document}

% \slshape % if uncommented, will the reference to the Magic Theorem be slanted?

Some text before the equation, to show where the right margin lies.  Oh, no, 
that was not enough: \emph{now} it is enough!
\begin{flalign*}
    \textit{formula\_result} &= \textit{short\_term} \\
        &= \textit{muuuuch\_loooonger\_term} + \textit{another\_one}
                && \text{by \ldots} && \\
        &= x-x
                && && \makebox[0pt][r]{by the Magic Theorem} \\
        &= 0
\end{flalign*}
Some text after the equation, to show again the position of the right margin of
the text block.

\end{document}

This is the output I get (on a TeXLive 2016, sorry, but that’s what I’ve got at the moment):

Output of the code

Addition: An even simpler solution using \hidewidth, which, however, does not yield exactly the same printout as the previous one:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{amsmath}



\begin{document}

Some text before the equation, to show where the right margin lies.  Oh, no, 
that was not enough: \emph{now} it is enough!
\begin{flalign*}
    \textit{formula\_result} &= \textit{short\_term} \\
        &= \textit{muuuuch\_loooonger\_term} + \textit{another\_one}
                & \text{by \ldots} & \\
        &= x-x
                & \hidewidth\text{by the Magic Theorem} & \\
        &= 0
\end{flalign*}
Some text after the equation, to show again the position of the right margin of
the text block.

\end{document}

Indeed, IMHO the output is actually better, with both explanations aligned along the right margin:

Output of the second code sample

Second addition: If you object to using plain TeX commands like \hidewidth, you can replace the second example with the following code

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{amsmath}



\begin{document}

% \slshape

Some text before the equation, to show where the right margin lies.  Oh, no, 
that was not enough: \emph{now} it is enough!
\begin{flalign*}
    \textit{formula\_result} &= \textit{short\_term} \\
        &= \textit{muuuuch\_loooonger\_term} + \textit{another\_one}
                & \text{by \ldots} & \\
        &= x-x
                & \makebox[0pt][r]{by the Magic Theorem} & \\
        &= 0
\end{flalign*}
Some text after the equation, to show again the position of the right margin of
the text block.

\end{document}

with no change in the output.

7
  • You can replace \hidewidth{\text{...}} with the simpler \llap{...}.
    – Bernard
    Dec 14, 2018 at 21:11
  • @Bernard: Yes, but some very influential users, here, object against advertising the use of \llap to newbies… :-) Moreover, perhaps a still better solution could be to move the \hidewidth to the line of the “muuuch loooonger term”: I guess it depends on the context…
    – GuM
    Dec 14, 2018 at 21:18
  • Of course it has to be used very cautiously.
    – Bernard
    Dec 14, 2018 at 21:21
  • @Bernard: Well, actually \llap was the first thing I thought of, but I deliberately rejected it.
    – GuM
    Dec 14, 2018 at 21:24
  • Well, I agree that \llap is not for newbies. But \hidewidth is for wizards. ;-)
    – egreg
    Dec 14, 2018 at 21:41
1

Googling \lefteqn from egreg's answer I found this answer which is about \MoveEqLeft - a command from mathtools (see 3.4.4 here). So the example becomes:

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}

\begin{document}
    This is just some text to show where the margin is. Some more and more text yay text is good.
    \begin{align*}
    \MoveEqLeft[1] a + b + c + d + e \\
    &= an-expression \\
    &= some-very-longgggggggggg-expression && \text{comment} \\
    &= another-expression && \text{a little longer comment} \\
    &= 0
    \end{align*}
\end{document}

Which yields the following output (same as egreg's):

enter image description here

I think this solution is a bit better than using \lefteqn since \MoveEqLeft was made specifically to solve this problem (according to the documentation).

3
  • That's essentially equivalent, in my opinion. It allows to not add braces.
    – egreg
    Dec 15, 2018 at 15:46
  • @egreg And not to add \quad Dec 15, 2018 at 16:17
  • Well, \quad is a space of 1em, so it is exactly what's done by \MoveEqLeft[1].
    – egreg
    Dec 15, 2018 at 16:18
0

Here are two possibilities, with the flalign* environment, and \mathrlap from mathtools, or with a simple align* and the fleqn environment from nccmath:

\documentclass{article}
\usepackage{mathtools, nccmath}
\usepackage{showframe}
 \renewcommand{\ShowFrameLinethickness}{0.3pt}

\begin{document}

    This is just some text to show where the margin is. Some more and more text yay text is good.
    \begin{flalign*}
   \mathrlap{ a + b + c + d + e} \\
    &= an-expression \\
    &= some-very-longgggggggggg-expression && \text{comment} \\
    &= another-expression && \text{a little longer comment} \\
    &= 0
    \end{flalign*}
Some other text and another possibility:
\begin{fleqn}
    \begin{align*}
    a + b &+ c + d + e\\
    &= an-expression \\
    &= some-very-longgggggggggg-expression && \text{comment} \\
    &= another-expression && \text{a little longer comment} \\
    &= 0
    \end{align*}
\end{fleqn}

\end{document} 

enter image description here

14
  • Thanks! Can you make the lines after the first one a little indented? (without adding some \quad on every line) Dec 14, 2018 at 21:33
  • I've adde a second possibility with fleqn.
    – Bernard
    Dec 14, 2018 at 21:48
  • Thank you! The problem with that second solution is that the first expression (a+b+c+d+e) is actually the output of a command I created (imagine something like \mycommand{a+b+c}{d+e} and it doesn't work to put & in there. Sorry for not including that in the question, I tried to create a good MWE. Should I add the exact command? Dec 15, 2018 at 13:48
  • 1
    In this case, in my 2nd solution, you can replace the first line with something like \makebox[2em][l]{$ a + b + c + d + e $}.
    – Bernard
    Dec 15, 2018 at 14:15
  • 1
    fleqnmakes the left indent of all equations to 0 (or the value you give as an optional argument). It can be use with align, alignat, gather. It works as a switch (somewhat like the subequations environment).
    – Bernard
    Dec 15, 2018 at 18:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .