2

I am trying to define the maximum width of a tabular column, see the code below:

\documentclass{article}

\usepackage[]{float}
\usepackage[]{hyperref}
\usepackage[]{booktabs}
\usepackage[]{array}
\usepackage[]{varwidth}
\title{}
\author{}
\date{\today}
\newcolumntype{\vbf}{>{\begin{varwidth}{3.0cm}}l<{\end{varwidth}}}

\begin{document}

        \begin{table}[H]
            \centering

            \begin{tabular}{\vbf\vbf\vbf\vbf\vbf\vbf}
            \toprule
             0  & 1 & 2 & 3 & 4 & 5 \\
            \midrule
             1 & 11802316.16 & 10270230.14 &  210947.01 & 6716332.35 & 6808728.66 \\
             2 & 9992538.31 & 10211855.55 &  331381.44 & 6444971.54 & 6403637.45 \\
             3 & 9989460.45 & 10174367.75 &  282403.66 & 7065539.95 & 7152965.85 \\
             4 & 11773342.70 & 10413039.41 &  297383.17 & 6267637.00 & 6375493.07 \\
            \bottomrule
            \end{tabular}
            \end{table}

\end{document}

The error is:

! Misplaced \noalign.
\midrule ^^@-\noalign 
                    {\ifnum 0=`}\fi \@aboverulesep =\aboverulesep \global \@...
l.21            \midrule

How can I fix this error?

  • Is there same special reason to use varwidth? From your MWE I can conclude, that you actually need S column type from package siunitx. – Zarko Aug 11 '15 at 13:44
  • I found this as an example. tex.stackexchange.com/questions/4709/… – OHLÁLÁ Aug 11 '15 at 13:47
  • If you set one single cell as \begin{varwidth}{3cm}7121855.55\end{varwidth} there is nothing happening IMO. What do you try to get here? – LaRiFaRi Aug 11 '15 at 13:47
3

Error is caused by missing \arraybackslash in your definition of column. I also sugest to not name column type with macro like \vbf, better is select for example some leter as has been used in answer which you like to repit. SO:

\documentclass{article}
    \usepackage[]{float}
    \usepackage[]{booktabs}
    \usepackage[]{array}
    \usepackage[]{varwidth}
\newcolumntype{M}{>{\begin{varwidth}{4cm}\arraybackslash}l<{\end{varwidth}}}

\begin{document}
    \begin{table}[H]
        \centering
\begin{tabular}{*{7}{M}}
\toprule
 0  & 1 & 2 & 3 & 4 & 5 \\
\midrule
 1 & 11802316.16 & 10270230.14 &  210947.01 & 6716332.35 & 6808728.66 \\
 2 & 9992538.31 & 10211855.55 &  331381.44 & 6444971.54 & 6403637.45  \\
 3 & 9989460.45 & 10174367.75 &  282403.66 & 7065539.95 & 7152965.85  \\
 4 & 11773342.70 & 10413039.41 &  297383.17 & 6267637.00 & 6375493.07 \\
\bottomrule
\end{tabular}
    \end{table}
\end{document}

It works as you expected. Tested!

  • Since you mentioned siunitx in comments, it would be a nice addition showing the different (and better) way. – egreg Aug 11 '15 at 17:23
  • I also would like to have line breakes. – OHLÁLÁ Aug 11 '15 at 18:05
  • @egreg, you already do this! In my answer I was focused only to OP problem . By the way, from your solution with \tabularnewline I learn something new! – Zarko Aug 11 '15 at 19:01
3

The example you found suggests varwidth for a very different situation, where line breaks in a cell are wanted. In your table you have nothing of that kind, so the output is exactly the same as if you just use l.

Here's the code that proves the claim, using \tabularnewline instead of \\ for ending lines:

\documentclass{article}

\usepackage[]{booktabs}
\usepackage[]{array}
\usepackage[]{varwidth}

\newcolumntype{\vbf}{>{\begin{varwidth}{3.0cm}}l<{\end{varwidth}}}

\title{}
\author{}
\date{\today}

\begin{document}

\begin{tabular}{\vbf\vbf\vbf\vbf\vbf\vbf}
\toprule
0  & 1 & 2 & 3 & 4 & 5 \\
\midrule
1 & 11802316.16 & 10270230.14 &  210947.01 & 6716332.35 & 6808728.66 \tabularnewline
2 & 9992538.31 & 10211855.55 &  331381.44 & 6444971.54 & 6403637.45 \tabularnewline
3 & 9989460.45 & 10174367.75 &  282403.66 & 7065539.95 & 7152965.85 \tabularnewline
4 & 11773342.70 & 10413039.41 &  297383.17 & 6267637.00 & 6375493.07 \tabularnewline
\bottomrule
\end{tabular}

\bigskip

\begin{tabular}{l l l l l l}
\toprule
0  & 1 & 2 & 3 & 4 & 5 \\
\midrule
1 & 11802316.16 & 10270230.14 &  210947.01 & 6716332.35 & 6808728.66 \\
2 & 9992538.31 & 10211855.55 &  331381.44 & 6444971.54 & 6403637.45 \\
3 & 9989460.45 & 10174367.75 &  282403.66 & 7065539.95 & 7152965.85 \\
4 & 11773342.70 & 10413039.41 &  297383.17 & 6267637.00 & 6375493.07 \\
\bottomrule
\end{tabular}

\end{document}

enter image description here

Here's a different way to input the table, using siunitx:

\documentclass{article}

\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}

\begin{table}[htp]

\centering
\sisetup{group-digits=false}

\begin{tabular}{
  c
  S[table-format=8.2]
  S[table-format=8.2]
  S[table-format=6.2]
  S[table-format=7.2]
  S[table-format=7.2]
}
\toprule
0 & {1} & {2} & {3} & {4} & {5} \\
\midrule
1 & 11802316.16 & 10270230.14 &  210947.01 & 6716332.35 & 6808728.66 \\
2 & 9992538.31 & 10211855.55 &  331381.44 & 6444971.54 & 6403637.45 \\
3 & 9989460.45 & 10174367.75 &  282403.66 & 7065539.95 & 7152965.85 \\
4 & 11773342.70 & 10413039.41 &  297383.17 & 6267637.00 & 6375493.07 \\
\bottomrule
\end{tabular}

\end{table}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.