1

I would like to align some text that is common to multiple rows in a column in a way similar to aligning decimal points in a column using dcolumn. I have not figured out how to use dcolumn for text (it expects math?). \llap improved the look, but didn't quite align how I wanted. I also tried tabbing within the table, but couldn't get past an error (it wanted an \item).

\documentclass{article}
\usepackage{tabularx,tabu,arraycols,multirow,booktabs}
\usepackage{textcomp}
\usepackage{dcolumn}
\begin{document}
\begin{table}
\centering
\caption{Table.}
\begin{tabular}{c D{,}{\pm}{-1} c c}
\toprule
\textbf{some text} & \multicolumn{1}{c}{\textbf{more text}} & \textbf{other text} & \textbf{\% of something} \\
\midrule
\multirow{5}{*}{x to y} & 170 , 2.5 & $\sim$no wake & \multirow{5}{*}{<29\%} \\
& 175 , 2.5 & $\sim$half wake & \\
& 180 , 2.5 & full wake & \\
& 185 , 2.5 & $\sim$half wake & \\
& 190 , 2.5 & $\sim$no wake & \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

my table

I don't know why I get the upside down !, but that doesn't happen in my real file. I would like to have the third column generally centered horizontally while also aligned on the w's.

2
  • 1
    Welcome to TeX.SE. You're getting an upside-down exclamation mark because the character < in <29\% cannot be rendered correctly in text mode if the default font encoding (known as OT1) is in use. Two possible, non-exclusive remedies: (a) change the font encoding to T1 by inserting \usepackage[T1]{fontenc` in the preamble; (b) switch to math mode, i.e., change <29\% to $<29\%$.
    – Mico
    Commented May 18, 2020 at 15:57
  • Why not use an r type column for column 3?
    – leandriis
    Commented May 18, 2020 at 17:39

2 Answers 2

1

Two version sith either a single r type column or an r and an l type column:

enter image description here

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tabularx,arraycols,multirow,booktabs}
\usepackage{textcomp}
\usepackage{dcolumn}
\begin{document}
\begin{table}
\centering
\caption{Table.}
\begin{tabular}{c D{,}{\pm}{-1} r c}
\toprule
\textbf{some text} & \multicolumn{1}{c}{\textbf{more text}} & \textbf{other text} & \textbf{\% of something} \\
\midrule
\multirow{5}{*}{x to y} & 170 , 2.5 & $\sim$no wake & \multirow{5}{*}{<29\%} \\
& 175 , 2.5 & $\sim$half wake & \\
& 180 , 2.5 & full wake & \\
& 185 , 2.5 & $\sim$half wake & \\
& 190 , 2.5 & $\sim$no wake & \\
\bottomrule
\end{tabular}
\end{table}

\begin{table}
\centering
\caption{Table.}
\begin{tabular}{c D{,}{\pm}{-1} r@{~}l c}
\toprule
\textbf{some text} & \multicolumn{1}{c}{\textbf{more text}} & \multicolumn{2}{c}{\textbf{other text}} & \textbf{\% of something} \\
\midrule
\multirow{5}{*}{x to y} & 170 , 2.5 & $\sim$no &wake & \multirow{5}{*}{<29\%} \\
& 175 , 2.5 & $\sim$half& wake & \\
& 180 , 2.5 & full& wake & \\
& 185 , 2.5 & $\sim$half& wake & \\
& 190 , 2.5 & $\sim$no& wake & \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tabularx,arraycols,multirow,booktabs}
\usepackage{textcomp}
\usepackage{dcolumn}
\usepackage{calc}

\begin{document}
\begin{table}
\centering
\newlength{\mywidth}
\setlength{\mywidth}{\widthof{a longer column header}}
\caption{Table.}
\begin{tabular}{c D{,}{\pm}{-1} Wr{0.5\mywidth}@{~} Wl{0.5\mywidth} c}
\toprule
\textbf{some text} & \multicolumn{1}{c}{\textbf{more text}} & \multicolumn{2}{c}{\textbf{a longer column header}} & \textbf{\% of something} \\
\midrule
\multirow{5}{*}{x to y} & 170 , 2.5 & $\sim$no &wake & \multirow{5}{*}{<29\%} \\
& 175 , 2.5 & $\sim$half& wake & \\
& 180 , 2.5 & full& wake & \\
& 185 , 2.5 & $\sim$half& wake & \\
& 190 , 2.5 & $\sim$no& wake & \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

enter image description here

6
  • Thank you. I think the second one looks more how I'm hoping. Can you explain what the command r@{~}l is doing? Also, am I right that this is effectively the same as the one below from F. Pantigny? Thanks.
    – Dan
    Commented May 18, 2020 at 18:27
  • r@{~}l is a right aligned and a left aligned column with a space in the middle.
    – leandriis
    Commented May 18, 2020 at 18:31
  • Actually, now that I see it in my actual file, which has a slightly longer heading above column three, I like that everything is aligned at the beginning of "wake", but I don't like that it is not centered below the heading. Is there a way to do both???
    – Dan
    Commented May 18, 2020 at 18:43
  • I'm not sure. Your edited version looks more right justified to me. Could you show it again with a longer heading to verify that the column text is both aligned at the beginning of "wake" and centered under the heading?
    – Dan
    Commented May 18, 2020 at 18:53
  • @Dan: Please see the update for a version that also works with a longer column header. The \widthof command is from the calc package; the W column type from the array package.
    – leandriis
    Commented May 18, 2020 at 19:02
0

Here is a workaround by creating 2 columns:

\documentclass{article}
\usepackage{tabularx,arraycols,multirow,booktabs}
\usepackage{textcomp}
\usepackage{dcolumn}
\begin{document}
\begin{table}
\centering
\caption{Table.}
\begin{tabular}{c D{,}{\pm}{-1} r @{\space} l c }
\toprule
\textbf{some text} & \multicolumn{1}{c}{\textbf{more text}} & \multicolumn{2}{c}{\textbf{other text}} & \textbf{\% of something} \\ 
\midrule
\multirow{5}{*}{x to y} & 170 , 2.5 & $\sim$no & wake & \multirow{5}{*}{$<29\%$} \\
& 175 , 2.5 & $\sim$half&wake & \\
& 180 , 2.5 & full&wake & \\
& 185 , 2.5 & $\sim$half&wake & \\
& 190 , 2.5 & $\sim$no&wake & \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

Result of the above code

You must log in to answer this question.

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