4

Many of you may feel the need to make tables look beautiful, as do I. However, I am having trouble with formatting. Is there a good way to format column headings across multiple columns?

First, as you can see I am have numbers with varying precision, and I wanted them to line up along the $\pm$ symbol. So I used that as the column boundary and wrapped the two numbers around: r@{$\pm$}l. This made the table headings a nightmare. As you can see in the attached code I have used various iterations of multirow and multicolumn however without great success.

Is there a better way to format the table headings across what is really two columns? Or, perhaps there is a better way to join the two numbers along the $\pm$ symbol such that I can avoid using two columns?

In addition, there is an extra $\pm$ symbol underneath the central column heading. I don't understand how this appears for this column and not the others -- how can I get rid of this?

I have attached my attempt and as you can see the woeful result.

Result

enter image description here

Implementation

    \documentclass[a4paper,12pt]{article}
    \usepackage{multirow}
    \begin{document}


    \begin{table}
    \centering\renewcommand\arraystretch{1.1}
    %\begin{tabular}{l|cr@{ $\pm$ }lc}
    \begin{tabular}{l|cr@{ $\pm$ }lr@{ $\pm$ }lr@{ $\pm$ }lc}
    \hline

    \multirow{2}{*}{\begin{minipage}[t]{0.08\textwidth}{ \bf Region \\type}  \end{minipage}}
                     &  \multirow{2}{*}{\begin{minipage}[t]{0.12\textwidth}\begin{center}\textbf{ Observed events}\end{center}\end{minipage}}
                     & \multicolumn{2}{c}{ \multirow{2}{*}{\begin{minipage}[t]{0.12\textwidth}\begin{center}{ \bf Fitted BG \\Slime} \end{center}\end{minipage}}}
                     & \multicolumn{2}{c}{ \multirow{2}{*}{\begin{minipage}[t]{0.12\textwidth}\begin{center}{ \bf Fitted BG \\Integral} \end{center}\end{minipage}}}
                     & \multicolumn{2}{c}{ \multirow{2}{*}{\begin{minipage}[t]{0.12\textwidth}\begin{center}{ \bf Fitted BG \\Angle} \end{center}\end{minipage}}}
                     & \multirow{2}{*}{\begin{minipage}[t]{0.10\textwidth}\begin{center}{ \bf Pre-fit \\prediction}\end{center}\end{minipage}}\\
    && \multicolumn{2}{c}{}&\\

    \hline
    {alpha-1} & 453 & $196.4$&$7.3$ & $84$&$33$  & $183$&$27$ & $378$   \\
    {alpha-2} & 34 & $16.7$&$2.6$ & $98$&$84$  & $159$& $23$ & $447$   \\
    {alpha-3} & 43 & $33.8$&$4.3$ & $67$&$43$  & $67$& $13$ & $10$   \\
    \hline
    {beta-1} & 19 & $7.4$&$1.6$ & $7.8$&$1.3$  & $22.1$& $3.6$ & $22$   \\
    {beta-2} & 2 & $5.61$&$0.60$ & $2.99$&$0.62$  & $7.60$& $0.86$ & $33$   \\
    {bets-3} & 3 & $5.42$&$0.62$ & $4.44$&$0.49$  & $8.86$& $0.79$ & $422$   \\
    \hline
    \end{tabular}
    \caption{a caption}
    \end{table}

    \end{document}
  • I am not answering your question but you might find nice tables in the csquotes package user guide. – pluton Dec 11 '15 at 1:35
4

I'd like to suggest that you use (a) the S column type (provided by the siunitx package) to format the numeric columns and (b) the tabularx package to set up the overall structure of the tabular material and to enable line wrapping of long material in some of the header cells. The header might also become more easily parsable if it were organized a bit more thoroughly; the code below provides a specific suggestion.

enter image description here

