# array: set width for certain columns

I have an array (in math mode) for which I want the first column a little bit wider than the rest, and the rest should be of a consistent width. If I just write

\begin{array}{c|ccc}
10000 & 1 & 20 & 300
\end{array}


then the second, third and fourth column all have a separate width, which I want to avoid. Ideally I'd like to set the width of the first column to e.g. 2cm and the width of the remaining ones to 1cm.

Is there a way to do that? I'd like to avoid nested array environments.

I'm assuming that (a) columns 2, 3, and 4 should all be as wide as the widest element in any one of the columns -- "300" in the table you've provided -- and (b) that the contents of these contents should be centered. If this interpretation is correct, I suggest you proceed by defining a new column type, called C in the code below, that satisfies these assumptions and also places its contents in math mode by default.

I've added vertical bars to the code to make it readily visible that columns 2, 3, and 4 are indeed equally wide.

\documentclass{article}
\newlength\mylen
\usepackage{array} % for "\newcolumntype" macro
\newcolumntype{C}{>{\hfil$}p{\mylen}<{$\hfil}} % centered, in math mode, fixed width
\begin{document}
$\settowidth\mylen{300} % choose widest element in columns 2--4 \begin{array}{|c|C|C|C|} 10000 & 1 & 20 & 300\\ 1 & 101 & 555 & 888\\ \end{array}$
\end{document}

• Thank you very much! I don't completely understand the >{}p{}<{} command, what do we need the $ for? (Is there a reference for that command?) – flawr Nov 19 '16 at 21:12 • @flawr - The >{\hfil$} directive tells LaTeX to insert \hfil$ at the start of each cell, and the <{$\hfil} directive tells LaTeX to insert $\hfill at the end of each cell. The $ particles initiate and terminate inline math mode automatically. (Since an array environment must occur in math mode and the c column type applies math mode automatically (if used in an array), I assumed that the C column type should place the associated cell contents in math mode as well.) The pair of \hfil directives serves to center the material inside the cell. – Mico Nov 20 '16 at 10:06
• @flawr - That's correct: By itself, p{<width parameter>} would tell LaTeX to use text mode and to fully-justify the cell contents; think of p as being short for "paragraph mode". The >{...} and <{...} directives serve to modify the default behavior. – Mico Nov 20 '16 at 10:13
• Ok now I understand, thank you very much for answering my follow up questions! – flawr Nov 20 '16 at 10:14
• @flawr - You're most welcome! – Mico Nov 20 '16 at 10:14

A solution with \hphantom to get equal column width for the second to forth column:

\documentclass{article}
\begin{document}
$\begin{array}{c|ccc} 10000 & \hphantom{00}1 & \hphantom{0}20 & 300 \end{array}$
\end{document}


Column type p also works in array, e.g.:

\documentclass{article}
\usepackage{array}
\begin{document}
$\begin{array}{>{\centering}p{2cm}|*{3}{>{\centering}p{1cm}}} 10000 & 1 & 20 & 300 \end{array}$
\end{document}


Or aligned columns with package siunitx:

\documentclass{article}
\usepackage{siunitx}
\begin{document}
$\begin{array}{S[table-format=5]|*{3}{S[table-format=3]}} 10000 & 1 & 20 & 300 \end{array}$
\end{document}


A possibility is with dcolumn:

\documentclass{article}
\usepackage{dcolumn,booktabs}

\begin{document}

$\begin{array}{r *{3}{D{.}{.}{3.-1}}} \toprule 10000 & 1 & 20 & 300 \\ 100 & 10 & 10 & 10 \\ \bottomrule \end{array}$

$\begin{array}{r *{3}{D{.}{.}{3.-1}}} \toprule 10000 & 100 & 200 & 300 \\ 100 & 100 & 100 & 100 \\ \bottomrule \end{array}$

\end{document}


The second table shows the spacing is the same. The arguments to the D column type are, respectively the input decimal separator, the output decimal separator and the format of the numbers; here 3.-1` means “three digits in the integral part and no decimal digit.