TransWikia.com

How to correctly align the uncertainty symbols in this table, and control horizontal column alignment?

TeX - LaTeX Asked on December 17, 2021

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.

One Answer

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}{normalsizebfseries}

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}

Answered by leandriis on December 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP