1

I wanted columns that were ragged left as well as pushed up against the left. I achieved this with dummy (Z) columns of 0pt width and multicolumn headers.

However, now I can't add any space between the columns because it messes with the alignment. Even trying to add 0pt of space causes a misalignment. What is going on here?


MWE

\documentclass{report}
\usepackage{inputenc}
\usepackage{array}
\usepackage{booktabs}
\usepackage[singlelinecheck=false]{caption}

\newcommand{\mc}{\multicolumn} % multicolumn is too many letters...
\newcolumntype{Z}{@{}p{0pt}}   % dummy column

\begin{document}

% Value column is correctly aligned
\begin{table}[h]
\caption*{\textit{Value} column is correctly aligned}
\begin{tabular}[l]{@{} r@{ }rZ rZ @{}}
\toprule
    \mc{3}{@{}l}{Time} & \mc{2}{l@{}}{Value} \\
\midrule
            6h &  0m & &              -1.1 & \\
           19h & 15m & &              26.3 & \\
\bottomrule
\end{tabular}
\end{table}

% With \hskip, Value column is not correctly aligned
\vspace{2em}
\begin{table}[h]
\caption*{\textit{Value} column is not correctly aligned}
\begin{tabular}{@{} r@{ }rZ @{\hskip 20pt} rZ @{}}
\toprule
    \mc{3}{@{}l}{Time} & \mc{2}{l@{}}{Value} \\
\midrule
            6h &  0m & &              -1.1 & \\
           19h & 15m & &              26.3 & \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

Edit: Image

enter image description here


Edit2: Answer

siunitx seems to be what I want. The following table is way better for what I'm trying to do.

\begin{tabular}[h!]{
    @{}
    S[
        table-format=2,
        table-space-text-post=\si{\hour},
        table-number-alignment=left]
    @{\hspace{5pt}}
    S[
        table-format=2,
        table-space-text-post=\si{\meter},
        table-number-alignment=left]
    S[
        table-format=-1.1,
        table-number-alignment=left]
    @{}
}
\toprule
    \mc{2}{@{}l}{Time} & {Value} \\
\midrule
     6 \si{\hour} &  0 \si{\meter} & -1.1 \\
    19 \si{\hour} & 15 \si{\meter} & 26.3 \\
\bottomrule
\end{tabular}

\si{\meter} is a bit of a hack, but I prefer m to min for minute as it takes up less space and is consistent with d for day and h for hour.

enter image description here

Thanks to everyone who replied!

3
  • It's hard to guess the intention of the Z column (whatever it is p{0pt} can't be the right way to get any effect) Nov 3, 2016 at 7:52
  • The effect of the Z column is to push the right-aligned sub-columns up against the left side of the multicolumn. This could also be accomplished with another r column, for instance, but the extra column messes with the spacing between the multicolumns. I defined a columntype with zero width I could achieve the left/right alignment without disrupting with the spacing.
    – hhquark
    Nov 3, 2016 at 14:50
  • There can't be any need to have spurious columns here. Nov 3, 2016 at 14:57

2 Answers 2

1

I'm not sure what you mean by “value column is correctly aligned”, as it doesn't seem to be.

Numeric tables are better treated with the help of siunitx:

\documentclass{report}
\usepackage{array}
\usepackage{siunitx,booktabs}

\begin{document}

Value column is correctly aligned

\begin{tabular}{@{} r@{\,}r S[table-format=-1.1] @{}}
\toprule
\multicolumn{2}{@{}l}{Time} & {Value} \\
\midrule
 6h &  0m & -1.1 \\
19h & 15m & 26.3 \\
\bottomrule
\end{tabular}

\medskip

\begin{tabular}{
  @{} 
  r@{\,}r % time columns
  @{\hspace{20pt}}
  S[table-format=-1.1] % value column
  @{}
}
\toprule
\mc{2}{@{}l}{Time} & {Value} \\
\midrule
 6h &  0m & -1.1 \\
19h & 15m & 26.3 \\
\bottomrule
\end{tabular}

\end{document}

The format is -1.2 because a minus sign is wider than a digit; if the first value had been -21.1, the format should be -2.1 and so on.

Note that the [l] option to tabular does nothing at all: the meaningful options are c (default), t or b. Using \hskip in LaTeX documents is not recommended: better using \hspace.

enter image description here

4
  • See edit for what I mean by misaligned. And thanks for the note about the [l] option. I was not aware of the siunitx package. I'll play with it a bit. I think it might be what I want. If it is, I'll mark this as the answer.
    – hhquark
    Nov 3, 2016 at 14:40
  • @hhquark I got the same result as your image and that's why I'm not sure what you mean by “correct alignment” in the first table: to my eyes it is incorrect as well.
    – egreg
    Nov 3, 2016 at 15:31
  • In the correct table, the 'V' in Value lines up with the '-' in -1.1. In the incorrect table, it does not. It gets pushed over to the right a bit.
    – hhquark
    Nov 3, 2016 at 18:52
  • @hhquark That alignment in the first table is just by chance.
    – egreg
    Nov 3, 2016 at 20:32
1

Not sure what alignment you intended, but perhaps one of these

enter image description here

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{dcolumn}
\usepackage{booktabs}
\usepackage[singlelinecheck=false]{caption}
\newcolumntype{d}{D..{2.1}}
\newcommand{\mc}{\multicolumn} % multicolumn is too many letters...
%what?\newcolumntype{Z}{@{}p{0pt}}   % dummy column

\begin{document}

% Value column is correctly aligned
\begin{table}[htp]
\caption*{\textit{Value} column is correctly aligned}
\begin{tabular}{@{}rr@{\qquad} d @{}}
\toprule
    \mc{2}{@{}l}{Time} & \mc{1}{c@{}}{Value} \\
\midrule
            6h &  0m &              -1.1 \\
           19h & 15m &              26.3  \\
\bottomrule
\end{tabular}

\bigskip

\begin{tabular}{@{}rr@{\qquad} r @{}}
\toprule
    \mc{2}{@{}l}{Time} & \mc{1}{c@{}}{Value} \\
\midrule
            6h &  0m &              -1.1 \\
           19h & 15m &              26.3  \\
\bottomrule
\end{tabular}
\end{table}


\end{document}
2
  • The alignment in the first one with the d column is what I'm going for. However, this document is being auto-generated. I won't know the width of the columns at compile time. I.e. I wouldn't know to use 2 in the definition \newcolumntype{d}{D..{2.1}}.
    – hhquark
    Nov 3, 2016 at 14:31
  • @hhquark use -1 then Nov 3, 2016 at 14:43

You must log in to answer this question.

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