4

I try to design a neat table here and what I am doing is:

\begin{table}[ht]                                                                                                                                                                  
\centering                                                                                                                                                                     
\resizebox{\textwidth}{!}{%
\begin{tabular}{r r r r r r r r r r r r r}                                                                                                                                   
\hline                                                                                                                                                                         
\multicolumn{3}{r}{Set A}    
&&                                           
\multicolumn{3}{r}{Set B}
&&
\multicolumn{3}{r}{Set C}
&&
\multicolumn{3}{r}{Set D} \\\cmidrule(r){2-4}\cmidrule(r){5-7}\cmidrule(r){8-10}\cmidrule(r){11-13}\\  
 & Coef & tstat & Adj.R-sq & Coef & tstat & Adj.R-sq & Coef & tstat & Adj.R-sq & Coef & tstat & Adj.R-sq \\
\hline                                                                                                                                                                         
logGP & -2.735 & -4.203 & 0.048 & -1.047 & -2.675 & 0.018 & 0.094 & 0.453 & -0.002 & 0.193 & 1.710 & 0.006 \\                                                                  

logPD & 0.096 & 0.281 & -0.003 & 0.028 & 0.139 & -0.003 & -0.012 & -0.115 & -0.003 & -0.005 & -0.087 & -0.003 \\                                                               

PNY & 0.233 & 0.298 & -0.003 & -0.040 & -0.086 & -0.003 & -0.165 & -0.677 & -0.002 & 0.080 & 0.588 & -0.002 \\                                                                 

CAY & -0.823 & -0.343 & -0.003 & 0.752 & 0.526 & -0.002 & 1.315 & 1.766 & 0.006 & 0.729 & 1.787 & 0.007 \\                                                                     

VRP & 0.002 & 1.386 & 0.003 & -0.001 & -1.451 & 0.004 & 0.001 & 1.784 & 0.007 & -0.000 & -0.688 & -0.002 \\                                                                    

Def.Prem & 0.112 & 1.513 & 0.004 & 0.036 & 0.813 & -0.001 & -0.034 & -1.495 & 0.004 & -0.061 & -5.015 & 0.069 \\                                                               

Inflation & -4.642 & -0.411 & -0.003 & 5.123 & 0.767 & -0.001 & -11.654 & -3.389 & 0.031 & -2.761 & -1.444 & 0.003 \\                                                          

Termspread & -0.013 & -0.524 & -0.002 & -0.005 & -0.307 & -0.003 & 0.025 & 3.330 & 0.030 & 0.044 & 12.617 & 0.326 \\                                                           
\hline                                                                                                                                                                         
\end{tabular}                                                                                                                                                                  
}
\caption{MyTableCaption}                                                                                                                                                       
\label{table:MyTableLabel}                                                                                                                                                     
\end{table}

I get this error and cannot go forward: ! Extra alignment tab has been changed to \cr.

3
  • 1
    Welcome to TeX SX! You declare 13 columns, but use 15 in the first row. Other thaan that, never use \adjustboxwith tables: it yields inconsistent font sizes.
    – Bernard
    May 27, 2020 at 21:19
  • Please add the documentclass you are using. Also do you use the geometry package? If so, please also include the corresponding settings. Depending on the size of your margins, your table will quite likely not fit into the textwidth while still keeping a reasonable font size. You might have to do some redesigning to make it fit.
    – leandriis
    May 27, 2020 at 21:36
  • 2
    Regarding the error message itself: \multicolumn{3}{r}{Set A} & \multicolumn{3}{r}{Set B} & \multicolumn{3}{r}{Set C} & \multicolumn{3}{r}{Set D} should make the code work.
    – leandriis
    May 27, 2020 at 21:37

4 Answers 4

5
  • Please, in questions always provide MWE (Minimal Working Example), a small, complete, compilable document, which reproduce your problem. Only table code fragment doesn't gives information about your document's page layout, which is important in your table design.
  • Regarding the error message itself, it is solved by @leandriis comment and thoroughly explained in @wiped answer.
  • Some off-topic suggestions:
    • for negative numbers you should use correct minus sign, so in columns with numbers I suggest to use S column type, which provide consistent printing of numbers
    • don't terminate horizontal rules with \\,
    • since your table is wider than text width, it is sensible to left calculation of inter column spaces (\tabcolsep) to macro \extracolsep and by this reduce table width to \textwidth:
\documentclass{article}
\usepackage[margin=25mm]{geometry}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document} 
    \begin{table}[ht]
    \small
    \setlength\tabcolsep{0pt}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}r *{12}{S[table-format=-1.3]} @{}}
    \toprule
