Graphics, Figures & Tablessiunitx | Alignments in Table

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

siunitx | Alignments in Table

Post by svend_tveskaeg »

Hi all.

Consider the following MWE:

Code: Select all

\documentclass{article}
\usepackage[locale=DE]{siunitx}
\usepackage{booktabs}

\begin{document}

\begin{table}
\centering
 \begin{tabular}{
   S[table-figures-integer=1,table-figures-decimal=1]
   S[table-figures-integer=2,table-figures-decimal=3]
   @{\,}
   s[table-unit-alignment=left]
   S[table-figures-integer=2,table-figures-decimal=3]
   @{\,}
   s[table-unit-alignment=left]
  }
  \toprule
   \multicolumn{1}{c}{Something random~A} &
   \multicolumn{2}{c}{Something random~B} &
   \multicolumn{2}{c}{Something random~C}  \\
  \midrule
   6.5 &  2.35  & \percent &  2.35  & \percent \\
   7   & 34.234 & \percent & 34.234 & \percent \\
   7.5 &  3.762 & \percent &  3.762 & \percent \\
  \bottomrule
 \end{tabular}
\end{table}

\end{document}
I have a few problems with formatting of this table:

(1) The first column: I would like to vertically align ``7'' in the second row of numbers with the 6 in ``6,5'' and the 7 in ``7,5''.
(2) The second and third column: I would like to horisontically center the numbers and units relative to ``Something random B'' and ``Something random C'', resp.

How do I do this?

Thank you in advance!

P.S. I have tried to look at the siunitx documentation, but without coming up with any solutions.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

siunitx | Alignments in Table

Post by svend_tveskaeg »

First: I have also posted this question on TeX Stackexchange, here. I have not gotten any help with my modified question, though.

Here is the new code:

Code: Select all

\documentclass{article}
\usepackage[locale=DE]{siunitx}
\usepackage{booktabs}

\begin{document}

\begin{table}
\centering
 \caption{S{\o}vnm{\ae}ngde blandt elever i niende klasse.}
 \label{tbl:2}
 \begin{tabular}{
   S[table-format = 1.1]
   S[table-format = 2.0]
   S[table-format = 2.0]<{{\,\si{\percent}}}
   S[table-format = 2.0]
   S[table-format = 2.0]<{{\,\si{\percent}}}
  }
  \toprule
   {Antal timer} &
   \multicolumn{2}{c}{Andel af eleverne fra 9.~A} &
   \multicolumn{2}{c}{Andel af eleverne fra 9.~B}   \\
  \midrule
       & {Abs.} & \multicolumn{1}{c}{Rel.} & {Abs.} & \multicolumn{1}{c}{Rel.} \\
  \midrule
   6.5 &  1     &   4    &   0    &   0 \cr
   7   &  4     &  16    &   2    &  10 \cr
   7.5 & 99     &  12    &   3    &  15 \cr
  \bottomrule
 \end{tabular}
\end{table}

\end{document}
The columns with ''Abs.'' numbers and ''Rel.'' numbers+units are not centered properly under ''Andel af eleverne fra 9. A'' and ''Andel af eleverne fra 9. B'', resp.

Also, the numbers and numbers+units seem not to be directly under ''Abs.'' and ''Rel.'', resp.

How do I fix this?

Thank you in advance!
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

siunitx | Alignments in Table

Post by cgnieder »

You might want to specify table-space-text-post (see section 5.14 of the siunitx manual). Also your table-format doesn't match the numbers your actually using.

Is this what you want:

Code: Select all

\documentclass{article}
\usepackage[locale=DE]{siunitx}
\usepackage{booktabs}

\begin{document}

\begin{table}
\centering
 \caption{S{\o}vnm{\ae}ngde blandt elever i niende klasse.}
 \label{tbl:2}
 \begin{tabular}{
   S[table-format = 1.1]
   S[table-format = 2]
   S[table-format = 2,table-space-text-post={\,\si{\percent}}]<{{\,\si{\percent}}}
   S[table-format = 1]
   S[table-format = 2,table-space-text-post={\,\si{\percent}}]<{{\,\si{\percent}}}
  }
  \toprule
   {Antal timer} &
   \multicolumn{2}{c}{Andel af eleverne fra 9.~A} &
   \multicolumn{2}{c}{Andel af eleverne fra 9.~B}   \\
  \midrule
   & {Abs.} & {Rel.} & {Abs.} & {Rel.} \cr
  \midrule
   6.5 &  1 &   4&   0&   0 \cr
   7   &  4 &  16&   2&  10 \cr
   7.5 & 99 &  12&   3&  15 \cr
  \bottomrule
 \end{tabular}
