# Centering math while having comments to the right

I'm trying to have math like in the gather* environment, where the math equations are centered within their space, but also have comments to the right explaining the steps. How can I do this? Below is some code that right-justifies the mathematical equations within their space. How can I get that centered instead? I want to do this without lining up all the equals symbols because some of the equations have long right hand sides while others have long left hand sides.

\begin{align*}
e^{\int Pdx}(y'+Py)=e^{\int Pdx}Q & \quad\textrm{multiply both sides by }I=J=e^{\int Pdx}\\
(ye^{\int Pdx})'=e^{\int Pdx}Q & \quad\textrm{by \eqref{eq:3}}\\
ye^{\int Pdx}=\int e^{\int Pdx}Q dx + C\\
y=e^{-\int Pdx}\int e^{\int Pdx}Q dx + Ce^{-\int Pdx}\\
\end{align*}


EDIT:

I have found that you can use the array environment to do this, but the math lines look very close together. Is there a way to fix that?

\begin{displaymath}
\begin{array}{cl}
e^{\int Pdx}(y'+Py)=e^{\int Pdx}Q & \quad\textrm{multiply both sides by }I=J=e^{\int Pdx}\\
(ye^{\int Pdx})'=e^{\int Pdx}Q & \quad\textrm{by \eqref{eq:3}}\\
ye^{\int Pdx}=\int e^{\int Pdx}Q dx + C\\
y=e^{-\int Pdx}\int e^{\int Pdx}Q dx + Ce^{-\int Pdx}\\
\end{array}
\end{displaymath}

• Do you want to align the items using gather, but have the comments all left-aligned at the same location? – Werner Feb 21 '19 at 0:06
• It doesn't matter to me whether the end result uses gather as long as it looks as if it did. I'm okay with the comments being left-aligned but it would be nice to know how to configure that too. – user109923 Feb 21 '19 at 0:10
• But yes, that sounds like what I'm looking for. – user109923 Feb 21 '19 at 0:16

Enforce \displaystyle in the column of math, and stretch out the content to match what you would typically get for align-like environments: \documentclass{article}

\usepackage{amsmath,array}
\newcommand{\dx}{\mathrm{d}x}

\begin{document}

$\renewcommand{\arraystretch}{1.5} \begin{array}{ >{\displaystyle}c l } y' + Py = Q & \eqcomment{by (1)} \\ e^{\int P \dx}(y' + Py) = e^{\int P\dx} Q & \eqcomment{multiply both sides by I = J = e^{\int P \dx}} \\ (ye^{\int P \dx})' = e^{\int P \dx} Q & \eqcomment{by (3)} \\ ye^{\int P \dx} = \int e^{\int P \dx}Q \dx + C \\ y = e^{-\int P \dx}\int e^{\int P \dx}Q \dx + Ce^{-\int P\dx} \end{array}$

\end{document}

• It looks like the >{\displaystyle}c bit makes the first column display in display style, but how does that syntax work? I'm only familiar with using rcl. Incidentally, it makes my latex complain with ""Illegal character in array arg" – user109923 Feb 21 '19 at 0:48
• @user109923: You need the array package added to your preamble. The documentation explains it clearly: >{before} inserts before at the start of every cell within that column, while <{after} adds after at the end of every cell within that column. This allows you to do stuff like >{\textbullet\quad}l if you want to make a fake itemize inside a tabular, or >{\centering\arraybackslash}p{5em} for a centred column that contains paragraph text (read about \arraybackslash) in the documentation. – Werner Feb 21 '19 at 0:54

Here's an implementation that measures the comments; if there is no overlap, the code centers the equations like with gather; otherwise it centers them in the remaining space.

The tolerance for the overlap is 1em, but it can be set to a negative value in case we see that this is feasible.

As you can see, your code would push one of the equations beyond the left margin (first example), but with a negative overlap we can make it fit.

Another parameter that can be set is stretch, to allow for more vertical spacing (default 1.2, in the second example it is set to 1.8).

\documentclass{article}
\usepackage{amsmath,xparse,environ,array}

\usepackage{showframe} % just to see the text block borders

\ExplSyntaxOn
\NewEnviron{gathercomment}[]
{
\keys_set:nn { gathercomment } { #1 }
\begin{equation*}
\gathercomment:V \BODY
\end{equation*}
}

\keys_define:nn { gathercomment }
{
overlap .dim_set:N = \l__gathercomment_overlap_dim,
stretch .code:n = \renewcommand{\arraystretch}{#1},
stretch .initial:n = 1.2,
}

\seq_new:N \l__gathercomment_lines_seq
\seq_new:N \l__gathercomment_arow_seq
\dim_new:N \l__gathercomment_equations_dim
\box_new:N \l__gathercomment_equation_box
\box_new:N \l__gathercomment_comment_box

\cs_new_protected:Nn \gathercomment:n
{
\seq_set_split:Nnn \l__gathercomment_lines_seq { \\ } { #1 }
\dim_zero:N \l__gathercomment_equations_dim
\seq_map_function:NN \l__gathercomment_lines_seq \__gathercomment_measure:n
% compare the widths
\dim_compare:nTF
{
>
0.5\displaywidth
}
{% there would be overlap
\begin{tabular}
{
@{}
>{$\displaystyle}w{c}{\dim_eval:n {\displaywidth-\l__gathercomment_comments_dim - \l__gathercomment_overlap_dim}}<{$}
@{\hspace{\l__gathercomment_overlap_dim}}
@{}
}
\seq_use:Nn \l__gathercomment_lines_seq { \\ }
\end{tabular}
}
{% no overlap
\begin{tabular}
{
@{}
>{$\displaystyle}w{c}{\displaywidth}<{$}
@{}
w{r}{0pt}
@{}
}
\seq_use:Nn \l__gathercomment_lines_seq { \\ }
\end{tabular}
}
}
\cs_generate_variant:Nn \gathercomment:n { V }

\cs_new_protected:Nn \__gathercomment_measure:n
{
\seq_set_split:Nnn \l__gathercomment_arow_seq { & } { #1 }
% measure the half widths of the equations
\hbox_set:Nn \l__gathercomment_equation_box
{ $\displaystyle \seq_item:Nn \l__gathercomment_arow_seq { 1 }$ }
\dim_set:Nn \l__gathercomment_equations_dim
{
\dim_max:nn
{ \l__gathercomment_equations_dim }
{ \box_wd:N \l__gathercomment_equation_box / 2 }
}
% measure the widths of the comments
\hbox_set:Nn \l__gathercomment_comment_box
{ \seq_item:Nn \l__gathercomment_arow_seq { 2 } }
{
\dim_max:nn
{ \box_wd:N \l__gathercomment_comment_box }
}
}

\ExplSyntaxOff

\begin{document}

\begin{gathercomment}
y'+Py=Q & by \eqref{eq:1} \\
e^{\int P\,dx}(y'+Py)=e^{\int P\,dx}Q & multiply both sides by $I=J=e^{\int P\,dx}$ \\
(ye^{\int P\,dx})'=e^{\int P\,dx}Q & by \eqref{eq:3} \\
ye^{\int P\,dx}=\int e^{\int Pdx}Q dx + C \\
y=e^{-\int P\,dx}\int e^{\int P\,dx}Q\,dx + Ce^{-\int P\,dx}
\end{gathercomment}

\begin{gathercomment}[stretch=1.8,overlap=-2em]
y'+Py=Q & by \eqref{eq:1} \\
e^{\int P\,dx}(y'+Py)=e^{\int P\,dx}Q & multiply both sides by $I=J=e^{\int P\,dx}$ \\
(ye^{\int P\,dx})'=e^{\int P\,dx}Q & by \eqref{eq:3} \\
ye^{\int P\,dx}=\int e^{\int Pdx}Q dx + C \\
y=e^{-\int P\,dx}\int e^{\int P\,dx}Q\,dx + Ce^{-\int P\,dx}
\end{gathercomment}

\begin{gathercomment}
abc=def & xyz \\
x=y & xx \\
1=2 \\
xxx=xxxxxxx & x
\end{gathercomment}

\end{document} 