\multicolumn{3}{r}{Set A} 
    & \multicolumn{3}{r}{Set B} 
        & \multicolumn{3}{r}{Set C} 
            & \multicolumn{3}{r}{Set D}     \\
    \cmidrule{2-4}\cmidrule(l){5-7}\cmidrule(l){8-10}\cmidrule(l){11-13}
    & {Coef} & {tstat} & {Adj.R-sq} 
        & {Coef} & {tstat} & {Adj.R-sq} 
            & {Coef} & {tstat} & {Adj.R-sq} 
                & {Coef} & {tstat} & {Adj.R-sq}   \\
    \midrule
logGP & -2.735 & -4.203 & 0.048 & -1.047 & -2.675 & 0.018 & 0.094 & 0.453 & -0.002 & 0.193 & 1.710 & 0.006 \\
logPD & 0.096 & 0.281 & -0.003 & 0.028 & 0.139 & -0.003 & -0.012 & -0.115 & -0.003 & -0.005 & -0.087 & -0.003 \\
PNY & 0.233 & 0.298 & -0.003 & -0.040 & -0.086 & -0.003 & -0.165 & -0.677 & -0.002 & 0.080 & 0.588 & -0.002 \\
CAY & -0.823 & -0.343 & -0.003 & 0.752 & 0.526 & -0.002 & 1.315 & 1.766 & 0.006 & 0.729 & 1.787 & 0.007 \\
VRP & 0.002 & 1.386 & 0.003 & -0.001 & -1.451 & 0.004 & 0.001 & 1.784 & 0.007 & -0.000 & -0.688 & -0.002 \\
Def.Prem & 0.112 & 1.513 & 0.004 & 0.036 & 0.813 & -0.001 & -0.034 & -1.495 & 0.004 & -0.061 & -5.015 & 0.069 \\
Inflation & -4.642 & -0.411 & -0.003 & 5.123 & 0.767 & -0.001 & -11.654 & -3.389 & 0.031 & -2.761 & -1.444 & 0.003 \\
Termspread & -0.013 & -0.524 & -0.002 & -0.005 & -0.307 & -0.003 & 0.025 & 3.330 & 0.030 & 0.044 & 12.617 & 0.326 \\
    \bottomrule
\end{tabular*}
\caption{My Table Caption}
\label{table:MyTableLabel}
    \end{table}
\end{document}

enter image description here

1
  • 1
    @Mico, thank you very much! You are right. Now centering hasn't sence. Corrected now.
    – Zarko
    May 28, 2020 at 8:16
2

This is typical example why LaTeX never can serve as new level over TeX and hiding TeX although it tries to do this tens years.

How many percent of LaTeX users understand the error message "Extra alignment tab has been changed to \cr"? Only few users because LaTeX level typically does not document this TeX level.

What the message mans? Tables are created in TeX by \halign primitve, especially the macro tabular uses this primitive. The number of colums (and their design) is declared first in \halign and then the lines of the table are created by item & item & ... & item \cr for each table line. If n columns are declared then there must be n-1 aligment tabs & and one \cr. You can write less than n items (then \cr go to the next line and some columns are leaved empty) but you cannot write more than n items, i.e. more than n-1 alignment tabs &. If you try it then you get the mentioned error.

LaTeX hides the primtive command \cr to the control sequence \\ which have context dependent meaning, but when \halign is processing its parameter then \\ means exactly \cr. LaTeX's \multicolumn{k}{decl}{text} macro spans over k items, it means that it creates exactly k-1 virtual alignment tabs.

Now, you can calculate how many alignment tabs you have in each line of the table. The error message "Extra alignemnt tab" tells you that there are more alignmnent tabs than follows from the column declaration of this \halign.

Edit: I append something about the second part of this error message "has been changed to \cr". TeX typically say what it does for recovery of the error. Suppose that you have declared four columns in the table, but you wote

itemA & itemB & itemC & itemD & itemF \cr
                            % ^ ... this is the reason of the error

Then the error message looks like:

! Extra alignment tab has been changed to \cr.
... 
...  itemA & item B & item C & item D &
                                        itemF \cr 

Very important is the position where the error message line is broken to two lines. The last token from the first message line causes the error. And TeX suggests to correct this by replacing this & by \cr, i.e. we get:

itemA & itemB & itemC & itemD \cr itemF \cr

and this yields to the resulting table:

itemA itemB itemC itemD
itemF
next items ...
1

Is this what you had in mind

enter image description here

\begin{table}[h]   
    \noindent                                                                                                 
      \hskip-3.0cm\footnotesize
        \begin{tabular}{rrrrrrrrrrrrr}                                                                                                                                    
            \hline                                                                                                                                                                         
            \multicolumn{3}{r}{Set A}%    
                &%                                           
                \multicolumn{3}{r}{Set B}%
                    &%
                    \multicolumn{3}{r}{Set C}%
                        &%
                        \multicolumn{3}{r}{Set D}& \\
%                       \cmidrule(r){2-4}\cmidrule(r){5-7}\cmidrule(r){8- 
 %           10}\cmidrule(r){11-13}\\  
            & Coef & tstat & Adj.R-sq & Coef & tstat & Adj.R-sq & Coef & tstat & 
            Adj.R-sq & Coef & tstat & Adj.R-sq \\
            \hline                                                                                                                                                                         
            logGP & -2.735 & -4.203 & 0.048 & -1.047 & -2.675 & 0.018 & 0.094 & 0.453 
             & -0.002 & 0.193 & 1.710 & 0.006 \\                                                                  

            logPD & 0.096 & 0.281 & -0.003 & 0.028 & 0.139 & -0.003 & -0.012 & -0.115 
            & -0.003 & -0.005 & -0.087 & -0.003 \\                                                               

            PNY & 0.233 & 0.298 & -0.003 & -0.040 & -0.086 & -0.003 & -0.165 & -0.677 
            & -0.002 & 0.080 & 0.588 & -0.002 \\                                                                 

            CAY & -0.823 & -0.343 & -0.003 & 0.752 & 0.526 & -0.002 & 1.315 & 1.766 & 
            0.006 & 0.729 & 1.787 & 0.007 \\                                                                     

            VRP & 0.002 & 1.386 & 0.003 & -0.001 & -1.451 & 0.004 & 0.001 & 1.784 & 
            0.007 & -0.000 & -0.688 & -0.002 \\                                                                    

            Def.Prem & 0.112 & 1.513 & 0.004 & 0.036 & 0.813 & -0.001 & -0.034 & 
            -1.495 & 0.004 & -0.061 & -5.015 & 0.069 \\                                                               

            Inflation & -4.642 & -0.411 & -0.003 & 5.123 & 0.767 & -0.001 & -11.654 & 
            -3.389 & 0.031 & -2.761 & -1.444 & 0.003 \\                                                          

            Termspread & -0.013 & -0.524 & -0.002 & -0.005 & -0.307 & -0.003 & 0.025 & 
            3.330 & 0.030 & 0.044 & 12.617 & 0.326 \\                                                           
            \hline                                                                                                                                                                         
        \end{tabular}                                   
    \caption{MyTableCaption}                                                                                                                                                       
    \label{table:MyTableLabel}                                                                                                                                                     
\end{table}
1

The immediate cause of the problem is that you write

\multicolumn{3}{r}{Set A} &&                                           
\multicolumn{3}{r}{Set B} &&
\multicolumn{3}{r}{Set C} &&
\multicolumn{3}{r}{Set D}

which requires the tabular environment to have (at least) 15 columns, whereas you've defined it to have 13 columns (of type r).

The fix this problem, I suggest you change the preceding code chunk to

& \multicolumn{3}{c}{Set A} 
& \multicolumn{3}{c}{Set B} 
& \multicolumn{3}{c}{Set C}
& \multicolumn{3}{c}{Set D}

Observe that I'm suggesting you start with a blank cell, followed by 4 centered, rather than right-aligned, labels ("Set A", "Set B", etc.).


Here's a solution which is, overall, fairly similar to @Zarko's. For instance, (a) it does not use the \resizebox hack, and it uses (b) the S column type for the 12 data columns to align the numbers on their respective decimal points while allowing typographically correct - ("minus") symbols, and (c) a tabular* environment to simplify the process of squeezing the table into the width of the text block. One difference relative to @Zarko's solution is the already-mentioned change in the 4 \multicolumn directives. Another difference concerns a simplification in the header cells, where I write $\bar{R}^2$ instead of Adj.R-sq; this simple change frees up a surprising amount of (horizontal) whitespace.

The following screenshot shows two tables which are identical up to the (relative) font size: The upper one employs the default, i.e., \normalsize, whereas the lower one employ \small. I'll leave it up to you to decide which font size is "better".

enter image description here

\documentclass{article} % or some other suitable document class
\usepackage[letterpaper,margin=1in]{geometry} % set suitable page size parameters
\usepackage{booktabs,siunitx}

\begin{document}

\begin{table}[ht]                                                                                                                                                                  
\setlength\tabcolsep{0pt} % let LaTeX figure out the optimal amount of intercolumn whitespace

\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}
   l *{6}{S[table-format=-1.3]} 
          S[table-format=-2.3] % one column needs a bit more space than the other 11
     *{5}{S[table-format=-1.3]} }                                                                                                                                   
\toprule
&\multicolumn{3}{c}{Set A} & \multicolumn{3}{c}{Set B}
&\multicolumn{3}{c}{Set C} & \multicolumn{3}{c}{Set D} \\
\cmidrule{2-4} \cmidrule{5-7} \cmidrule{8-10} \cmidrule{11-13}
 & {Coef} & {tstat} & {$\bar{R}^2$} & {Coef} & {tstat} & {$\bar{R}^2$} 
 & {Coef} & {tstat} & {$\bar{R}^2$} & {Coef} & {tstat} & {$\bar{R}^2$} \\