\end{table}

\end{document}
Regards
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

siunitx | Alignments in Table

Post by svend_tveskaeg »

The first of my problems are now solved (thanks!).

There is another problem, though: The left edge of "Andel af eleverne ..." is vertically aligned with the left edge of "Abs." while the "Rel. %" is 'indented' relative to the right egde of "Andel af eleverne ...". I would like to have the same (non-zero) indentation form both sides.

P.S. I have again modified the code (this time just a little bit):

Code: Select all

\documentclass{article}
\usepackage[locale=DE]{siunitx}
\usepackage{booktabs}

\begin{document}

\begin{table}
\centering
 \caption{S{\o}vnm{\ae}ngde blandt elever i niende klasse.}
 \label{tbl:1}
 \begin{tabular}{
   S[table-format = 1.1]
   S[table-format = 2]
   S[table-format = 3,table-space-text-post={\,\si{\percent}}]<{{\,\si{\percent}}}
   S[table-format = 2]
   S[table-format = 3,table-space-text-post={\,\si{\percent}}]<{{\,\si{\percent}}}
  }
  \toprule
   {Antal timer} &
   \multicolumn{2}{c}{Andel af eleverne fra 9.~A} &
   \multicolumn{2}{c}{Andel af eleverne fra 9.~B}   \\
  \midrule
   & {Abs.} & \multicolumn{1}{c}{Rel.} & {Abs.} & \multicolumn{1}{c}{Rel.} \cr
  \midrule
   6.5 &  1 &   4 &  0 &   0 \cr
   7   &  4 &  16 &  2 &  10 \cr
   7.5 &  3 &  12 &  3 &  15 \cr
  \midrule
       & 25 & 100 & 20 & 100 \cr
  \bottomrule
 \end{tabular}
\end{table}

\end{document}
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

siunitx | Alignments in Table

Post by cgnieder »

Ok, the following is a hack...

Code: Select all

\documentclass{article}
\usepackage[locale=DE]{siunitx}
\usepackage{booktabs}

\begin{document}

\begin{table}[ht]
 \def\prc{\,\si{\percent}}
 \def\spc{\hspace{2em}}
 \centering
 \caption{S{\o}vnm{\ae}ngde blandt elever i niende klasse.}
 \label{tbl:1}
 \begin{tabular}{
   S[table-format = 1.1]
   >{\spc}S[table-format = 2,,table-space-text-pre={\spc}]
   S[table-format = 3,table-space-text-post={\prc}]
   >{\spc}S[table-format = 2,,table-space-text-pre={\spc}]
   S[table-format = 3,table-space-text-post={\prc}]
  }
  \toprule
   {Antal timer} &
   \multicolumn{2}{c}{Andel af eleverne fra 9.~A} &
   \multicolumn{2}{c}{Andel af eleverne fra 9.~B} \\
  \midrule
       & {Abs.} &   {Rel.} & {Abs.} & {Rel.} \\
  \midrule
   6.5 &      1 &   4 \prc &      0 &   0 \prc \\
   7   &      4 &  16 \prc &      2 &  10 \prc \\
   7.5 &      3 &  12 \prc &      3 &  15 \prc \\
  \midrule
       &     25 & 100 \prc &     20 & 100 \prc \\
  \bottomrule
 \end{tabular}
\end{table}

\end{document}
Regards
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: siunitx | Alignments in Table

Post by svend_tveskaeg »

Thank you a lot!

Any chance this can be 'automated'?

I would probably like to use this construction another time, but with some other text than "Andel af eleverne ...", which would make the spacing incorrect.

P.S. I will mark this thread as solved after an eventual answer.
Last edited by svend_tveskaeg on Fri Sep 21, 2012 4:09 pm, edited 1 time in total.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

siunitx | Alignments in Table

Post by cgnieder »

svend_tveskaeg wrote:Any chance this can be 'automated'?
I'm not sure... One of the reasons I put

Code: Select all

\def\spc{\hspace{2em}}
in there was to minimize the effort when adjust the spacing. 2em was only a guess, anyway. Told you it was a hack ;)

Regards
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: siunitx | Alignments in Table

Post by svend_tveskaeg »

Thank you for the explanation and for helping me in general!
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
Post Reply