0

I have a table that looks something like the following:

\documentclass{article}
\usepackage{booktabs}
\usepackage{dcolumn}
\newcolumntype{d}{D{.}{.}{2}}
\begin{document}
\begin{tabular}{l r@{\thinspace}d r@{\thinspace}d r@{\thinspace}d}
  \toprule
  & \multicolumn{2}{c}{Special Programs} & \multicolumn{2}{c}{Equipment Maintenance} & \multicolumn{2}{c}{General} \\
  \cmidrule{2-7}
  Category 1 & \$ & 15.25 &    &      &    &        \\
  Category 2 &    & 25.34 &    &      & \$ &  73.00 \\
  Category 3 &    &       & \$ & 5.15 &    & 128.16 \\
  \cmidrule{3-3} \cmidrule{5-5} \cmidrule{7-7}
  Totals     & \$ & 40.59 & \$ & 5.15 & \$ & 201.16 \\
  \bottomrule
\end{tabular}
\end{document}

That gives me results like the following:

enter image description here

I'm using separate columns for the dollar signs and the amounts so that the top and bottom dollar signs will always line up. Unfortunately, when the two-column headers are wider than the columns below them, the extra space is added to the amount column, which puts rather a lot of space between the dollar sign and amount.

Is there a way to reformat things so that the dollar signs are no more than a \thinspace away from the left side of the amount texts?

I do have a strong preference for letting LaTeX manage the width of the columns overall, so I'd prefer not to do anything that involves hardcoding column widths anywhere. (Among other things, all of the table contents--amounts, categories, and column names--are dynamic. I would like a solution I can put into the generating program once and not have to fine-tune for each separate dataset.)

4
  • 1
    To answer the title question, the last column always gets the extra space. Sep 4, 2019 at 23:44
  • And there's no way to give the extra space to any column other than the last one?
    – asciiphil
    Sep 5, 2019 at 21:42
  • Not automatically. You can always adjust the widths manually using \makebox. Sep 5, 2019 at 23:17
  • That wouldn't be great for this project, unfortunately, since the goal is to generate the reports automatically without needing manual layout tweaks. It looks like the siunitx solution from @leandriis will give the best results.
    – asciiphil
    Sep 9, 2019 at 13:27

1 Answer 1

3

The siunitx package can align numbers with respect to the decimal separator and keep units in the same column. In the second table I have also used the makecell package to introduce linebreaks in the long column headers.

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\DeclareSIUnit{\dollar}{\$}

\usepackage{makecell}

\begin{document}

\sisetup{table-space-text-pre=\si{\dollar}, table-align-text-pre=true}
\begin{tabular}{l S[table-format=2.2]S[table-format=1.2]S[table-format=3.2]}
  \toprule
  & {Special Programs} &{Equipment Maintenance} & {General} \\
  \cmidrule{2-4}
  Category 1 & \si{\dollar} 15.25 &                   &        \\
  Category 2 &             25.34  &                   & \si{\dollar}  73.00 \\
  Category 3 &                    & \si{\dollar} 5.15 &              128.16 \\
  \cmidrule(r){2-2} \cmidrule(lr){3-3} \cmidrule(l){4-4}
  Totals     & \si{\dollar} 40.59 & \si{\dollar} 5.15 & \si{\dollar}  201.16 \\
  \bottomrule
\end{tabular}

\bigskip

\begin{tabular}{l S[table-format=2.2]S[table-format=1.2]S[table-format=3.2]}
  \toprule
  & {\makecell{Special\\ Programs}} &{\makecell{Equipment\\ Maintenance}} & {General} \\
  \cmidrule{2-4}
  Category 1 & \si{\dollar} 15.25 &                   &        \\
  Category 2 &             25.34  &                   & \si{\dollar}  73.00 \\
  Category 3 &                    & \si{\dollar} 5.15 &              128.16 \\
  \cmidrule(r){2-2} \cmidrule(lr){3-3} \cmidrule(l){4-4}
  Totals     & \si{\dollar} 40.59 & \si{\dollar} 5.15 & \si{\dollar}  201.16 \\
  \bottomrule
\end{tabular}
\end{document}

You must log in to answer this question.

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