2

I am trying to create a table with uncertainties using the following code:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{table}[!ht]
  \begin{center}
    \label{tab:table1}
    \begin{tabular}{l|r|r}
      \textbf{First Column Name} & \textbf{Second Column Name} & \textbf{Third Column Name} \\
                                 & Sub Name 2                  & Sub Name 3                 \\
      %
      \hline
      Row 1                   &   1.00\, $\pm$\, 0.02  &  -                       \\
      Row 2                   &   1.55\, $\pm$\, 0.05  &  50\%    \,$\pm$\, 5\%   \\
      \hspace{2mm}Sub Row 3   &   1.20\, $\pm$\, 0.15  &  -12.2\% \,$\pm$\; 1.2\% \\
      Row 4                   &   10.0\, $\pm$\, 1.1   &  -                       \\
      Row 5                   &   -                    &  -85\%   \,$\pm$\, 2\%   \\
      \hspace{2mm}Sub Row 6   &   2.6 \, $\pm$\, 0.9   &  -20.0\% \,$\pm$\, 0.1\% \\
    \end{tabular}
  \end{center}
  \caption{My caption text.}
\end{table}

\end{document}

which produces this table:

enter image description here

I have several problems with regards to formatting:

  1. How can the alignment of numbers within a column be controlled? I would like the ± symbols to all be aligned within a column.
  2. Is there a way that the column titles ("Second Column Name" and "Sub Name 2") can be centered, whilst leaving the numbers in that column right-aligned?
  3. What code would I use to make "Second Column Name" wrap onto a new line (still keeping "Sub Name 2" on a separate line though). At the moment, the column is much wider than it needs to be.

I have tried following several answers (for example here) which recommend the siunitx package, but within each column I do not always have the same number of decimal places in every row, and was struggling to make it work. Is that package really the best way to go here, or are there simple tweaks that can be done to this example to make it work? Thanks.

2

With the help of the siunitx package you can align the numbers with respect to teh decimal separator as well as with respect to the \pm. Column headers and other text are automatically centered with respect to the numbers, as long as you surround them with a pair of {}. For the column headers, I used the \thead command from the makecell package. Inside of \thead you can manually add a linebreak wherever you like.

To avoid repetition of the %, I have placed it inside of the corresponding column header.

Addiaiontally, I have removed the 9pt option as this is not a valid option with the article class. (Available options are: 10pt, 11pt, 12pt). I have also replaced the center environment with the \centering command as the former adds white space that might be undesired. Lastly, I have corrected the placement of the \label command.

enter image description here

\documentclass{article} % 9pt is not a  valid class option of the article class
\usepackage{amsmath}

\usepackage{siunitx}
\sisetup{input-uncertainty-signs=\pm, 
         separate-uncertainty=true}
        
\usepackage{makecell}
\renewcommand{\theadfont}{\normalsize\bfseries}

\begin{document}

\begin{table}[!ht]
\centering % replaced the center environment with centering to avoid additional vertical white space
    \begin{tabular}{l|S[table-format=2.2(3)]|S[table-format=-2.2(4)]}
      \thead{First Column Name}  & {\thead{Second Column\\ Name}} & {\thead{Third Column Name}} \\
                                 & {Sub Name 2}                   & {Sub Name 3 in \%}                \\
      %
      \hline
      Row 1                   &   1.00 \pm  0.02  &  {-}           \\
      Row 2                   &   1.55 \pm  0.05  &  50    \pm 5   \\
      \hspace{2mm}Sub Row 3   &   1.20 \pm  0.15  &  -12.2 \pm 1.2 \\
      Row 4                   &   10.0 \pm  1.1   &  {-}           \\
      Row 5                   &   {-}             &  -85   \pm 2   \\
      \hspace{2mm}Sub Row 6   &   2.6  \pm  0.9   &  -20.0 \pm 0.1 \\
    \end{tabular}
  \caption{My caption text.}
      \label{tab:table1} % label must be placed after caption!
\end{table}

\end{document}
2
  • Excellent, thanks. There is lot's of new information that I have learned from your answer. The 9pt was a mistake (I was using the extarticle class, and the 9pt was a hangover from creating a MWE). One problem I still have is the space between 10.0 and the ± symbol in the first column (and similar with the 50 and 85 in the third column). I understand that the reason is because you align the decimal points, but it just looks wrong, and I think the amount of space either side of the ± needs to be equal. How can I fix that? Thanks for your assistance.
    – teeeeee
    Jul 23 '20 at 19:38
  • I have edited my answer to remove the [9pt] flag, just so if someone else tries to run it directly it will work for them. You can remove any reference to it from your answer. Cheers.
    – teeeeee
    Jul 23 '20 at 19:41

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.