# siunitx and vertical line

I used siunitx so numbers in a table can be aligned. However, a vertical line is lost between two columns. Also, numbers in the last column are not vertically aligned. How can this fixed? Thank you!

\documentclass{article}
\usepackage{array}
\usepackage{siunitx}

\newcolumntype{L}[1]{>{\raggedright\arraybackslash}m{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}

\begin{table}
\makebox[\linewidth][c]{\begin{tabular}{| L{4.2cm} | C{2.6cm} | S[table-format=9.6] |  C{2.6cm} | C{2.6cm} | C{2.6cm} |}
\hline
& \bfseries 1988& {\centering} {\bfseries 1989} & \bfseries 1990 & \bfseries Change \\ [2ex]
\hline
Score & 249 & 234 & 266 & +17 \\ [1.8ex]
\hline
Percent High & 14 & 9 & 26 & +12 \\ [1.8ex]
\hline
\end{tabular}}
\end{table}

\end{document}


• You are missing a | character: \begin{tabular}{| L{4.2cm} | C{2.6cm} | S[table-format=9.6] | C{2.6cm} | C{2.6cm} | C{2.6cm} |}. I cannot see any problem in the last column. – Gonzalo Medina Sep 19 '13 at 5:16
• Now I see the problem in the last column. – Gonzalo Medina Sep 19 '13 at 5:24
• @GonzaloMedina I don't see what's wrong: could you explain (or add an image)? – Joseph Wright Sep 19 '13 at 5:54
• Thanks! How can numbers in all 4 columns be aligned? I can't seem to get it work. And yes, entries in the last column are not centered vertically... – user36923 Sep 19 '13 at 6:03
• A separate matter: In a column of type S, {\centering} {\bfseries 1989} is a bit redundant; {\bfseries 1989} suffices to center the 1989 entry. – Mico Sep 19 '13 at 6:06

This is an extension to Mico's wunderful answer. Instead of explicit struts the internal struts, which are automatically set by the tabular code, can be increased by redefining the scaling factor \arraystretch. The second example uses less lines with a smaller stretch factor:

\documentclass{article}

\usepackage{tabularx}
\usepackage{siunitx}
\sisetup{retain-explicit-plus}

\begin{document}
\begin{table}[h!]
\renewcommand*{\arraystretch}{1.5}
\multicolumn{1}{X|}{\centering\textbf{#1}}%
}%
\sisetup{table-format=3.0}%
\begin{tabularx}{\linewidth}{|l|S|S|S|S[table-format=4.0]|}
\hline
\hline
Score & 249 & 234 & 266 & +17 \\
\hline
Percent High & 14 & 9 & 26 & +12 \\
\hline
\end{tabularx}
\end{table}
\begin{table}[h!]
\renewcommand*{\arraystretch}{1.2}
\multicolumn{1}{X}{\centering\textbf{#1}}%
}%
\sisetup{table-format=3.0}%
\begin{tabularx}{\linewidth}{l|SSSS[table-format=4.0]}
\hline
Score & 249 & 234 & 266 & +17 \\
Percent High & 14 & 9 & 26 & +12 \\
\end{tabularx}
\end{table}
\end{document}


Remarks:

• The numbers are set with column specifier S of package siunitx, but the table is a tabularx, where the header row uses the X columns with \centering. This way the column is horizontally centered together with aligned numbers.

• The standard fonts have a quite large plus sign, thus I had to use table-format=4.0 for this column to avoid overful \hbox warnings.

• There is also \extrarowheight of package array to add some space at the top of a row.

I wonder if you might be better off redesigning the entire table. As it stands, there are a lot of extra commands to adjust vertical widths as well as the horizontal positions of the column entries. Here are some suggestions; implementation is shown further below.

• It looks like you want to force the table to take up the full width of the text block. To achieve this, consider using either a tabular* or a tabularx environment instead of forcing the table's contents into a \makebox instruction -- which, in any case, gives you something that's much wider than \textwidth.

• If you want to keep using vertical lines to separate the columns visually, consider inserting "struts" rather than using instructions such as \\[1.8ex].

• Consider getting rid of the vertical rules entirely. Trust me, they won't be missed. Give it a try.

• Even better than using struts, consider loading the booktabs package and using its commands \toprule, \midrule, and \bottomrule. You'll automatically get proper spacing above and below these lines.

• If you don't want to use the tabular* environment, you could use the tabularx environment.

• To align the numerical entries of each column correctly, you could use the siunitx package and its S column type.

\documentclass{article}
\usepackage{booktabs,tabularx,siunitx}
\sisetup{table-format=3.0,retain-explicit-plus=true}
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}m{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\newcommand\TB{\rule[-1.5ex]{0ex}{4.7ex}} % "top&bottom" strut
\begin{document}

\begin{table}[t]
\caption{Original form of table, minimally fixed. Note that the table is much too wide.}
\makebox[\linewidth][c]{\begin{tabular}{| L{4.2cm} | C{2.6cm} | S[table-format=9.6] |  C{2.6cm} | C{2.6cm} | C{2.6cm} |}
\hline
& \bfseries 1988& {\centering} {\bfseries 1989} & \bfseries 1990 & \bfseries Change \\ [2ex]
\hline
Score & 249 & 234 & 266 & +17 \\ [1.8ex]
\hline
Percent High & 14 & 9 & 26 & +12 \\ [1.8ex]
\hline
\end{tabular}}
\end{table}

\begin{table}[h!]
\caption{Correct width, use of \texttt{tabular*} environment and explicit struts, \texttt{c}~column types}
\begin{tabular*}{\textwidth}{ |l|@{\extracolsep{\fill}}c|c|c|c| }
\hline
\TB & \bfseries 1988& {\bfseries 1989} & \bfseries 1990 & \bfseries Change \\
\hline
Score\TB & 249 & 234 & 266 & +17 \\
\hline
Percent High\TB & 14 & 9 & 26 & +12 \\
\hline
\end{tabular*}
\end{table}

\begin{table}[h!]
\caption{\texttt{tabular*} environment, \texttt{c}~column types, use \texttt{booktabs} package's rules (struts no longer needed!), get rid of vertical lines}
\begin{tabular*}{\textwidth}{ l@{\extracolsep{\fill}}cccc }
\toprule
& \bfseries 1988& {\bfseries 1989} & \bfseries 1990 & \bfseries Change \\
\midrule
Score & 249 & 234 & 266 & +17 \\
Percent High & 14 & 9 & 26 & +12 \\
\bottomrule
\end{tabular*}
\end{table}

\begin{table}[h!]
\caption{\texttt{tabularx} environment, centered columns, \texttt{booktabs} package in use}
\begin{tabularx}{\textwidth}{ lYYYY }
\toprule
& \bfseries 1988& {\bfseries 1989} & \bfseries 1990 & \bfseries Change \\
\midrule
Score & 249 & 234 & 266 & +17 \\
Percent High & 14 & 9 & 26 & +12 \\
\bottomrule
\end{tabularx}
\end{table}

\begin{table}[h!]
\caption{\texttt{tabular*} environment, columns of type \texttt{S}, \texttt{booktabs} package in use}
\begin{tabular*}{\textwidth}{ l @{\extracolsep{\fill}} *{4}{S} }
\toprule
& \bfseries 1988& {\bfseries 1989} & {\bfseries 1990} & {\bfseries Change} \\
\midrule
Score & 249 & 234 & 266 & +17 \\
Percent High & 14 & 9 & 26 & +12 \\
\bottomrule
\end{tabular*}
\end{table}

\end{document}