\midrule
logGP & -2.735 & -4.203 &  0.048 & -1.047 & -2.675 &  0.018 &  0.094 &  0.453 & -0.002 &  0.193 &  1.710 &  0.006 \\                                                                  
logPD &  0.096 &  0.281 & -0.003 &  0.028 &  0.139 & -0.003 & -0.012 & -0.115 & -0.003 & -0.005 & -0.087 & -0.003 \\                                                               
PNY   &  0.233 &  0.298 & -0.003 & -0.040 & -0.086 & -0.003 & -0.165 & -0.677 & -0.002 &  0.080 &  0.588 & -0.002 \\                                                                 
CAY   & -0.823 & -0.343 & -0.003 &  0.752 &  0.526 & -0.002 &  1.315 &  1.766 &  0.006 &  0.729 &  1.787 &  0.007 \\                                                                     
VRP   &  0.002 &  1.386 &  0.003 & -0.001 & -1.451 &  0.004 &  0.001 &  1.784 &  0.007 & -0.000 & -0.688 & -0.002 \\                                                                    
DefPrem& 0.112 &  1.513 &  0.004 &  0.036 &  0.813 & -0.001 & -0.034 & -1.495 &  0.004 & -0.061 & -5.015 &  0.069 \\                                                               
Inflation&-4.642&-0.411 & -0.003 &  5.123 &  0.767 & -0.001 &-11.654 & -3.389 &  0.031 & -2.761 & -1.444 &  0.003 \\                                                          
Termspread&-0.013&-0.524 &-0.002 & -0.005 & -0.307 & -0.003 &  0.025 &  3.330 &  0.030 &  0.044 & 12.617 &  0.326 \\                                                           
\bottomrule
\end{tabular*}                                                                                                                                                                  
\caption{Use default font size, i.e., \texttt{\string\normalsize}}                                                                                                                                                       
\label{table:default_size}                                                                                                                                                    

\bigskip\bigskip
\small % 10% linear reduction in font size
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}
   l *{6}{S[table-format=-1.3]} 
          S[table-format=-2.3] % one column needs a bit more space than the other 11
     *{5}{S[table-format=-1.3]} }                                                                                                                                   
\toprule
&\multicolumn{3}{c}{Set A} &\multicolumn{3}{c}{Set B}
&\multicolumn{3}{c}{Set C} &\multicolumn{3}{c}{Set D} \\
\cmidrule{2-4} \cmidrule{5-7} \cmidrule{8-10} \cmidrule{11-13}
 & {Coef} & {tstat} & {$\bar{R}^2$} & {Coef} & {tstat} & {$\bar{R}^2$} 
 & {Coef} & {tstat} & {$\bar{R}^2$} & {Coef} & {tstat} & {$\bar{R}^2$} \\
\midrule
logGP & -2.735 & -4.203 &  0.048 & -1.047 & -2.675 &  0.018 &  0.094 &  0.453 & -0.002 &  0.193 &  1.710 &  0.006 \\                                                                  
logPD &  0.096 &  0.281 & -0.003 &  0.028 &  0.139 & -0.003 & -0.012 & -0.115 & -0.003 & -0.005 & -0.087 & -0.003 \\                                                               
PNY   &  0.233 &  0.298 & -0.003 & -0.040 & -0.086 & -0.003 & -0.165 & -0.677 & -0.002 &  0.080 &  0.588 & -0.002 \\                                                                 
CAY   & -0.823 & -0.343 & -0.003 &  0.752 &  0.526 & -0.002 &  1.315 &  1.766 &  0.006 &  0.729 &  1.787 &  0.007 \\                                                                     
VRP   &  0.002 &  1.386 &  0.003 & -0.001 & -1.451 &  0.004 &  0.001 &  1.784 &  0.007 & -0.000 & -0.688 & -0.002 \\                                                                    
DefPrem& 0.112 &  1.513 &  0.004 &  0.036 &  0.813 & -0.001 & -0.034 & -1.495 &  0.004 & -0.061 & -5.015 &  0.069 \\                                                               
Inflation&-4.642&-0.411 & -0.003 &  5.123 &  0.767 & -0.001 &-11.654 & -3.389 &  0.031 & -2.761 & -1.444 &  0.003 \\                                                          
Termspread&-0.013&-0.524 &-0.002 & -0.005 & -0.307 & -0.003 &  0.025 &  3.330 &  0.030 &  0.044 & 12.617 &  0.326 \\                                                           
\bottomrule
\end{tabular*}                                                                                                                                                                  
\caption{Use \texttt{\string\small} font size}                                                                                                                                                       
\label{table:small_size}                                                                                                                                                    

\end{table}
\end{document}

You must log in to answer this question.

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