# Alignat: column not flushing right to the margin

I am trying to use alignat* to have flushed-right comments on equations. The comments are correctly aligned, but I can't find a way to ensure they are flushing to the document's margins. Either the comments don't go all the way right:

or the comments bleed into the margin:

It seems like the comment placement is determined by the longest equation length and the longest comment length. However, that's not necessary, because some long equations have short comments that won't run into them. I would like it to be something like this:

The LaTeX code is given. I have to use the report document class. Thank you.

\documentclass[11pt]{report}
\usepackage[fleqn]{amsmath}
\usepackage{amssymb}
% For binomial coefficients that look better i.e. \mbinom
\usepackage{nccmath}
\usepackage{float}
\usepackage{geometry}
\usepackage{bbm}

\begin{document}
% First example: comments aren't far enough right
{\setlength{\mathindent}{0cm}
\begin{alignat*}{2}
&= \sum_{\substack{B \le \beta \\ |B| < \ell}} (-1)^{|B|} \mbinom{\beta}{B} f_{B} \sum_{\substack{P \le A \\ |P| = |B|}} \mbinom{A}{P} g(P + B) \Psi_{A - P} &\small\text{[induction hypothesis]} \\
&=  \sum_{P \le A } \sum_{B \le \beta}  \mathbbm{1}_{|P| = |B|}  \mathbbm{1}_{|B| < \ell} \thinspace (-1)^{|B|} \mbinom{A}{P} \mbinom{\beta}{B} \Psi_{A - P} &\small\text{[Thm.~1]} \\ \\
&= \sum_{P \le A } \sum_{B \le \beta}  \mathbbm{1}_{|P| = |B|}  \mathbbm{1}_{|P| < \ell} \thinspace (-1)^{|P|} \mbinom{A}{P} \mbinom{\beta}{B} \Psi_{A - P} &\small\text{[since $|P| = |B|$]}
\end{alignat*}}

\bigskip
\bigskip

% Second example: comments bleed into the margin
{\setlength{\mathindent}{0cm}
\begin{alignat*}{2}
&= \sum_{\substack{B \le \beta \\ |B| < \ell}} (-1)^{|B|} \mbinom{\beta}{B} H_{\beta - B} \sum_{\substack{P \le A \\ |P| = |B|}} \mbinom{A}{P} \langle H_{P}, H_{B} \rangle H_{A - P} &\small\text{[induction hypothesis]} \\
&=  \sum_{P \le A } \sum_{B \le \beta}  \mathbbm{1}_{|P| = |B|}  \mathbbm{1}_{|B| < \ell} \thinspace (-1)^{|B|} \mbinom{A}{P} \mbinom{\beta}{B}  \langle H_{P}, H_{B} \rangle \Psi_{A - P} \Psi_{\beta - B} &\small\text{[Thm.~2]} \\
&\vdots \\
&=  \sum_{\substack{P \le A  \\ |P| < |A| = \ell} }  \thinspace (-1)^{|P|} \mbinom{A}{P} H_{A - P} \thinspace \partial^{P} H_{\beta} &\small\text{[induc.~on $\partial^{P}\Psi_{\beta}$ for $|P| < \ell$]} \\
&= \mathcal{S}_2.
\end{alignat*}}

\end{document}

• ‘amsmath’ defines ‘\tag*{label}’, which typeset custom label at a place where equation number would be. It is usually at the right margin. The starred version causes the label to be typeset literally. Jun 11 at 2:08
• Welcome to tex.sx. Your hypothesis that the total width of the display block is caused by the length of the longest left-hand plus the longest right-hand component is correct. "Shortening" the over-long left-hand component of the longest line by using \mathrlap (requires mathtools) plus setting the right-hand elements using \tag* as already suggested should give the result you're looking for. (I won't say this is the best solution, but I'm not in a position to experiment at the moment.) Jun 11 at 2:40

I suggest you use an align* environment and place the comments in \tag* "wrappers"; \tag* is a macro provided by the amsmath package. User @Celdor has already made this suggestion in a comment.

To limit the extent to which the presence of the comments push the main math material to the left, I suggest "hiding" the [induction substring in an \llap directive, effectively making it disappear from the width calculations that are performed in the background.

To provide a visual clue that the \mathbbm{1}_{...} elements -- indicator functions, right? -- are "math operators" (in the same way that \ln and \sin are math operators), the code below creates a macro named \one that's given math-operator status.

\documentclass[11pt]{report}
\usepackage[showframe]{geometry} % omit 'showframe' option in real doc.
\usepackage{mathtools} % for \mathclap macro; load amsmath autom.
\usepackage{amssymb,bbm}
\newcommand\one[1]{\mathop{\mathbbm{1}^{}_{#1}}} % indicator function
\usepackage{nccmath} % For \mbinom macro
%%\usepackage{float} % not needed

\begin{document}

\begin{align*}
&= \sum_{\substack{B \le \beta \\ |B| < \ell}}
(-1)^{|B|} \mbinom{\beta}{B} f_{B}
\sum_{\substack{P \le A \\ \mathclap{|P| = |B|}}}
\mbinom{A}{P} g(P + B) \Psi_{\!A - P}
\tag*{\llap{[induction} hypothesis]} \\
&= \sum_{P \le A } \sum_{B \le \beta}
\one{|P| = |B|} \one{|B| < \ell} \,
(-1)^{|B|} \mbinom{A}{P} \mbinom{\beta}{B} \Psi_{\!A - P}
\tag*{[Thm.\ 1]} \\
&= \sum_{P \le A } \sum_{B \le \beta}
\one{|P| = |B|} \one{|P| < \ell} \,
(-1)^{|P|} \mbinom{A}{P} \mbinom{\beta}{B} \Psi_{\!A - P}
\tag*{[since $|P| = |B|$\,]}
\end{align*}
\end{document}


You can create your own macro \commentedmath with two parameters: math and comment based on TeX primitive:

\hbox to\hsize {$math$ \hss comment}


For example, like this:

\def\commentedmath#1#2{\par
\smallskip
\hbox to\hsize{$\displaystyle{}#1$\hss[#2]}
\smallskip
}

\commentedmath
{ = \sum_{\substack{B\le\beta \\ |B|<\ell}} (-1)^{|B|}
\mbinom{\beta}{B} f_{B}
\sum_{\substack{P\le A \\ |P|=|B|}}
\mbinom{A}{P} g(P + B) \Psi_{A - P} }   {induction hypothesis}
\commentedmath
{ = \sum_{P\le A} \sum_{B\le\beta} \mathbbm{1}_{|P|=|B|} \mathbbm{1}_{|B|<\ell}
\, (-1)^{|B|} \mbinom{A}{P} \mbinom{\beta}{B}
\Psi_{A-P} }                                  {Thm. 1}
\commentedmath
{ = \sum_{P\le A} \sum_{B\le\beta} \mathbbm{1}_{|P|=|B|} \mathbbm{1}_{|P|<\ell}
\; (-1)^{|P|} \mbinom{A}{P} \mbinom{\beta}{B}
\Psi_{A-P} }                                  {since $|P|=|B|$}