\documentclass[a4paper,12pt]{article}
\usepackage{tabularx,booktabs,ragged2e,siunitx}
\newcolumntype{L}{>{\RaggedRight\arraybackslash}X}
\newcolumntype{C}{>{\Centering\arraybackslash}X}
\newcommand{\mC}[1]{\multicolumn{1}{@{}C@{}}{#1}}
\begin{document}

\begin{table}
\sisetup{separate-uncertainty}
\renewcommand\arraystretch{1.1}
\setlength\tabcolsep{4pt} % default value: 6pt
\begin{tabularx}{\textwidth}{@{} 
           L 
           S[table-format=3.0]
           S[table-format=3.2(4)]
           S[table-format=2.2(3)]
           S[table-format=3.2(3)]
           S[table-format=3.0]
           @{}}
\toprule
Region type 
& \mC{Observed events} 
& \multicolumn{3}{c}{Fitted BG}
& \mC{Pre-fit prediction} \\
\cmidrule(lr){3-5}
& & {Slime} & {Integral} & {Angle} \\
\midrule
alpha-1 & 453 & 196.4\pm7.3 & 84\pm33 & 183\pm27 & 378 \\
alpha-2 &  34 &  16.7\pm2.6 & 98\pm84 & 159\pm23 & 447 \\
alpha-3 &  43 &  33.8\pm4.3 & 67\pm43 &  67\pm13 & 10  \\
\addlinespace
beta-1 & 19 & 7.4 \pm1.6  &  7.8\pm1.3  & 22.1\pm 3.6 & 22 \\
beta-2 &  2 & 5.61\pm0.60 & 2.99\pm0.62 & 7.60\pm0.86 & 33 \\
beta-3 &  3 & 5.42\pm0.62 & 4.44\pm0.49 & 8.86\pm0.79 & 422 \\
\bottomrule
\end{tabularx}
\caption{A caption}
\end{table}

\end{document} 
2

There did not appear to be any row spanning entries so I removed all the \multirow/minipage markup. The table then basically worked but is too wide for the page, I made headings smaller here but you may prefer to shorten them or split over three lines.

enter image description here

 \documentclass[a4paper,12pt]{article}
\usepackage{array}
    \begin{document}


    \begin{table}
    \centering

X\dotfill X

\bigskip

\renewcommand\arraystretch{1.1}
\setlength\tabcolsep{5pt}
\newcommand\hd[2]{\multicolumn{#1}{c}{%
    \small\bfseries\begin{tabular}{@{}c@{}}#2\end{tabular}}}

    \begin{tabular}{@{}>{\small\bfseries}lcr@{${}\pm{}$}lr@{${}\pm{}$}lr@{${}\pm{}$}lc@{}}
    \hline
\hd{1}{Region \\type}& 
\hd{1}{Observed\\events}&
\hd{2}{Fitted BG \\Slime}&
\hd{2}{Fitted BG \\Integral}&
\hd{2}{Fitted BG \\Angle}&
\hd{1}{Pre-fit \\prediction}\\

    \hline
    alpha-1 & 453 & $196.4$&$7.3$ & $84$&$33$  & $183$&$27$ & $378$   \\
    alpha-2 & 34 & $16.7$&$2.6$ & $98$&$84$  & $159$& $23$ & $447$   \\
    alpha-3 & 43 & $33.8$&$4.3$ & $67$&$43$  & $67$& $13$ & $10$   \\
    \hline
    beta-1 & 19 & $7.4$&$1.6$ & $7.8$&$1.3$  & $22.1$& $3.6$ & $22$   \\
    beta-2 & 2 & $5.61$&$0.60$ & $2.99$&$0.62$  & $7.60$& $0.86$ & $33$   \\
    bets-3 & 3 & $5.42$&$0.62$ & $4.44$&$0.49$  & $8.86$& $0.79$ & $422$   \\
    \hline
    \end{tabular}
    \caption{a caption}
    \end{table}

    \end{document}

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.