Graphics, Figures & TablesEntries of large Table not aligned with Columns

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
NELLLY
Posts: 113
Joined: Thu Nov 26, 2009 2:21 am

Entries of large Table not aligned with Columns

Post by NELLLY »

Hi,

I have a large table, previously I posted on the forum about how to get a large table such that it doesn't exceed the width of the page. Inspired from the answer I used the following instructions.

Code: Select all

\documentclass{article}
\usepackage{booktabs}
\usepackage{array}
\usepackage{siunitx}
\usepackage{lipsum}  % dummy text

\begin{document}
  \lipsum[1]

  \begin{table}[ht]
    \makebox[\textwidth][c]{\footnotesize
      \begin{tabular}{@{}>{$}r<{$}@{}*{14}{S[table-format=-3]}@{}}
        \toprule
        &\multicolumn{6}{c}{Optimal design parameters}
        &\multicolumn{6}{c}{Statistical performance}\\
        \cmidrule(lr){2-7}\cmidrule(lr){8-13}       
        Run&{$n$}&{$WL$}&{$UCL$}&{$h_1$}&{$h_2$}&{$ETCU_s^*$}
        &{$ANSSC$}&{$ANOC$}&{$ANFA$}&{$ANSS$}&{$ANOS$}&{$ATS$}
        &{$\triangle C_{Fp}$}&{$\triangle C_{VSS}$}\\
        \midrule
        $1$    &14&0&2&5.58&0.01&1.49&275.01&3850.18&2.11&3.21&44.50&4.98&13.87\%&3.25\%\\\bottomrule[1.2pt]
      \end{tabular}
    }
    \caption{Caption}
  \end{table}
\end{document}
In the output, the numbers are not aligned with the column labels. What should I do?

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

Entries of large Table not aligned with Columns

Post by localghost »

You have to give the digits format explicitly for every S column type. These specifications are based on the table row you posted and might need to be corrected if digits with other formats occur later in a column.

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{caption}
\usepackage{array,booktabs,multirow}
\usepackage{mathtools}
\usepackage{siunitx}
\usepackage{lipsum}  % dummy text

\begin{document}
  \lipsum[1]

  \begin{table}[ht]
    \caption{Caption}
    \label{tab:dummy}
    \makebox[\textwidth][c]{%
      \footnotesize%
      \begin{tabular}{
        r
        S[table-format=2.0]
        S[table-format=1.0]
        S[table-format=1.0]
        S[table-format=1.2]
        S[table-format=1.2]
        S[table-format=1.2]
        S[table-format=3.2]
        S[table-format=4.2]
        S[table-format=1.2]
        S[table-format=1.2]
        S[table-format=2.2]
        S[table-format=1.2]
        S[table-format=2.2]
        S[table-format=1.2]
      }\toprule
        \multirow{2}{*}[-0.75ex]{Run} & \multicolumn{6}{c}{Optimal design parameters} & \multicolumn{6}{c}{Statistical performance} & {$\Delta C_\text{Fp}$} & {$\Delta C_\text{VSS}$} \\ \cmidrule(lr){2-7}\cmidrule(lr){8-13}      
        & {$n$} & {WL} & {UCL} & {$h_1$} & {$h_2$} & {$\text{ETCU}_s^*$}  & {ANSSC} & {ANOC} & {ANFA} & {ANSS} & {ANOS} & {ATS} & {(\%)} & {(\%)} \\ \midrule
        1 & 14 & 0 & 2 & 5.58 & 0.01 & 1.49 & 275.01 & 3850.18 & 2.11 & 3.21 & 44.50 & 4.98 & 13.87 & 3.25 \\ \bottomrule
      \end{tabular}%
    }
  \end{table}
\end{document}

Remarks:
  • Tables have their captions above.

Thorsten
NELLLY
Posts: 113
Joined: Thu Nov 26, 2009 2:21 am

Entries of large Table not aligned with Columns

Post by NELLLY »

Many thanks Localghost. Your solution works perfectly.I adopted it also for my second table but this time it exceeds a little bit the width of the page

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{caption}
\usepackage{array,booktabs,multirow}
\usepackage{mathtools}
\usepackage{siunitx}
\usepackage{lipsum}  % dummy text

\begin{document}
  \lipsum[1]
\begin{table}[ht]
  \caption{Name}
    \label{tab:dumm3}
    \makebox[\textwidth][c]{%
      \footnotesize% 
      \begin{tabular}{
        c
        S[table-format=2.0]
        S[table-format=2.0]
        S[table-format=1.0]
        S[table-format=1.0]
        S[table-format=1.0]
        S[table-format=1.2]
        S[table-format=3.2]
        S[table-format=4.2]
        S[table-format=1.2]
        S[table-format=1.2]
        S[table-format=2.2]
        S[table-format=1.2]
        S[table-format=2.2]
        S[table-format=1.2]
        S[table-format=1.2]
      }\toprule
 
\multirow{2}{*}[-0.75ex]{Run}& \multicolumn{9}{c}{Optimal design parameters} & \multicolumn{6}{c}{Statistical performance}   \\ \cmidrule(lr){2-10}\cmidrule(lr){11-16}      


&{$n_1$}&{$n_2$}&{$UWL_1$}&{$UCL_1$}&{$UWL_2$}&{$UCL_2$}&{$h$}&{$ETCU_s^*$}& {$\Delta C_\text{Fp}$(\%)}&{$ANSSC$}&{$ANOC$}&{$ANFA$}&{$ANSS$}&{$ANOS$}&{$ATS$}\\
\midrule
1&2&24&0&1&0.51&3.98&0.99&1.54&10.98&1009.60&4483.95&1.40&5.75&58.08&5.20\\
2&4&13&0&1&0.17&1.98&1.74&5.64&3.42&191.07&1024.39&2.41&3.69&27.21&5.55\\
$3$&51&77&3&5&4.12&6.57&5.22&4.35&0.68&63.36&3366.84&0.28&1.28&73.06&4.08\\
$4$&3&20&0&1&0.37&2.95&3.76&3.28&3.24&265.46&1443.87&1.38&4.15&39.38&13.73\\
$5$&12&16&0&1&0.06&1.22&6.12&5.01&0.59&53.97&722.26&3.22&1.50&20.77&6.11\\
$6$&33&51&2&3&2.79&4.03&7.9&1.94&0.51&126.08&4374.62&2.11&1.22&44.29&5.68\\
$7$&2&14&0&1&0.26&2.91&1.69&2.74&14.64&591.22&1786.80&0.86&6.08&39.73&9.44\\
$8$&1&20&0&1&0.47&4.94&0.25&3.46&13.5&1332.83&2688.19&0.02&11.70&103.97&2.80\\
$9$&13&16&0&1&0.08&1.19&11.4&3.40&0.29&87.22&1272.78&14.28&1.59&23.46&12.40\\
$10$&49&68&3&5&4.05&6.40&5.95&4.61&0.43&55.52&2982.56&2.16&1.32&73.97&4.88\\
$11$&2&30&0&1&1.11&4.99&1.65&6.77&8.51&201.52&1284.36&0.90&6.03&74.84&9.13\\
$12$&1&48&0&1&2.05&8.98&0.60&2.26&14.07&1666.17&8081.64&0.07&11.94&223.15&6.87\\
$13$&2&28&0&1&1.02&4.77&0.68&3.75&8.09&489.70&2821.91&1.87&5.17&60.88&3.17\\
$14$&3&9&0&1&1.90&1.92&2.69&2.85&3.06&489.70&2821.91&1.87&5.17&60.88&3.17\\
$15$&66&111&6&8&9.05&11.64&11.08&2.18&0.91&89.75&6154.86&0.51&1.27&94.65&8.53\\
$16$&2&29&0&1&1.07&4.87&1.83&7.43&2.36&181.65&1113.08&0.75&4.96&59.12&8.16\\\bottomrule
      \end{tabular}
    }
  \end{table}
\end{document}
What should I add? Also I need the values to be centered. Thanks
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Entries of large Table not aligned with Columns

Post by localghost »

NELLLY wrote:[…] I adopted it also for my second table but this time it exceeds a little bit the width of the page. […]
It's not the table that exceeds the page, it's the content of certain cells that causes the warnings. You didn't work thoroughly when adjusting table-format for the S columns.

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{caption}
\usepackage{array,booktabs,multirow}
\usepackage{mathtools}
\usepackage{siunitx}
\usepackage{lipsum}  % dummy text

\begin{document}
  \lipsum[1]
  \begin{table}[!ht]
    \caption{Table caption}
    \label{tab:dummy}
    \makebox[\textwidth][c]{%
      \footnotesize%
      \begin{tabular}{
        S[table-format=2.0]
        S[table-format=2.0]
        S[table-format=3.0]
        S[table-format=1.0]
        S[table-format=1.0]
        S[table-format=1.2]
        S[table-format=2.2]
        S[table-format=2.2]
        S[table-format=1.2]
        S[table-format=2.2]
        S[table-format=4.2]
        S[table-format=4.2]
        S[table-format=2.2]
        S[table-format=2.2]
        S[table-format=3.2]
        S[table-format=2.2]
      }\toprule
        {\multirow{2}{*}[-0.75ex]{Run}} & \multicolumn{9}{c}{Optimal design parameters} & \multicolumn{6}{c}{Statistical performance}   \\ \cmidrule(lr){2-10}\cmidrule(lr){11-16}      
           & {$n_1$} & {$n_2$} & {$\text{UWL}_1$} & {$\text{UCL}_1$} & {$\text{UWL}_2$} & {$\text{UCL}_2$} & {$h$} & {$\text{ETCU}_s^*$} & {$\Delta C_\text{Fp}$(\%)} & {ANSSC} & {ANOC} & {ANFA} & {ANSS} & {ANOS} & {ATS} \\ \midrule
         1 &  2 &  24 & 0 & 1 & 0.51 &  3.98 &  0.99 & 1.54 & 10.98 & 1009.60 & 4483.95 &  1.40 &  5.75 &  58.08 &  5.20 \\
         2 &  4 &  13 & 0 & 1 & 0.17 &  1.98 &  1.74 & 5.64 &  3.42 &  191.07 & 1024.39 &  2.41 &  3.69 &  27.21 &  5.55 \\
         3 & 51 &  77 & 3 & 5 & 4.12 &  6.57 &  5.22 & 4.35 &  0.68 &   63.36 & 3366.84 &  0.28 &  1.28 &  73.06 &  4.08 \\
         4 &  3 &  20 & 0 & 1 & 0.37 &  2.95 &  3.76 & 3.28 &  3.24 &  265.46 & 1443.87 &  1.38 &  4.15 &  39.38 & 13.73 \\
         5 & 12 &  16 & 0 & 1 & 0.06 &  1.22 &  6.12 & 5.01 &  0.59 &   53.97 &  722.26 &  3.22 &  1.50 &  20.77 &  6.11 \\
         6 & 33 &  51 & 2 & 3 & 2.79 &  4.03 &  7.9  & 1.94 &  0.51 &  126.08 & 4374.62 &  2.11 &  1.22 &  44.29 &  5.68 \\
         7 &  2 &  14 & 0 & 1 & 0.26 &  2.91 &  1.69 & 2.74 & 14.64 &  591.22 & 1786.80 &  0.86 &  6.08 &  39.73 &  9.44 \\
         8 &  1 &  20 & 0 & 1 & 0.47 &  4.94 &  0.25 & 3.46 & 13.5  & 1332.83 & 2688.19 &  0.02 & 11.70 & 103.97 &  2.80 \\
         9 & 13 &  16 & 0 & 1 & 0.08 &  1.19 & 11.4  & 3.40 &  0.29 &   87.22 & 1272.78 & 14.28 &  1.59 &  23.46 & 12.40 \\
        10 & 49 &  68 & 3 & 5 & 4.05 &  6.40 &  5.95 & 4.61 &  0.43 &   55.52 & 2982.56 &  2.16 &  1.32 &  73.97 &  4.88 \\
        11 &  2 &  30 & 0 & 1 & 1.11 &  4.99 &  1.65 & 6.77 &  8.51 &  201.52 & 1284.36 &  0.90 &  6.03 &  74.84 &  9.13 \\
        12 &  1 &  48 & 0 & 1 & 2.05 &  8.98 &  0.60 & 2.26 & 14.07 & 1666.17 & 8081.64 &  0.07 & 11.94 & 223.15 &  6.87 \\
        13 &  2 &  28 & 0 & 1 & 1.02 &  4.77 &  0.68 & 3.75 &  8.09 &  489.70 & 2821.91 &  1.87 &  5.17 &  60.88 &  3.17 \\
        14 &  3 &   9 & 0 & 1 & 1.90 &  1.92 &  2.69 & 2.85 &  3.06 &  489.70 & 2821.91 &  1.87 &  5.17 &  60.88 &  3.17 \\
        15 & 66 & 111 & 6 & 8 & 9.05 & 11.64 & 11.08 & 2.18 &  0.91 &   89.75 & 6154.86 &  0.51 &  1.27 &  94.65 &  8.53 \\
        16 &  2 &  29 & 0 & 1 & 1.07 &  4.87 &  1.83 & 7.43 &  2.36 &  181.65 & 1113.08 &  0.75 &  4.96 &  59.12 &  8.16 \\ \bottomrule
      \end{tabular}
    }
  \end{table}
\end{document}

Remarks:
  • It can't do any harm to format already the source accordingly in order to stay on top of things (see code). (La)TeX doesn't care about the format of the source. But it helps you for better readability.
  • It seems better to rotate such tables by the rotating package and place them on their own page by the sidewaystable environment.
NELLLY
Posts: 113
Joined: Thu Nov 26, 2009 2:21 am

Re: Entries of large Table not aligned with Columns

Post by NELLLY »

Many thanks, it works perfectly now.
Post Reply