Search code examples
latextabular

Right margin in table cells aligned to the right in Latex


I have the following table in Latexenter image description here

Created with this code:

\begin{table}[h]
\caption{aaaa}
\label{tab:treatments}
\centering
\scalebox{0.8}{
\begin{tabular}{l c c c}
\toprule

\tabhead{Por 1} & \tabhead{Por 3}  & \tabhead{Quantificazione log-dim} & \tabhead{cintin\'a}  \\
\midrule
A & M & 99.3\%& 5.4\\
B& A &  2.0\%& 4.6\\
C & N & 5.8\% & 4.6\\
D & N & 3.5\% & 4.26\\
E & K & 22.5\% & 3.7\\
\bottomrule\\

\end{tabular}
}
\end{table}

I would like to have the third column of percentages aligned to the right At the same time the column label is too large. If I aligned the column to the right it would and up visually weird.

In a Word table I would align the column to the right and than add a right margin to the cells. The numbers would move toward the cell center while keeping their alignment to the right.

How do I do that in Latex? How do I add a margin to the right?


Solution

  • There are a number of options you can try:

    • Original:

      enter image description here

    • Multi-row header:

      enter image description here

    • Manual alignment of centred columns using \phantoms:

      enter image description here

    • Manual alignment of centred columns using \eqmakeboxes (from eqparbox):

      enter image description here

    • Automated alignment of centred columns using siunitx:

      enter image description here

    \documentclass{article}
    
    \usepackage{booktabs,graphicx,makecell,siunitx,eqparbox}
    \newcommand{\tabhead}{\textbf}
    
    \begin{document}
    
    Original:
    
    \begin{tabular}{ l c c c }
      \toprule
      \tabhead{Por 1} & \tabhead{Por 3}  & \tabhead{Quantificazione log-dim} & \tabhead{cintin\'a}  \\
      \midrule
      A & M & 99.3\% & 5.4  \\
      B & A &  2.0\% & 4.6  \\
      C & N &  5.8\% & 4.6  \\
      D & N &  3.5\% & 4.26 \\
      E & K & 22.5\% & 3.7  \\
      \bottomrule
    \end{tabular}
    
    \bigskip
    
    Multi-row header:
    
    \begin{tabular}{ l c c c }
      \toprule
      \tabhead{Por 1} & \tabhead{Por 3}  & \tabhead{\makecell[b]{Quantificazione \\ log-dim}} & \tabhead{cintin\'a}  \\
      \midrule
      A & M & 99.3\% & 5.4  \\
      B & A &  2.0\% & 4.6  \\
      C & N &  5.8\% & 4.6  \\
      D & N &  3.5\% & 4.26 \\
      E & K & 22.5\% & 3.7  \\
      \bottomrule
    \end{tabular}
    
    \bigskip
    
    Manual alignment of centred columns using \verb|\phantom|s:
    
    \begin{tabular}{ l c c c }
      \toprule
      \tabhead{Por 1} & \tabhead{Por 3}  & \tabhead{\makecell[b]{Quantificazione \\ log-dim}} & \tabhead{cintin\'a}  \\
      \midrule
      A & M & 99.3\% & 5.4\phantom{0}  \\
      B & A & \phantom{0}2.0\% & 4.6\phantom{0}  \\
      C & N & \phantom{0}5.8\% & 4.6\phantom{0}  \\
      D & N & \phantom{0}3.5\% & 4.26 \\
      E & K & 22.5\% & 3.7\phantom{0}  \\
      \bottomrule
    \end{tabular}
    
    \bigskip
    
    Manual alignment of centred columns using \verb|\eqmakebox|es:
    
    \begin{tabular}{ l c c c }
      \toprule
      \tabhead{Por 1} & \tabhead{Por 3}  & \tabhead{\makecell[b]{Quantificazione \\ log-dim}} & \tabhead{cintin\'a}  \\
      \midrule
      A & M & \eqmakebox[log][r]{99.3\%} & \eqmakebox[cint][l]{5.4}  \\
      B & A & \eqmakebox[log][r]{ 2.0\%} & \eqmakebox[cint][l]{4.6}  \\
      C & N & \eqmakebox[log][r]{ 5.8\%} & \eqmakebox[cint][l]{4.6}  \\
      D & N & \eqmakebox[log][r]{ 3.5\%} & \eqmakebox[cint][l]{4.26} \\
      E & K & \eqmakebox[log][r]{22.5\%} & \eqmakebox[cint][l]{3.7}  \\
      \bottomrule
    \end{tabular}
    
    \newpage
    
    Automated alignment of centred columns using \verb|siunitx|:
    
    \begin{tabular}{ l c S[table-format = 2.1, table-space-text-post = \%] S[table-format = 1.2] }
      \toprule
      \tabhead{Por 1} & \tabhead{Por 3}  & \tabhead{\makecell[b]{Quantificazione \\ log-dim}} & \tabhead{cintin\'a}  \\
      \midrule
      A & M & 99.3\% & 5.4  \\
      B & A &  2.0\% & 4.6  \\
      C & N &  5.8\% & 4.6  \\
      D & N &  3.5\% & 4.26 \\
      E & K & 22.5\% & 3.7  \\
      \bottomrule
    \end{tabular}
    
    \end{document}