Graphics, Figures & TablesMultiple Alignment Points within a Table Column

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

Multiple Alignment Points within a Table Column

Post by svend_tveskaeg »

Hi all.

Consider the following MWE:

Code: Select all

\documentclass[12pt,a4paper,danish]{article}
\usepackage{babel}
\usepackage[hmargin=2.4cm]{geometry}
\usepackage{booktabs,dcolumn}

\newcommand*{\mc}[1]{\multicolumn{1}{c}{#1}}

\begin{document}

\begin{table}[htbp]
\def\mlr{\hspace{5.89pt}}
 \centering
  \label{tbl:3}
  \begin{tabular}{l l l l}
   \toprule
    \mc{Samlet placering} & \mc{Placering i sv{\o}mning} & \mc{Placering i cykling} & \mc{Placering i l{\o}b}      \\
   \midrule
     \mlr $1$.~Anders     & \mlr $1$.~Dennis             & \mlr $1$.~Anders         & \mlr $1$.~Anders   ($32.30$) \\
     \mlr $2$.~Brian      & \mlr $2$.~Brian              & \mlr $2$.~Frede          & \mlr $2$.~Gorm     ($34.08$) \\
     \mlr $3$.~Frede      & \mlr $3$.~Frederik           & \mlr $3$.~Brian          & \mlr $3$.~Brian    ($34.28$) \\
     \mlr $4$.~Dennis     & \mlr $4$.~Emil               & \mlr $4$.~Emil           & \mlr $4$.~Frede    ($35.19$) \\
     \mlr $5$.~Emil       & \mlr $5$.~Henrik             & \mlr $5$.~Dennis         & \mlr $5$.~Frederik ($35.20$) \\
     \mlr $6$.~Frederik   & \mlr $6$.~Frede              & \mlr $6$.~Ivar           & \mlr $6$.~Dennis   ($35.39$) \\
     \mlr $7$.~Gorm       & \mlr $7$.~Anders             & \mlr $7$.~Henrik         & \mlr $7$.~Jens     ($35.40$) \\
     \mlr $8$.~Henrik     & \mlr $8$.~Gorm               & \mlr $8$.~Frederik       & \mlr $8$.~Emil     ($35.41$) \\
     \mlr $9$.~Ivar       & \mlr $9$.~Ivar               & \mlr $9$.~Jens           & \mlr $9$.~Henrik   ($35.56$) \\
         $10$.~Jens       &     $10$.~Jens               &     $10$.~Gorm           &     $10$.~Ivar     ($36.32$) \\
   \bottomrule
  \end{tabular}
\end{table}

\end{document}
Issues:
  1. Is it possible to align each column at the full stop (e.g., just before or after the end of "1.") in order to avoid the manual aligning?
  2. Can I align the last column both as mentioned in 1. and align the numbers in the parentheses at the same time?
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)

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

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Multiple Alignment Points within a Table Column

Post by localghost »

The short answer is to use more columns. The long answer is to use left and right aligned pairs of columns with a specific column separator to get the desired alignment. The number alignment in the last column can be done by the siunitx package. In form of code it looks like this.

Code: Select all

\documentclass[12pt,a4paper,danish]{article}
\usepackage[T1]{fontenc}
\usepackage{selinput}
\SelectInputMappings{
  ae={æ},
  oslash={ø},
  Euro={€}
}
\usepackage{babel}
\usepackage[margin=2.4cm]{geometry}
\usepackage[tableposition=top]{caption}
\usepackage{booktabs}
\usepackage{siunitx}

\newcommand*{\mcii}[1]{\multicolumn{2}{c}{#1}}
\newcommand*{\mciii}[1]{\multicolumn{3}{c}{#1}}

\begin{document}
  \begin{table}[!htb]
    \caption{Table caption}
    \label{tbl:3}
    \centering
    \begin{tabular}{
      r@{.\enspace}l  % right and left aligned pair of columns
      r@{.\enspace}l  % right and left aligned pair of columns
      r@{.\enspace}l  % right and left aligned pair of columns
      r@{.\enspace}l  % right and left aligned pair of columns
      S[table-format=3.3,input-symbols={(,)}] % number alignement specifications
    }\toprule
      \mcii{Samlet placering} & \mcii{Placering i svømning} & \mcii{Placering i cykling} & \mciii{Placering i løb} \\ \midrule
       1 & Anders   &  1 & Dennis   &  1 & Anders   &  1 & Anders   & (32.30) \\
       2 & Brian    &  2 & Brian    &  2 & Frede    &  2 & Gorm     & (34.08) \\
       3 & Frede    &  3 & Frederik &  3 & Brian    &  3 & Brian    & (34.28) \\
       4 & Dennis   &  4 & Emil     &  4 & Emil     &  4 & Frede    & (35.19) \\
       5 & Emil     &  5 & Henrik   &  5 & Dennis   &  5 & Frederik & (35.20) \\
       6 & Frederik &  6 & Frede    &  6 & Ivar     &  6 & Dennis   & (35.39) \\
       7 & Gorm     &  7 & Anders   &  7 & Henrik   &  7 & Jens     & (35.40) \\
       8 & Henrik   &  8 & Gorm     &  8 & Frederik &  8 & Emil     & (35.41) \\
       9 & Ivar     &  9 & Ivar     &  9 & Jens     &  9 & Henrik   & (35.56) \\
      10 & Jens     & 10 & Jens     & 10 & Gorm     & 10 & Ivar     & (36.32) \\ \bottomrule
    \end{tabular}
  \end{table}
\end{document}

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

Multiple Alignment Points within a Table Column

Post by svend_tveskaeg »

Perfect!

Thank you for the other hints/corrections, too.

Update
There is actually one small thing: In the last column, I would like the output to be on the form "(xx.yy)" instead of "(xx,yy)". Therefore I tried changing input-symbols={(,)} to input-symbols={(.)} but it gives me the same output.

If someone can either point to the right place in the sinuitx manual or tell me what to do here, I will much appreciate it.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Multiple Alignment Points within a Table Column

Post by localghost »

svend_tveskaeg wrote:[…] There is actually one small thing: In the last column, I would like the output to be on the form "(xx.yy)" instead of "(xx,yy)". Therefore I tried changing input-symbols={(,)} to input-symbols={(.)} but it gives me the same output. […]
Oh, yes. I accidently left out an important detail. For the desired output add these lines to the preamble.

Code: Select all

\sisetup{
  output-decimal-marker={.}
}
Alternatively you can check if one of the predefined localisations matches your demands (see Section 5.16).
svend_tveskaeg wrote:[…] If someone can either point to the right place in the sinuitx manual or tell me what to do here, I will much appreciate it.
Presumably Section 5 is a good starting point. Especially Section 5.6 in your case here.
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: Multiple Alignment Points within a Table Column

Post by svend_tveskaeg »

I will definitely read section 5 tonight.

Thank you very much, Thorsten!
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
Post Reply