8

I want to use the right side of an aligned environment to present the reason for the next line. If the explanation requires more than one line the alignment fails. This could be eye-balled by adding a horizontal space but there should be a better way.

How can this be fixed?

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align}
  a + b &= \quad \text{short reason} \\
  c + d &= \quad \text{a really long and} \\
        &\phantom{=} \quad \text{complicated reason} \notag \\
  e + f \text{.}
\end{align}
\end{document}

If you compile the above code you will see what I mean. I can't figure out how to add compiled code to a question. If you know how do add compiled code to the question, please do so.

enter image description here

2
  • 1
    You can make a screenshot of the result and add it as in image, clicking on the 6th icon in the edit toolbar the icon which looks like a mountain).
    – Bernard
    Commented Jul 16, 2018 at 15:06
  • 1
    You might also want to add a & on line 8 before \text{.}, because currently that line of the equation is a little bit further to the left.
    – Flexo013
    Commented Jul 16, 2018 at 15:10

4 Answers 4

10

One option is to use the aligned construct for the multi-line comments as below.

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align}
  a + b &= \quad \text{short reason} \\
  c + d &= \quad \begin{aligned}[t]&\text{a really long and} \\
                                   &\text{complicated reason}\end{aligned}\\
  e + f \text{.}
\end{align}
\end{document}

enter image description here

2
  • 1
    if the "long and complicated" text takes even more lines, the lines are really too far apart for it to be recognized easily as a unit. a paragraphing approach (with ragged right text) is better. Commented Jul 18, 2018 at 19:49
  • Notably this solution also works in mathjax, which doesn't support parbox
    – Migwell
    Commented Apr 19, 2020 at 12:00
8

with use of parbox:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\begin{align}
  a + b &= \quad \text{short reason} \\
  c + d &= \quad \parbox[t]{0.35\textwidth}{
                    really long and complicated\\
                    reason in two lines} \\
  e + f &=\text{.}
\end{align}
\end{document}

enter image description here

(red lines indicate text borders)

supplement: considering comment of barbara beton, the above solution should be:

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\begin{align}
  a + b & = \quad \text{short reason} \\
  c + d & = \quad \parbox[t]{0.35\textwidth}{\raggedright  % <--- 
                    really long and complicated\\
                    reason in two lines} \\
  e + f & = .
\end{align}
\end{document}

in this simple case result is the same as at the first case, however if there's really long word that can't fit on the first line, added \ragged will avoid wide spaces between words in the first line.

4
  • Another good way to solve this alignment problem.
    – Jay
    Commented Jul 16, 2018 at 22:14
  • although it's not obvious here, if there's a really long word that can't fit on the first line, it would be better to specify ragged right to avoid wide spaces in the first line. Commented Jul 18, 2018 at 19:51
  • @barbarabeeton, thank you very much for your comment. i consider it in my answer. now result will look better also at circumstances of very long words in text.
    – Zarko
    Commented Jul 18, 2018 at 21:03
  • 1
    I think this is the only correct answer.
    – GuM
    Commented Jul 19, 2018 at 0:57
4

A variation on @AboAmmar's answer, but using an environment that takes text and paragraphs:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{align}
  a + b &= \quad \text{short reason} \\
  c + d &= \quad \begin{tabular}{@{}p{0.8\textwidth}}
                    really long and\\
                    complicated reason
                 \end{tabular} \\
  e + f &=\text{.}
\end{align}
\end{document}

to produce

enter image description here

Of course, the 0.8\textwidth might need tweaking...

4
  • Thanks. Typically the reasons are no more than two lines so I accepted AboAmmar's answer. But I will remember your answer if there are really, really long reasons.
    – Jay
    Commented Jul 16, 2018 at 15:25
  • @Jay It's AboAmmar's idea so he deserves the tick on principle!
    – user30471
    Commented Jul 16, 2018 at 15:26
  • I up-ticked AboAmmar's answer and accepted it as well.
    – Jay
    Commented Jul 16, 2018 at 22:07
  • 1
    the second group would really look better if the numbe were not dropped down a line. (i would also prefer that the baseline of the text be the same as that of the math at the left, but that's more subjective.) Commented Jul 18, 2018 at 19:47
0

Another way could be using \begin{center} line_one\\line_two \end{center}

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\begin{center}
      \begin{equation}
        a + b = \quad \text{short reason}
      \end{equation}  
      \begin{equation}
        c + d = \quad \parbox[t]{0.35\textwidth}{
                        really long and complicated\\
                        reason in two lines}
      \end{equation}
      \begin{equation}
        e + f =\text{.}
      \end{equation}
\end{center}
\end{document}

The result would be like below image (Green color is drawn by hand just for showing the page's border) enter image description here

1
  • 2
    In the code you use \parbox[t]{0.35\textwidth}{...}, that is already suggested in the answer by Zarko. Also, the question is about an align environment, so the equal signs should be aligned. This is not the case in your answer. The equation environment is not really suitable for several directly consecutive equations; there is align for that.
    – dexteritas
    Commented Apr 15, 2023 at 8:54

You must log in to answer this question.

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