3

I am having trouble aligning my comments and equations in the align environment. I would like my equations as far left as possible, the numbers to the equations as far right as possible (but guaranteed to stay on the same line!), and the comment right aligned to the number of the equation, with as much space as possible between the equation and comment.

It seems like the issue is that the text comments will not start farther to the left than the right-most part of the longest equation (like the third equation below).

Example:

\documentclass[12pt, letterpaper]{article}
\usepackage{amsmath}

\begin{document}
    
\begin{align}
&f'\left(0\right)=\lim\limits_{x\to 0} \frac{f\left(x\right)-f\left(0\right)}{x-0} 
&\text{ limit definition of derivative}\\
&=\lim\limits_{x\to 0} \frac{x^2 \sin\left(\frac{1}{x}\right)}{x}=\lim\limits_{x\to 0} x\sin\left(\frac{1}{x}\right) 
&\text{ simplifying the limit}\\
&0\leq\left| \sin \left(\frac{1}{x}\right) \right| \leq 1 \implies 0\leq \left|x \sin \left(\frac{1}{x}\right)\right|\leq \lvert x\rvert 
&\text{bound for sine}
\end{align}
\end{document}

Output:

enter image description here

So far I have tried using the double && and the flalign package, but neither have helped me.

3 Answers 3

4

If you measure the left part in the last line and right part in the first line, you find that their sum exceeds the line width.

The align environment will never make two columns overlap. You might exploit the particular case, where the longer text will fit in the first line even if a small overlap with the final line appears.

Not a general method, I'm afraid: each elephant needs its particular method to be stuffed in a suitcase.

I tried with \hspace{-2em} without success. With \hspace{-2.5em} the amount of overlap is smaller.

\documentclass[12pt, letterpaper]{article}
\usepackage{amsmath}

\begin{document}

\begin{align}
&f'(0)=\lim_{x\to 0} \frac{f(x)-f(0)}{x-0}
&\text{\hspace{-3em}limit definition of derivative}\\
&=\lim_{x\to 0} \frac{x^2 \sin\left(\frac{1}{x}\right)}{x}
 =\lim_{x\to 0} x\sin\left(\frac{1}{x}\right)
&\text{simplifying the limit}\\
&0\leq\left| \sin \left(\frac{1}{x}\right) \right| \leq 1 \implies
 0\leq \left|x \sin \left(\frac{1}{x}\right)\right|\leq \lvert x\rvert
&\text{bound for sine}
\end{align}

\end{document}

Avoid \left and \right unless necessary (and consider that they essentially only work well with fractions or arrays).

Also \limits is to be very rarely used when doing inline math and does nothing when in display math.

enter image description here

1
  • +1 for "each elephant needs its particular method to be stuffed in a suitcase". :-)
    – Mico
    Nov 12, 2022 at 10:06
4

[I'd like] the comment right aligned to the number of the equation, with as much space as possible between the equation and comment. [emphasis added]

You may achieve this objective by employing \tag* directives to typeset both the comments and the equation numbers .

Furthermore, I'd like to suggest that you nest the align environment -- to cover just the first two rows -- in a gather environment. Why? It's because there does not appear to be a natural alignment point for row 3 that relates meaningfully to the natural alignment point -- the first = symbol -- in rows 1 and 2.

Oh, and don't overuse (and thus abuse) \left and \right.

enter image description here

The frame lines at the left-hand and right-hand edges are there courtesy of the showframe package.

\documentclass[12pt, letterpaper]{article}
\usepackage{showframe} % draw frame lines around text block
\usepackage{mleftright}
\usepackage{mathtools} % for "\DeclarePairedDelimiter" macro
\DeclarePairedDelimiter{\abs}{\lvert}{\rvert} % optional
\newcommand\puteqnum{\refstepcounter{equation}(\theequation)} % typeset eq. num.

\begin{document}
    
\begin{gather}
\begin{align}
f'(0)
&=\lim_{x\to 0} \frac{f(x)-f(0)}{x-0} 
  \tag*{limit definition of derivative \puteqnum}\\
&=\lim_{x\to 0} \frac{x^2 \sin\bigl(\frac{1}{x}\bigr)}{x}
 =\lim_{x\to 0} x\sin\mleft(\frac{1}{x}\mright) 
  \tag*{simplifying the limit \puteqnum}
\end{align}\\[1ex]
0\leq\abs*{\sin \mleft(\frac{1}{x}\mright)}   \leq 1 
\implies 
0\leq\abs*{x \sin \mleft(\frac{1}{x}\mright)} \leq \abs{x} 
  \tag*{bound for sine \puteqnum}
\end{gather}

\end{document}
2

If You add the line:

\usepackage[margin=2cm]{geometry}

in the preambule You have n the same line: equation comment, equation number. This way my output is:

enter image description here

4
  • Can you modify your solution method to satisfy the OP's requirement that "the comment [should be] right aligned to the number of the equation"?
    – Mico
    Nov 12, 2022 at 10:26
  • It is right aligned! Nov 12, 2022 at 10:39
  • Alright, the OP also requires "with as much space as possible between the equation and comment". The noticeable gap between the comment and the equation number would appear to interfere with fulfilling that requirement, no?
    – Mico
    Nov 12, 2022 at 10:47
  • @Mico OK. I forget "with as much space as possible between the equation and comment". Nov 12, 2022 at 11:44

You must log in to answer this question.

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