1

Is it possible to align the numbers in the last column of a table with color? For this column, I would like the numbers (they are actually text) to be in the center of the table, but aligned to the right of last number. I was able to align them using the siunitx package, but the color and last vertical line were gone. Is there an easy way to do keep the color/vertical line and align the text? Here's my code:

\documentclass{article}
\usepackage{array}
\usepackage{graphicx,xcolor,colortbl}

\definecolor{LightDandelion}{RGB}{255,255,204}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}m{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{D}[1]{>{\centering\arraybackslash\columncolor{LightDandelion}}m{#1}}

\begin{document}


{\renewcommand{\arraystretch}{1.1}%

\newcommand\B{\rule[-1.5ex]{0pt}{0pt}}

\begin{table}
\makebox[\linewidth][c]{\begin{tabular}{| L{4.5cm} | C{1.8cm} | C{1.8cm} | C{1.8cm} | D{1.8cm} |}
\hline
& \multicolumn{4}{c|}{\bfseries Scores}  \\ \hline
& {\centering} {\bfseries 1988} & {\centering} {\bfseries 1989} & {\centering} {\bfseries 1990} & {\centering} {\bfseries Change} \\ \hline
{\bfseries Overall}  & {\bfseries 234} & {\bfseries 218} & {\bfseries 198} & {\bfseries-36}   \\ \hline
Sub Score 1 & 262 & 257 & 253 & -9   \\ \hline
Sub Score 2 & 227 & 214 & 196 & -31   \\ \hline
Sub Score 3 & 187 & 188 & 353 & +166   \\ \hline
\end{tabular}}
\end{table}
\end{document}
  • \usepackage{siunitx} in the preamble and then replace the D{1.8cm} with >{\columncolor{LightDandelion}}S[table-format=4.] – Gonzalo Medina Sep 20 '13 at 3:10
1

In this case, I just put the entries from the last column into a right-justified fixed-width box via the macro defined as \rjust. The width of the box I have hardwired as the widest element of the column, in this case +166.

\documentclass{article}
\usepackage{array}
\usepackage{graphicx,xcolor,colortbl}
\usepackage{calc}

\definecolor{LightDandelion}{RGB}{255,255,204}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}m{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{D}[1]{>{\centering\arraybackslash\columncolor{LightDandelion}}m{#1}}
\newcommand\rjust[1]{\makebox[\widthof{\mdseries+166}][r]{#1}}
\begin{document}
{\renewcommand{\arraystretch}{1.1}%
\newcommand\B{\rule[-1.5ex]{0pt}{0pt}}
\begin{table}
\makebox[\linewidth][c]{\begin{tabular}{| L{4.5cm} | C{1.8cm} | C{1.8cm} | C{1.8cm} | D{1.8cm} |}
\hline
& \multicolumn{4}{c|}{\bfseries Scores}  \\ \hline
& {\centering} {\bfseries 1988} & {\centering} {\bfseries 1989} & {\centering} {\bfseries 1990} & {\centering} {\bfseries Change} \\ \hline
{\bfseries Overall}  & {\bfseries 234} & {\bfseries 218} & {\bfseries 198} & {\bfseries\rjust{$-$36}}   \\ \hline
Sub Score 1 & 262 & 257 & 253 & \rjust{$-$9}   \\ \hline
Sub Score 2 & 227 & 214 & 196 & \rjust{$-$31}   \\ \hline
Sub Score 3 & 187 & 188 & 353 & \rjust{+166}   \\ \hline
\end{tabular}}
\end{table}
\end{document}

enter image description here

| improve this answer | |
  • Why $-$9 and not $-9$? (Not really a big deal, I know.) – Torbjørn T. Oct 5 '13 at 23:00
  • @TorbjørnT. Didn't matter in this case, I suppose, but in case someone had the math numerals distinctly defined from the text numerals... – Steven B. Segletes Oct 7 '13 at 3:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.