LaTeX forum ⇒ Graphics, Figures & Tablessiunitx | Alignments in Table Topic is solved

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

siunitx | Alignments in Table

Postby svend_tveskaeg » Thu Sep 20, 2012 3:16 am

Hi all.

Consider the following MWE:

  1. \documentclass{article}
  2. \usepackage[locale=DE]{siunitx}
  3. \usepackage{booktabs}
  4.  
  5. \begin{document}
  6.  
  7. \begin{table}
  8. \centering
  9. \begin{tabular}{
  10. S[table-figures-integer=1,table-figures-decimal=1]
  11. S[table-figures-integer=2,table-figures-decimal=3]
  12. @{\,}
  13. s[table-unit-alignment=left]
  14. S[table-figures-integer=2,table-figures-decimal=3]
  15. @{\,}
  16. s[table-unit-alignment=left]
  17. }
  18. \toprule
  19. \multicolumn{1}{c}{Something random~A} &
  20. \multicolumn{2}{c}{Something random~B} &
  21. \multicolumn{2}{c}{Something random~C} \\
  22. \midrule
  23. 6.5 & 2.35 & \percent & 2.35 & \percent \\
  24. 7 & 34.234 & \percent & 34.234 & \percent \\
  25. 7.5 & 3.762 & \percent & 3.762 & \percent \\
  26. \bottomrule
  27. \end{tabular}
  28. \end{table}
  29.  
  30. \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)

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

Postby svend_tveskaeg » Thu Sep 20, 2012 5:46 pm

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:

  1. \documentclass{article}
  2. \usepackage[locale=DE]{siunitx}
  3. \usepackage{booktabs}
  4.  
  5. \begin{document}
  6.  
  7. \begin{table}
  8. \centering
  9. \caption{S{\o}vnm{\ae}ngde blandt elever i niende klasse.}
  10. \label{tbl:2}
  11. \begin{tabular}{
  12. S[table-format = 1.1]
  13. S[table-format = 2.0]
  14. S[table-format = 2.0]<{{\,\si{\percent}}}
  15. S[table-format = 2.0]
  16. S[table-format = 2.0]<{{\,\si{\percent}}}
  17. }
  18. \toprule
  19. {Antal timer} &
  20. \multicolumn{2}{c}{Andel af eleverne fra 9.~A} &
  21. \multicolumn{2}{c}{Andel af eleverne fra 9.~B} \\
  22. \midrule
  23. & {Abs.} & \multicolumn{1}{c}{Rel.} & {Abs.} & \multicolumn{1}{c}{Rel.} \\
  24. \midrule
  25. 6.5 & 1 & 4 & 0 & 0 \cr
  26. 7 & 4 & 16 & 2 & 10 \cr
  27. 7.5 & 99 & 12 & 3 & 15 \cr
  28. \bottomrule
  29. \end{tabular}
  30. \end{table}
  31.  
  32. \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: 1988
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Thu Sep 20, 2012 6:26 pm

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:
  1. \documentclass{article}
  2. \usepackage[locale=DE]{siunitx}
  3. \usepackage{booktabs}
  4.  
  5. \begin{document}
  6.  
  7. \begin{table}
  8. \centering
  9. \caption{S{\o}vnm{\ae}ngde blandt elever i niende klasse.}
  10. \label{tbl:2}
  11. \begin{tabular}{
  12. S[table-format = 1.1]
  13. S[table-format = 2]
  14. S[table-format = 2,table-space-text-post={\,\si{\percent}}]<{{\,\si{\percent}}}
  15. S[table-format = 1]
  16. S[table-format = 2,table-space-text-post={\,\si{\percent}}]<{{\,\si{\percent}}}
  17. }
  18. \toprule
  19. {Antal timer} &
  20. \multicolumn{2}{c}{Andel af eleverne fra 9.~A} &
  21. \multicolumn{2}{c}{Andel af eleverne fra 9.~B} \\
  22. \midrule
  23. & {Abs.} & {Rel.} & {Abs.} & {Rel.} \cr
  24. \midrule
  25. 6.5 & 1 & 4& 0& 0 \cr
  26. 7 & 4 & 16& 2& 10 \cr
  27. 7.5 & 99 & 12& 3& 15 \cr
  28. \bottomrule
  29. \end{tabular}
  30. \end{table}
  31.  
  32. \end{document}


Regards
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

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

Postby svend_tveskaeg » Thu Sep 20, 2012 6:43 pm

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):

  1. \documentclass{article}
  2. \usepackage[locale=DE]{siunitx}
  3. \usepackage{booktabs}
  4.  
  5. \begin{document}
  6.  
  7. \begin{table}
  8. \centering
  9. \caption{S{\o}vnm{\ae}ngde blandt elever i niende klasse.}
  10. \label{tbl:1}
  11. \begin{tabular}{
  12. S[table-format = 1.1]
  13. S[table-format = 2]
  14. S[table-format = 3,table-space-text-post={\,\si{\percent}}]<{{\,\si{\percent}}}
  15. S[table-format = 2]
  16. S[table-format = 3,table-space-text-post={\,\si{\percent}}]<{{\,\si{\percent}}}
  17. }
  18. \toprule
  19. {Antal timer} &
  20. \multicolumn{2}{c}{Andel af eleverne fra 9.~A} &
  21. \multicolumn{2}{c}{Andel af eleverne fra 9.~B} \\
  22. \midrule
  23. & {Abs.} & \multicolumn{1}{c}{Rel.} & {Abs.} & \multicolumn{1}{c}{Rel.} \cr
  24. \midrule
  25. 6.5 & 1 & 4 & 0 & 0 \cr
  26. 7 & 4 & 16 & 2 & 10 \cr
  27. 7.5 & 3 & 12 & 3 & 15 \cr
  28. \midrule
  29. & 25 & 100 & 20 & 100 \cr
  30. \bottomrule
  31. \end{tabular}
  32. \end{table}
  33.  
  34. \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: 1988
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Thu Sep 20, 2012 7:54 pm

Ok, the following is a hack...

  1. \documentclass{article}
  2. \usepackage[locale=DE]{siunitx}
  3. \usepackage{booktabs}
  4.  
  5. \begin{document}
  6.  
  7. \begin{table}[ht]
  8. \def\prc{\,\si{\percent}}
  9. \def\spc{\hspace{2em}}
  10. \centering
  11. \caption{S{\o}vnm{\ae}ngde blandt elever i niende klasse.}
  12. \label{tbl:1}
  13. \begin{tabular}{
  14. S[table-format = 1.1]
  15. >{\spc}S[table-format = 2,,table-space-text-pre={\spc}]
  16. S[table-format = 3,table-space-text-post={\prc}]
  17. >{\spc}S[table-format = 2,,table-space-text-pre={\spc}]
  18. S[table-format = 3,table-space-text-post={\prc}]
  19. }
  20. \toprule
  21. {Antal timer} &
  22. \multicolumn{2}{c}{Andel af eleverne fra 9.~A} &
  23. \multicolumn{2}{c}{Andel af eleverne fra 9.~B} \\
  24. \midrule
  25. & {Abs.} & {Rel.} & {Abs.} & {Rel.} \\
  26. \midrule
  27. 6.5 & 1 & 4 \prc & 0 & 0 \prc \\
  28. 7 & 4 & 16 \prc & 2 & 10 \prc \\
  29. 7.5 & 3 & 12 \prc & 3 & 15 \prc \\
  30. \midrule
  31. & 25 & 100 \prc & 20 & 100 \prc \\
  32. \bottomrule
  33. \end{tabular}
  34. \end{table}
  35.  
  36. \end{document}


Regards
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

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

Postby svend_tveskaeg » Thu Sep 20, 2012 8:06 pm

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: 1988
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Thu Sep 20, 2012 8:12 pm

svend_tveskaeg wrote:Any chance this can be 'automated'?

I'm not sure... One of the reasons I put
  1. \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
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

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

Postby svend_tveskaeg » Thu Sep 20, 2012 8:16 pm

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)


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 8 guests