7

I'm very new to LaTeX, and I'm currently waist deep in my first project using it.

I'm trying to right align some text in cells in a table. I am using booktabs, not sure if that changes anything (I'm still trying to figure out how packages work, to be honest).

Here is the output

Here is my code:

\begin{table}[H]
  \begin{center}
    \caption{A comparison of statistics for forest and trailside ash saplings}
    \begin{tabular}{lll}
    \toprule 
    Statistic & Forest & Trailside \\ \midrule
    \rowcolor[gray]{.9} Sample size (trees)    & 243     & 257 \\
    Mean (mm)  & 133.19 & 139.39 \\ 
    \rowcolor[gray]{.9} Median (mm)            & 93      & 100 \\ 
    Standard deviation (mm) & 110.36  & 117.96 \\ 
    \rowcolor[gray]{.9} Standard error (mm)    & 7.08   & 7.36   \\
    95\% Confidence intervals (mm) &  ~ & ~ \\
    \makebox[2.4cm][r] Upper bound & 147.07 & 153.81 \\
    \makebox[2.4cm][r] Lower bound & 119.31 & 124.96 \\
    \bottomrule
    \end{tabular}
  \end{center}
\end{table}

As you can see, I am currently using \makebox[2.4][r] to hack it in, but if there's a more elegant way to accomplish this I'd love to know it.

  • 1
    Welcome at tex.sx! booktabs is a great package, but indeed doesn't meddle with cell alignment. I also changed align to horizontal-alignment :-). – lockstep Sep 26 '11 at 21:22
  • @lockstep is there anything I need to do to put this as resolved or something like that (I clicked the arrow next to the best answer already)? Thanks for the help! – Zak Sep 26 '11 at 21:34
  • You've done all you should -- upvoted answers you viewed as helpful, and "accepted" (that's the checkmark) the most helpful one. – lockstep Sep 26 '11 at 21:43
4

Your table needs very little additional tweaking to achieve your objectives, I believe. In the MWE below, I use the siunitx package (and its column type "S") to align the numbers on the decimal point, as well as the \multicolumn command. Happy TeXing!

\documentclass{article}
\usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{siunitx}
\begin{document}
\begin{table}
\centering
\caption{A comparison of statistics for forest and trailside ash saplings}
\smallskip
\sisetup{table-format = 3.2}
\begin{tabular}{lSS}
\toprule 
Statistic & \multicolumn{1}{l}{Forest} & \multicolumn{1}{l}{Trailside} \\ 
\midrule
\rowcolor[gray]{.9} 
Sample size (trees) & 243 & 257\\
Mean (mm)           & 133.19 & 139.39 \\ 
\rowcolor[gray]{.9} Median (mm)  & 93  & 100 \\ 
Standard deviation (mm)          & 110.36  & 117.96 \\ 
\rowcolor[gray]{.9} Standard error (mm)  & 7.08 & 7.36   \\
95\% Confidence intervals (mm) &  ~ & ~ \\
\multicolumn{1}{r}{Upper bound} & 147.07 & 153.81 \\
\multicolumn{1}{r}{Lower bound} & 119.31 & 124.96 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

enter image description here

  • Well I'd like to use this for the decimal point stuff, but I get a ton of errors as soon as I add \usepackage{siunitx}... – Zak Sep 26 '11 at 21:45
  • 1
    @Zak: The siunitx options have somewhat changed from version 1 to 2. Try to upgrade your TeX distribution or replace table-format with tabformat. EDIT: See tex.stackexchange.com/questions/2746/… – lockstep Sep 26 '11 at 22:03
  • @lockstep Output, code As you can see, the "Forest" and "Trailside" headers are getting messed up. How do I exempt them from "S"? – Zak Sep 26 '11 at 22:26
  • Disregard that, that was fixed in the example up above by using \multicolumn. – Zak Sep 26 '11 at 22:35
7

Using \multicolumn you can override the column specification for a particular cell:

\documentclass{article}
\usepackage{booktabs}
\usepackage[table]{xcolor}

\begin{document}

\begin{table}
  \centering
    \caption{A comparison of statistics for forest and trailside ash saplings}
    \begin{tabular}{lll}
    \toprule 
    Statistic & Forest & Trailside \\ \midrule
        \rowcolor[gray]{.9} Sample size (trees)       & 243 & 257\\
        Mean (mm)               & 133.19 & 139.39 \\ 
        \rowcolor[gray]{.9} Median (mm)   & 93   & 100\\ 
        Standard deviation (mm) & 110.36  & 117.96 \\ 
        \rowcolor[gray]{.9} Standard error (mm)    & 7.08   & 7.36\\
    95\% Confidence intervals (mm) &  ~ & ~ \\
    \multicolumn{1}{r}{Upper bound} & 147.07 & 153.81 \\
    \multicolumn{1}{r}{Lower bound} & 119.31 & 124.96 \\
    \bottomrule
    \end{tabular}
\end{table}

\end{document}

enter image description here

I would also suggest you to use \centering instead of the center environment to avoid adding extra vertical spacing.

5

You can use

\multicolumn{1}{r}{Upper bound}

I suggest you to look at the siunitx package that provides an S column type for correctly output numerical data in tables.

  • egreg -- you were thinking along the same lines I did with regard to using the siunitx package (I posted before I saw your answer). :-) – Mico Sep 26 '11 at 21:43
  • @Mico Of course your answer is much more informative than mine. – egreg Sep 26 '11 at 22:52

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.