1

I'm trying to produce table with equally sized columns, right aligned, using math mode and tabularx package. Everything is ok when all numbers in table are positive:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{tabularx}
\newcolumntype{Q}{>{\hsize=0.7\hsize\raggedleft\arraybackslash$}X<{$}}
\usepackage{booktabs}

\begin{document}

\begin{table}[ht]
\begin{tabularx}{\textwidth}{*{12}{Q}}
  \toprule
  V1 & V2 & V3 & V4 & V5 & V6 & V7 & V8 & V9 & V10 & V11 & V12 \\ 
  \midrule
  5.93 & 4.53 & 5.62 & 4.82 & 5.12 & 4.97 & 4.64 & 5.43 & 4.97 & 5.51 & 5.21 & 5.14 \\ 
  4.32 & 4.47 & 6.02 & 5.35 & 5.61 & 5.32 & 5.76 & 4.95 & 4.12 & 4.00 & 4.98 & 5.51 \\ 
  5.01 & 4.78 & 5.65 & 4.95 & 4.33 & 5.67 & 5.19 & 5.31 & 5.05 & 4.79 & 3.77 & 5.41 \\ 
  5.62 & 5.17 & 5.38 & 4.37 & 5.33 & 5.00 & 3.97 & 5.48 & 4.71 & 5.06 & 6.29 & 4.90 \\ 
  4.64 & 3.99 & 4.14 & 5.84 & 4.74 & 5.51 & 4.32 & 5.84 & 4.51 & 4.55 & 4.90 & 5.19 \\ 
  4.62 & 5.11 & 4.70 & 5.46 & 5.34 & 4.41 & 4.90 & 5.03 & 4.91 & 5.17 & 5.33 & 4.53 \\ 
   \bottomrule
\end{tabularx}
\end{table}

\end{document}

However, when I have negative numbers in my table, it does not work properly anymore:

\begin{table}[ht]
\begin{tabularx}{\textwidth}{*{12}{Q}}
  \toprule
  V1 & V2 & V3 & V4 & V5 & V6 & V7 & V8 & V9 & V10 & V11 & V12 \\ 
  \midrule
 -0.36 &  1.96 & -0.18 &  1.50 & -0.66 &  1.60 & -0.52 & -0.13 &  0.68 &  0.43 &  0.12 &  1.17 \\ 
  0.06 & -0.09 &  0.42 & -1.14 &  0.49 & -0.09 & -0.49 & -1.76 & -1.40 &  0.02 &  0.13 &  1.05 \\ 
 -0.70 &  0.21 &  0.32 & -0.18 & -0.38 &  1.08 &  0.05 & -0.39 &  0.85 & -1.67 &  0.22 &  1.15 \\ 
 -0.72 & -0.74 & -0.78 &  1.90 & -0.56 &  0.63 &  1.30 &  0.09 & -0.45 &  0.74 &  1.64 & -0.58 \\ 
  0.88 & -0.57 & -0.79 & -0.10 & -0.34 & -0.11 &  2.29 &  0.85 &  0.17 &  0.39 & -0.22 &  2.00 \\ 
 -1.02 & -1.32 & -0.50 & -1.36 &  0.09 & -1.53 &  1.55 &  0.96 &  0.07 & -0.27 &  0.17 &  0.07 \\ 
   \bottomrule
\end{tabularx}
\end{table}

How can I fix this?

3
  • 2
    erh \raggedright means left aligned, look at all the minus' they all left align with the contents just as you asked for. Instead if this construction I'd probably use the S column from siunitx with the proper column configuration.
    – daleif
    Commented Jun 12, 2020 at 7:51
  • 12 columns with >{\hsize=0.7\hsize} each also is not correct. Also, X type columns are best used for columns in which you want to introduce a linebreak. Since this is not the case here, I'd also suggest using siunitx.
    – leandriis
    Commented Jun 12, 2020 at 8:05
  • The simplest would be to define a new, right aligned math columns: \newcolumntype{R}{>{$}r<{$}} and used tabular, or tabular*.
    – Sveinung
    Commented Jun 12, 2020 at 11:14

2 Answers 2

1

As leandriis has already commented, siunitx is recommended:

\documentclass[draft]{article}
\usepackage{siunitx}
\usepackage{tabularx,booktabs}

\begin{document}

\begin{table}
    \centering
    \footnotesize
    \begin{tabular}{*{12}{@{\,}S@{\,}}}
        \toprule
        {$V1$} & {$V2$} & {$V3$} & {$V4$} & {$V5$} & {$V6$} & {$V7$} & {$V8$} & {$V9$} & {$V10$} & {$V11$} & {$V12$} \\
        \midrule
        -0.36  & 1.96   & -0.18  & 1.50   & -0.66  & 1.60   & -0.52  & -0.13  & 0.68   & 0.43    & 0.12    & 1.17 \\
        0.06   & -0.09  & 0.42   & -1.14  & 0.49   & -0.09  & -0.49  & -1.76  & -1.40  & 0.02    & 0.13    & 1.05 \\
        -0.70  & 0.21   & 0.32   & -0.18  & -0.38  & 1.08   & 0.05   & -0.39  & 0.85   & -1.67   & 0.22    & 1.15 \\
        -0.72  & -0.74  & -0.78  & 1.90   & -0.56  & 0.63   & 1.30   & 0.09   & -0.45  & 0.74    & 1.64    & -0.58 \\
        0.88   & -0.57  & -0.79  & -0.10  & -0.34  & -0.11  & 2.29   & 0.85   & 0.17   & 0.39    & -0.22   & 2.00 \\
        -1.02  & -1.32  & -0.50  & -1.36  & 0.09   & -1.53  & 1.55   & 0.96   & 0.07   & -0.27   & 0.17    & 0.07 \\
        \bottomrule
    \end{tabular}
\end{table}

\end{document}

\end{document}

enter image description here

2
  • 1
    This works perfectly! Thank you
    – Adela
    Commented Jun 12, 2020 at 8:29
  • siunitx is overkill here, in my opinion.
    – Sveinung
    Commented Jun 12, 2020 at 11:26
2

It is not necessary to bring in siunitx or other heavyweight packages for such simple tabular. Use a tabular*, set tabcolsep to 0pt, and use @{\extracolsep{\fill}} to squeeze the tabular between the margin. If you decrease the margins or use a smaller font, the tabular will adjust.

enter image description here

\documentclass{article}
\usepackage{array, booktabs}
\newcolumntype{R}{>{$}r<{$}}

\begin{document}

\begin{table}[!ht]
\setlength{\tabcolsep}{0pt}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}*{12}{R}}
  \toprule
  V1 & V2 & V3 & V4 & V5 & V6 & V7 & V8 & V9 & V10 & V11 & V12 \\ 
  \midrule
 -0.36 &  1.96 & -0.18 &  1.50 & -0.66 &  1.60 & -0.52 & -0.13 &  0.68 &  0.43 &  0.12 &  1.17 \\ 
  0.06 & -0.09 &  0.42 & -1.14 &  0.49 & -0.09 & -0.49 & -1.76 & -1.40 &  0.02 &  0.13 &  1.05 \\ 
 -0.70 &  0.21 &  0.32 & -0.18 & -0.38 &  1.08 &  0.05 & -0.39 &  0.85 & -1.67 &  0.22 &  1.15 \\ 
 -0.72 & -0.74 & -0.78 &  1.90 & -0.56 &  0.63 &  1.30 &  0.09 & -0.45 &  0.74 &  1.64 & -0.58 \\ 
  0.88 & -0.57 & -0.79 & -0.10 & -0.34 & -0.11 &  2.29 &  0.85 &  0.17 &  0.39 & -0.22 &  2.00 \\ 
 -1.02 & -1.32 & -0.50 & -1.36 &  0.09 & -1.53 &  1.55 &  0.96 &  0.07 & -0.27 &  0.17 &  0.07 \\ 
   \bottomrule
\end{tabular*}
\end{table}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .