Information and discussion about graphics, figures & tables in LaTeX documents.
LavaTyper
Posts: 69 Joined: Sat Feb 11, 2012 2:38 am
Post
by LavaTyper » Wed Feb 05, 2014 6:07 pm
Greetings,
I am in the process of writing my dissertation, and I need to have special formatting of numerical data within tables.
I need numerical entries in the second column to be aligned to the right-most digit. But I still need to have all of the numbers centered.
I need numerical entries in the third column to be aligned with a decimal. But I still need to have all of the numbers centered.
I need the titles in the first row of the table to be centered as well.
How do I modify this example?
Code: Select all
\documentclass[12pt]{article}
\usepackage{array}
\begin{document}
\begin{table}[!htb]
\centering
\begin{tabular}{rcc}
\textbf{Col1} & \textbf{Sample Size} & \textbf{Raw Data} \\ \hline
Name 1 & 87 & 7689 \\
Name 2 & 102 & 325 \\
Name 3 & 93 & 109 \\
Name 4 & 111 & 91.3 \\
Name 5 & 55 & 67.9 \\
Name 6 & 76 & 5.43 \\
Name 7 & 82 & 1.83 \\
Name 8 & 92 & .178 \\ \hline
\end{tabular}
\end{table}
\end{document}
How do I align numerical data in a table to the last digit and/or to the decimal? I couldn't find a script that met all of these requirements.
Learn LaTeX easily with newest books:
The LaTeX Beginner's Guide : 2nd edition and perfect for students writing a thesis
The LaTeX Cookbook : 2nd edition full of practical examples for mathematics, physics, chemistry, and more
LaTeX Graphics with TikZ : the first book about TikZ for perfect drawings in your LaTeX thesis
hugovdberg
Posts: 133 Joined: Sat Feb 25, 2012 6:12 pm
Post
by hugovdberg » Wed Feb 05, 2014 6:59 pm
Alignment of the numbers can be done with the siunitx package, which provides a S column type for numbers, I believe that does exactly what you want for points 1 and 2.
A multicolumn over a single column can be used to center a single cell.
Attached pdf was created with this file:
Code: Select all
\documentclass[12pt]{article}
\usepackage{array}
\usepackage{siunitx}
\begin{document}
\begin{table}[!htb]
\centering
\begin{tabular}{rSS}
\multicolumn{1}{c}{\textbf{Col1}} & \textbf{Sample Size} & \textbf{Raw Data} \\ \hline
Name 1 & 87 & 7689 \\
Name 2 & 102 & 325 \\
Name 3 & 93 & 109 \\
Name 4 & 111 & 91.3 \\
Name 5 & 55 & 67.9 \\
Name 6 & 76 & 5.43 \\
Name 7 & 82 & 1.83 \\
Name 8 & 92 & .178 \\ \hline
\end{tabular}
\end{table}
\end{document}
Attachments
test.pdf
(27.3 KiB) Downloaded 2100 times
Ubuntu 13.10 + Tex Live 2013 + Texmaker / Windows 7 Pro + MikTex 2.9 + TexnicCenter / Android 4.3 + TexPortal + DroidEdit
LavaTyper
Posts: 69 Joined: Sat Feb 11, 2012 2:38 am
Post
by LavaTyper » Wed Feb 05, 2014 8:40 pm
Alright, but then I can't use $...$ in my table entries to write something like $1.83 \pm 0.23$. If I take out siunitx and change the tabular arguments from S to c, it works again. But then I lose the alignment. So, how can I get both to work at the same time?
Also:
If I have a big number (see revised column 2), it nudges the whole column over. How can I get the whole column to be centered again?
Code: Select all
\documentclass[12pt]{article}
\usepackage{array}
\usepackage{siunitx}
\begin{document}
\begin{table}[!htb]
\centering
\begin{tabular}{rSSc}
\multicolumn{1}{c}{\textbf{Col1}} & \textbf{Sample Size} & \textbf{Raw Data} & \textbf{Range} \\
\hline
Name 1 & 87999 & 7689 & $3.0 \pm 1.0$ \\
Name 2 & 102 & 325 & $3.0 \pm 1.2$ \\
Name 3 & 93 & 109 & $5.3 \pm 1.1$ \\
Name 4 & 111 & 91.3 & $6.2 \pm 2.2$ \\
Name 5 & 55 & 67.9 & $-0.9 \pm 0.8$ \\
Name 6 & 76 & 5.43 & $-3.0 \pm 1.0$ \\
Name 7 & 82 & 1.83 & $-2.0 \pm 0.6$ \\
Name 8 & 92 & .178 & $3.0 \pm 1.3$ \\
\hline
\end{tabular}
\end{table}
\end{document}
hugovdberg
Posts: 133 Joined: Sat Feb 25, 2012 6:12 pm
Post
by hugovdberg » Wed Feb 05, 2014 9:06 pm
That third column is actually centered on the decimal point, I'm not sure what you want it to be centered on, but you might want to take a look at the siunitx manual, section 5.14 provides a whole list of options for tabular material.
If you use siunitx and you want to format $1.83 \pm 0.23$
you can also use \num{1.83\pm0.23}
in normal text, and in a S column inside a table you actually don't need the $...$
or \num{...}
since siunitx automatically parses all numbers, so basically everything that's allowed inside \num{}
is allowed in the table.
Edit: when you use siunitx to typeset numbers with uncertainty check the manual, by default 1.83\pm0.23
becomes 1.83(0.23), but it is all configurable to display it any way you want is.
Ubuntu 13.10 + Tex Live 2013 + Texmaker / Windows 7 Pro + MikTex 2.9 + TexnicCenter / Android 4.3 + TexPortal + DroidEdit
LavaTyper
Posts: 69 Joined: Sat Feb 11, 2012 2:38 am
Post
by LavaTyper » Wed Feb 05, 2014 10:37 pm
Interesting...
Alright, I want this:
Screenshot1.jpg (32.9 KiB) Viewed 52289 times
to look like this:
Screenshot2.jpg (30.44 KiB) Viewed 52289 times
I'm really close... but now I have an extra space between
\pm
and the number to the left in the last column.
Code: Select all
\documentclass[12pt]{article}
\usepackage{array}
\usepackage{siunitx}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{
r
S[table-number-alignment = right]
S[table-number-alignment = center]
S[
table-number-alignment = right,
separate-uncertainty = true,
table-figures-uncertainty = 1
]
}
\multicolumn{1}{c}{\textbf{Col1}} & \textbf{Sample Size} & \textbf{Raw Data} & \multicolumn{1}{c}{\textbf{Range}} \\
\hline
Name 1 & 87999 & 7689 & 3.0 \pm 1.0 \\
Name 2 & 102 & 325 & 3.0 \pm 1.2 \\
Name 3 & 93 & 109 & 5.3 \pm 1.1 \\
Name 4 & 111 & 91.3 & 6.2 \pm 2.2 \\
Name 5 & 55 & 67.9 & -0.9 \pm 0.8 \\
Name 6 & 76 & 5.43 & -3.0 \pm 1.0 \\
Name 7 & 82 & 1.83 & -2.0 \pm 0.6 \\
Name 8 & 92 & .178 & 3.0 \pm 1.3 \\
Name 9 & 76 & .103 & 4.2 \pm 1.2 \\
Name 10 & 76 & .055 & -1.1 \pm 0.2 \\
\hline
\end{tabular}
\end{table}
\end{document}
Here is what I get:
Screenshot3.jpg (37.08 KiB) Viewed 52289 times
hugovdberg
Posts: 133 Joined: Sat Feb 25, 2012 6:12 pm
Post
by hugovdberg » Wed Feb 05, 2014 11:27 pm
Apparently siunitx by default reserves 2 spaces for decimals of which you use only one. If your entire table will contain only numbers with one decimal you can add the option
table-figures-decimal=1
.
Code: Select all
\documentclass[12pt]{article}
\usepackage{array}
\usepackage{siunitx}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{
r
S[table-number-alignment = right]
S[table-number-alignment = center]
S[
table-number-alignment = right,
separate-uncertainty = true,
table-figures-uncertainty = 1,
table-figures-decimal = 1
]
}
\multicolumn{1}{c}{\textbf{Col1}} & \textbf{Sample Size} & \textbf{Raw Data} & \multicolumn{1}{c}{\textbf{Range}} \\
\hline
Name 1 & 87999 & 7689 & 3.0 \pm 1 \\
Name 2 & 102 & 325 & 3.0 \pm 1 \\
Name 3 & 93 & 109 & 5.3 \pm 1.1 \\
Name 4 & 111 & 91.3 & 6.2 \pm 2.2 \\
Name 5 & 55 & 67.9 & -0.9 \pm 0.8 \\
Name 6 & 76 & 5.43 & -3.0 \pm 1.0 \\
Name 7 & 82 & 1.83 & -2.0 \pm 0.6 \\
Name 8 & 92 & .178 & 3.0 \pm 1.3 \\
Name 9 & 76 & .103 & 4.2 \pm 1.2 \\
Name 10 & 76 & .055 & -1.1 \pm 0.2 \\
\hline
\end{tabular}
\end{table}
\end{document}
Ubuntu 13.10 + Tex Live 2013 + Texmaker / Windows 7 Pro + MikTex 2.9 + TexnicCenter / Android 4.3 + TexPortal + DroidEdit
LavaTyper
Posts: 69 Joined: Sat Feb 11, 2012 2:38 am
Post
by LavaTyper » Thu Feb 06, 2014 1:55 am
This is great, I think this should do and I'll take it from here.
LavaTyper
Posts: 69 Joined: Sat Feb 11, 2012 2:38 am
Post
by LavaTyper » Thu Feb 06, 2014 6:28 pm
Alright, help, why can't I get the values in the second column to be exactly centered? I keep specifying table-alignment center but it's off to the right a little.
Code: Select all
\documentclass[12pt]{article}
\usepackage{array}
\usepackage{siunitx}
\begin{document}
\begin{table}[h]
\centering
\addtolength{\tabcolsep}{0.1cm}
\begin{tabular}{c S[
table-number-alignment = center,
separate-uncertainty = true,
table-figures-uncertainty = 1,
table-figures-decimal = 2,
table-figures-integer = 1
]}
\textbf{DATA} & \textbf{MMRS} \\
\hline
A & 4.32 \pm 0.94 \\
B & 6.82 \pm 0.34 \\
C & 8.25 \pm 0.63 \\
D & 3.55 \pm 0.43 \\
E & 7.39 \pm 0.33 \\
F & 8.11 \pm 0.49 \\
\hline
\end{tabular}
\end{table}
\end{document}
LavaTyper
Posts: 69 Joined: Sat Feb 11, 2012 2:38 am
Post
by LavaTyper » Mon Feb 10, 2014 12:29 am
So, any suggestions?
My project has several hundred tables and I need help on this soon.
hugovdberg
Posts: 133 Joined: Sat Feb 25, 2012 6:12 pm
Post
by hugovdberg » Mon Feb 10, 2014 1:27 am
Actually, the numbers are perfectly aligned in your example, it is the title that's not quite centered. To fix this you should enclose the title in
{}
, even when you already group the title in the
\textbf{}
. Adding the brackets prevents siunitx from parsing the contents at all, and it will apply the correct alignment to the group as it is. I'm not sure where the title is aligned at without them, but it gets the job done
Code: Select all
\documentclass[12pt]{article}
\usepackage{array}
\usepackage{siunitx}
\begin{document}
\begin{table}[h]
\centering
\addtolength{\tabcolsep}{0.1cm}
\begin{tabular}{c S[
table-number-alignment = center,
separate-uncertainty = true,
table-figures-uncertainty = 1,
table-figures-decimal = 2,
table-figures-integer = 1
]}
\textbf{DATA} & {\textbf{MMRS}} \\
\hline
A & 4.32 \pm 0.94 \\
B & 6.82 \pm 0.34 \\
C & 8.25 \pm 0.63 \\
D & 3.55 \pm 0.43 \\
E & 7.39 \pm 0.33 \\
F & 8.11 \pm 0.49 \\
\hline
\end{tabular}
\end{table}
\end{document}
Ubuntu 13.10 + Tex Live 2013 + Texmaker / Windows 7 Pro + MikTex 2.9 + TexnicCenter / Android 4.3 + TexPortal + DroidEdit