TeX - LaTeX Asked on October 22, 2020
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:
I have several problems with regards to formatting:
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.
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.
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}
Correct answer by leandriis on October 22, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP