Graphics, Figures & Tablesmultirow, siunitx | Decimal Alignment

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Brynolf
Posts: 5
Joined: Tue Apr 17, 2012 4:06 pm

multirow, siunitx | Decimal Alignment

Post by Brynolf »

I'm using booktabs and siunitx to create a table. I want to use multirow and also to align on decimals using the siunitx package's column type S as below:

Code: Select all

\documentclass[11pt,a4paper]{article}
\usepackage{booktabs}
\usepackage{multirow} 
\usepackage{siunitx}

\begin{document}
  \begin{table}[ht]
    \centering
    \begin{tabular}{@{}SSl@{}}\toprule
      675.7 & 6.8 & A \\
      1543.2 & 33.1 &B \\
      \multirow{2}{*}{8.6} & 4.5 & C \\
      & 5.5 & D \\ \bottomrule
    \end{tabular}
  \end{table}
\end{document}
However, the multirow part is not aligned properly. Does anyone know how to solve this?
Last edited by localghost on Fri Jan 18, 2013 5:28 pm, edited 2 times in total.

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

multirow, siunitx | Decimal Alignment

Post by cgnieder »

I believe you're out of luck here. \multirow sets its contents in a \parbox and there is no way I can see that siunitx can access and do its magic with it.


Regards
site moderator & package author
Brynolf
Posts: 5
Joined: Tue Apr 17, 2012 4:06 pm

Re: multirow, siunitx | Decimal Alignment

Post by Brynolf »

OK. =(

Do you see another way to get the desired result?
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

multirow, siunitx | Decimal Alignment

Post by cgnieder »

One could use another tabular inside \multirow and make sure to use the same table format for it as the column it is placed in has:

Code: Select all

\documentclass[11pt,a4paper]{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{siunitx}

\newcommand*\hacketyhack[2][]{%
\begin{tabular}{@{}S[#1]@{}}
 #2
\end{tabular}}

\begin{document}

\begin{table}[ht]
 \centering
 \begin{tabular}{@{}S[table-format=4.1]S[table-format=2.1]l@{}}
  \toprule
    675.7 &  6.8 & A \\
   1543.2 & 33.1 & B \\
   \multirow{2}{*}{\hacketyhack[table-format=4.1]{8.6}}
          &  4.5 & C \\
          &  5.5 & D \\
  \bottomrule
 \end{tabular}
\end{table}

\end{document}
You should probably think of a better name than \hacketyhack but I couldn't think of something useful in the moment :(

Regards
site moderator & package author
Brynolf
Posts: 5
Joined: Tue Apr 17, 2012 4:06 pm

multirow, siunitx | Decimal Alignment

Post by Brynolf »

Hi and thank you for the solution, it sort of works :) If I have titles on my columns this seems to mess with the table format. HOWEVER, this goes away if I include the xcolor package. Does anyone know what's going on? Anyway, with xcolor it works so I am happy! Thank you!

Code: Select all

\documentclass[a4paper,oneside,11pt]{article}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{multirow} 

\usepackage[table]{xcolor}

\newcommand*\hacketyhack[2][]{%
\begin{tabular}{@{}S[#1]@{}}
#2
\end{tabular}}

\begin{document}

\begin{table}[ht]
	\centering
	\begin{tabular}{@{}S[table-format=4.1]S[table-format=4.1]l@{}}
		\toprule
		{Some title} & {Some other title} & Title three\\ \midrule
		675.7&6.8 & A\\
		1543.2&33.1&B\\
		\multirow{2}{*}{\hacketyhack[table-format=4.1]{8.6}} & 4.5 & C\\
 		& 5.5 & D\\
		\bottomrule
	\end{tabular}
\end{table}

\end{document}
If you comment out \usepackage[table]{xcolor} the table is no good.
Last edited by Stefan Kottwitz on Fri Jan 25, 2013 11:23 am, edited 1 time in total.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

multirow, siunitx | Decimal Alignment

Post by cgnieder »

I can't find anything messed up, neither with nor without »xcolor«. However, anything like titles in S columns needs to be enclosed with braces. But since you've done that I assume you already know that...

Regards
site moderator & package author
Brynolf
Posts: 5
Joined: Tue Apr 17, 2012 4:06 pm

Re: multirow, siunitx | Decimal Alignment

Post by Brynolf »

That's strange. If I comment out that package in the mwe in writelatex I see something happening to the one number in the double row. Anyway, it's not broken anymore so no need to fix it. :)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

multirow, siunitx | Decimal Alignment

Post by cgnieder »

Brynolf wrote:That's strange. If I comment out that package in the mwe in writelatex I see something happening to the one number in the double row.
Indeed... I didn't test it on writelatex at first but on my own machine where I didn't see anything. (I also didn't expect any different output.) Since you say there's nothing to fix I won't investigate. :)

Regards
site moderator & package author
Post Reply