I'm trying to use \valign
to make an array of items. (See my self-answer to another question.) I'm doing this because I want to specify the items per column instead of per row, and because I want control over the spacing. I'm using LaTeX but these are plain-tex macros.
I'm happy with the following, except that the items are not vertically centred within their column, so this question is about how to achieve that.
\documentclass{article}
\usepackage{amsmath}
% this can't be an environment because the code for \end can't have parameters.
\newcommand{\colarray}[3]{
\vcenter{\hbox{\valign{&\vfil\vbox{\parindent=0pt\tabskip=#2\hsize=#1##}\vfil\cr%
#3%
}}\vskip-#2}%
}
\newcommand*\col[1]{#1\cr}
\begin{document}
\begin{equation}
x = \colarray{4mm}{2mm}{
\col{1 & 2 & 3}
\col{4 & 5 & \Big(6\Big)}
}
\end{equation}
\end{document}
This produces
From my current rather hazy understanding, centering the items should involve putting each one in an \hbox
and using \vfil
. However, if I try that the results are not as expected:
\documentclass{article}
\usepackage{amsmath}
% this can't be an environment because the code for \end can't have parameters.
\newcommand{\colarray}[3]{
\vcenter{\hbox{\valign{&\vfil\vbox{\parindent=0pt\tabskip=#2\hsize=#1\hbox to #1{\hfil##\hfil}}\vfil\cr%
#3%
}}\vskip-#2}%
}
\newcommand*\col[1]{#1\cr}
\begin{document}
\begin{equation}
x = \colarray{4mm}{2mm}{
\col{1 & 2 & 3}
\col{4 & 5 & \Big(6\Big)}
}
\end{equation}
\end{document}
Here's the result:
As you can see the '3' is horizontally aligned differently from the '1' and '2', and the '(6)' is not horizontally centred with respect to the '4' and '5'.
I am not able to work out why this is, so my questions are (1) why is this happening, and (2) how can I fix it?
\valign
, you give up the common baseline that\halign
would give you. To compensate for that, you can make all vboxes in your alignment the same height and insert a "strut" to control the baseline. See this answer for an example.