5

I am trying to put together a table that consists out of both text and numbers. Most of the numbers I want to align with respect to their decimal points. Nevertheless, some of the cells contain text and should therefore simply be centered. I managed to use the \multicolumn command to align specific cells correctly. Now I am wondering if there is a more efficient way to align whole rows (e.g. Row 1) instead of writing a \multicolumn command for every cell in that row. Below you can see how my table looks at the moment.

enter image description here

\documentclass{article}

% Column alignment
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{#1}}

\begin{document}

\begin{tabular}{lcc}
Column 1 & Column 2 & Column 3 \\
Row 1 & \multicolumn{1}{d{3.2}}{555} & 555\\
Row 2 & \multicolumn{1}{d{3.2}}{7.77} & 7.77 \\
Row 3 & \multicolumn{1}{d{3.2}}{99.9} & 99.9 \\
Row 4 & Text 1 & Text 2
\end{tabular}

\end{document}
3
  • 1
    The d specifier should be in the argument to tabular; use \multicolumn{1}{c} for the entries that don't fit.
    – egreg
    Oct 5, 2017 at 13:10
  • That still leaves a lot of \multicolumn commands as I basically have 50% of text in my table. The most efficient way would be to have a command that decides whether to decimal point align a row in my table or not.
    – Tom
    Oct 5, 2017 at 13:16
  • If you use siunitx S columntype you can just place inside braces the cell's content you want centered.
    – koleygr
    Oct 5, 2017 at 13:18

3 Answers 3

7

You can use siunitx, where cells that don't fit the numeric scheme have just to be braced and will be automatically centered.

\documentclass{article}

\usepackage{siunitx} % better

% the following two lines are not necessary for siunitx
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{#1}}

\begin{document}

\section{With \texttt{dcolumn}}

\begin{tabular}{ld{3.2}c}
Column 1 & \multicolumn{1}{c}{Column 2} & Column 3 \\
Row 1 & 555 & 555\\
Row 2 & 7.77 & 7.77 \\
Row 3 & 99.9 & 99.9 \\
Row 4 & \multicolumn{1}{c}{Text 1} & Text 2
\end{tabular}

\section{With \texttt{siunitx}}

\begin{tabular}{lS[table-format=3.2]c}
Column 1 & {Column 2} & Column 3 \\
Row 1 & 555 & 555\\
Row 2 & 7.77 & 7.77 \\
Row 3 & 99.9 & 99.9 \\
Row 4 & {Text 1} & Text 2
\end{tabular}

\end{document}

enter image description here

3

To cut down on the tedium of having to write \multicolumn{1}{c}{...} several times, simply set up a shortcut macro, say,

\newcommand\mc[1]{\multicolumn{1}{c}{#1}}

That'll speed up table creation a lot.

Here's how your code would look with this idea fully implemented:

\documentclass{article}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{.}{#1}}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro
\begin{document}
\begin{tabular}{ l d{3.2} c }
  Column 1 & \mc{Column 2} & Column 3 \\ % <-- note use of \mc
  Row 1    & 555           & 555    \\
  Row 2    &   7.77        &   7.77 \\
  Row 3    &  99.9         &  99.9  \\
  Row 4    & \mc{Text 1}   & Text 2  % <-- note use of \mc
\end{tabular}
\end{document}
1

Two ways:

  1. Using \phantom directly, or

  2. Incorporating \phantom into active characters # for digits and ! for the decimal point. Encapsulated in the ptabular environment.

The MWE:

\documentclass{article}
\catcode`\#=\active %
\def#{\phantom{0}}
\catcode`!=\active %
\def!{\phantom{.}}
\catcode`\#=6 %
\catcode`!=12 %
\newenvironment{ptabular}{\catcode`\#=\active \catcode`!=\active \begin{tabular}}{\end{tabular}}
\begin{document}

\begin{tabular}{lcc}
Column 1 & Column 2 & Column 3 \\
Row 1 & 555\phantom{.00} & 555\\
Row 2 & \phantom{00}7.77 & 7.77 \\
Row 3 & \phantom{0}99.9\phantom{0} & 99.9 \\
Row 4 & Text 1 & Text 2
\end{tabular}

\begin{ptabular}{lcc}
Column 1 & Column 2 & Column 3 \\
Row 1 & 555!## & 555\\
Row 2 & ##7.77 & 7.77 \\
Row 3 & #99.9# & 99.9 \\
Row 4 & Text 1 & Text 2
\end{ptabular}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .