Aligning numbers in an array

I need to typeset column vectors containing two parameters (variables) and an absolute value. All coefficients can be either integer or decimal numbers. I would like to achieve that all components are nicely aligned (numbers at decimal point if any, variables and operators).

The MWE

\documentclass{article}

\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{siunitx}

\begin{document}

\section{Not aligned}

\subsection{Integer coefficients}

\begin{math}
\begin{pmatrix}
2 - 2 \, s + t \\
28 - 3 \, t \\
-14 - 3 \, s + 4 \, t
\end{pmatrix}
\end{math}

\subsection{Decimal coefficients}

\begin{math}
\begin{pmatrix}
2 - \num{2,3} \, s + t \\
28 - \num{3,1} \, t \\
\num{-14,75} - 3 \, s + 4 \, t
\end{pmatrix}
\end{math}

\section{Manually aligned}

\subsection{Integer coefficients}

\begin{math}
\left(
\begin{array}{rcrcr}
2 & - & 2 \, s & + & t \\
28 & & & - & 3 \, t \\
-14 & - & 3 \, s & + & 4 \, t
\end{array}
\right)
\end{math}

\subsection{Decimal coefficients}

\begin{math}
\left(
\begin{array}{rcrcr}
2 & - & \num{2,3} \, s & + & t \\
28 & & & - & \num{3,1} \, t \\
\num{-14,75} & - & 3 \, s & + & 4 \, t
\end{array}
\right)
\end{math}

\end{document}


produces the following result.

In both examples in section 2, the spacing before and after the operator is far too much. In section 2.2, the numbers are not aligned at their decimal points.

(1) How can I fix these issues?

(2) Is there any package that does the job automatically without the need to manually place & characters?

• speces between columns in array you can set (reduce) with \setlength\arraycolsep{<desired distance>}, for example \setlength\arraycolsep{1 pt}. Dec 6, 2017 at 22:12

The column specifications of environment array are not limited to c, l, or r:

\documentclass{article}

\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{siunitx}

\begin{document}

\begin{math}
\left(
\begin{array}{
@{\,}
S[table-format=-2.2]
@{\>}c@{\>}
S[table-format=1.1]
@{\,}c@{\>}c@{\>}
S[table-format=1.1]
@{\,}c
@{\,}
}
2 & - & 2,3 & s & + & & t \\
28 & & & & - & 3,1 & t \\
-14,75 & - & 3 & s & + & 4 & t
\end{array}
\right)
\end{math}

\end{document}


However, I have the suspicion, that the vertical alignments are a little overkill and a simple non-aligned version suffices.

• This is exactly what I was looking for. You may be right in stating, however, that the alignment does not improve readability ... Dec 6, 2017 at 22:17

If you don't mind decimal coefficients not being aligned on the decimal comma, the systeme package gives you an easy and short code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage{systeme}

\begin{document}

\subsection*{Integer coefficients}

\begin{math}
\sysdelim()\systeme{2-2s + t, 28-3t, -14-3s + 4t}
\end{math}

\subsection*{Decimal coefficients}

\begin{math}
\sisetup{ copy-decimal-marker}
\sysdelim()\systeme{\num{2} - \num{2,3}s + t,\num{28} - \num{3,1}t, -\num{14,75} - 3s + 4t}
\end{math}

\end{document}


• I have just tested that it is even possible to add the \, between numbers and variables (such as employed in the question) when using systeme. Dec 6, 2017 at 22:34
• @Matthias: I didn't even think of testing this point! Well, that's fine, as I think it improves readability in this case (aligned variables). Dec 6, 2017 at 22:41

Two more array-based solutions: the first with simple right-alignment of the numbers, the second with decimal-alignment of the numbers.

\documentclass{article}
\usepackage{array}
\newcolumntype{C}{>{{}}c<{{}}}
\usepackage{siunitx}
\newcolumntype{T}[1]{S[table-format=#1]}
\begin{document}
$\setlength\arraycolsep{0pt} \left(\begin{array}{r C r C r} 2 & - & 2.3s & + & t \\ 28 & & & - & 3.1 t \\ -14.75 & - & 3s & + & 4 t \end{array}\right)$

$\setlength\arraycolsep{0pt} \left(\begin{array}{T{-2.2} C T{1.1}r C T{1.1}r} 2 & - & 2.3 & s & + & & t \\ 28 & & & & - & 3.1 & t \\ -14.75 & - & 3 & s & + & 4 & t \end{array}\right)$
\end{document}

• The definition of the column types is a very elegant approach. I would just add @{\,} between number and variable columns. Dec 6, 2017 at 22:48
• @Matthias - As always, there's no arguing about tastes. :-) I suppose it matters what "s" and "t" denote. If they're units, e.g., if s denotes "second", then it would indeed be customary to insert a thinspace between the number and the associated unit. If s and t denote "ordinary" variables, and if this were my own work, I would probably not insert thinspace. Aside: Would you insert thinspaces in the expression 5x+6y=2z? (I wouldn't...)
– Mico
Dec 6, 2017 at 22:53