# horizontal spacing in equations

I am trying to replicate the same exact output as plain math mode would give for a simple polynomial formula but using \matrix because I want to right-align the terms in the polynomial or leave some of them blank. I can't use \alignat either for other reasons.

I have made some progress but the spacing is hard to get right. In the following picture you can see the output in plain math mode, using \alignat and my attempt using \matrix (I use the convenience macro \arr).

As you can see, my version is somewhat off. I think the biggest error is in the spacing around the equals sign.

\documentclass[a4paper,11pt]{article}
\usepackage{amsmath}
\makeatletter
% http://tex.stackexchange.com/questions/2233/
\renewcommand*\env@matrix[1][*\c@MaxMatrixCols r]{%
\hskip -\arraycolsep
\let\@ifnextchar\new@ifnextchar
\array{#1}}
\newcommand{\arr}{\@ifstar\arr@star\arr@nostar}
\newcommand{\arr@nostar}[2][*\c@MaxMatrixCols r]{%
\begin{bmatrix}[#1]#2\end{bmatrix}%
}
\newcommand{\arr@star}[2][*\c@MaxMatrixCols r]{%
\begin{matrix}[#1]#2\end{matrix}%
}
\makeatother

\begin{document}

$$C + 3D = 2$$
%
\begin{alignat}{3}
C  &+{}& 3D &={}& 2 & \notag
\end{alignat}
%
$$\arr*[@{}r@{\,}c@{\,}r@{\,}c@{\,}r@{}]{ C &+{}& 3D &=& 2 \notag }$$

\end{document}


Would you consider a tabular stack as an option?

Here I set it up with the one-time invocation:

\usepackage{tabstackengine}
\stackMath
\setstacktabulargap{0pt}
\TABbinary


and then I invoke one of various forms of tabular stack using a syntax that mimics the tabular separators.

\documentclass[a4paper,11pt]{article}
\usepackage{amsmath}
\usepackage{tabstackengine}
\stackMath
\setstacktabulargap{0pt}
\TABbinary
\begin{document}

$$C + 3D = 2$$
%
\begin{alignat}{3}
C  &+{}& 3D &={}& 2 & \notag
\end{alignat}
%
$$\tabularCenterstack{rcrcr}{ C&+&3D&=&2 }$$
$$\tabularCenterstack{rcrcr}{ C&+&3D&=&2\\ 4C&-&D&=&27 }$$
\end{document}


• I didn't know about tabular stack but it seems to do exactly what I want. – Ernest A Apr 22 '15 at 17:38
• @ErnestA Great! Glad to hear that my package can help you. I should add that the \TABbinary essentially places a {} before and after each tabbed field, so that you don't have to add them manually as in {}+{}, etc. There are other options that only auto-adds the {} to the left or the right of the field. – Steven B. Segletes Apr 22 '15 at 18:00