3

I have some problem with my table: I want to resize it to \textwidth using \resizebox.

The error-message says: "! Missing } inserted." This is a reproducable minimal example:

\begin{table*}[h!]     
\caption{Testtable}  
\centering  \label{} 
\begin{tabular}{@{}lcccc|cccc} 
\resizebox{\columnwidth}{!}{%
 \\[-1.8ex]\hline  \hline \\
[-1.8ex]   & \multicolumn{4}{c}{(1y)} & \multicolumn{4}{c}{(3y)} \\  \\
[-1.8ex] & [1] & [2] & [3] & [4] & [1] & [2] & [3] & [4]\\  
\hline \\
[-1.8ex]   test1 & $-$0.01 & $-$0.03 & 4.50 & $-$3.95 & $-$0.64 & 8.97$^{***}$ & 2.31 & $-$10.22$^{*}$ \\    
& (2.85) & (3.82) & (3.54) & (4.70) & (2.96) & (2.79) & (4.98) & (5.60) \\   
test2 & 11.08$^{***}$ & $-$6.27 & $-$5.59 & 1.26 & 5.07 & 6.50$^{**}$ & $-$4.30 & $-$6.95 \\    
& (3.00) & (3.95) & (3.63) & (4.87) & (3.09) & (2.93) & (5.06) & (5.76) \\  
Adjusted R$^{2}$ & 0.46 & 0.43 & 0.37 & 0.52 & 0.43 & 0.41 & 0.35 & 0.50 \\    
\hline \\
[-1.8ex] 
\end{tabular} 
}
\end{table*}

I think it is related to this problem: Missing $ inserted while adding resizebox. What I have tried so far: adding $: and $% and the combination as suggested at the related problem, without really understanding the meaning. It didn't work.

I'm grateful for advice!

Many Thanks!

Heiko
  • 33
  • 4
    Welcome. First of all *don't* \resizebox your tables. – egreg Mar 20 '22 at 11:07
  • 2
    using resizebox on tables should almost always be avoided. (but it shouldn't generat errors in math) – David Carlisle Mar 20 '22 at 11:07
  • 2
    But the resize around the tabular (the start is inside). But note that any use of resizebox on tables are a sign of bad table design – daleif Mar 20 '22 at 11:07
  • 2
    Don't resize a table this way, this leads to inconsistent font sizes! Also, the issue is that your \resizebox is inside the table body and covers more than a single cell's content. – Skillmon Mar 20 '22 at 11:07
  • Why \columnwidth if you want a full width float? – egreg Mar 20 '22 at 11:17

1 Answers1

4

If the objective is "merely" to have the tabular material take up the full width of the (one- or two-column) text block, do not use \resizebox. Instead, depending on the table's overall properties, use either a tabularx or a tabular* environment (with width set to \textwidth). For the table at hand, line-breaking inside cells would appear to be neither desirable nor necessary. Hence, I suggest you employ a tabular* environment.

I would put in some extra effort, though, to align the numbers in the data columns on their respective decimal markers. In the code below, this is done by employing the machinery of the dcolumn package.

enter image description here

\documentclass[twocolumn]{article} % or some other suitable document class
\usepackage{booktabs} % for well-spaced horizontal rules
\usepackage{dcolumn}  % see https://www.ctan.org/pkg/dcolumn
\newcolumntype{d}[1]{D..{#1}}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro
\begin{document}

\begin{table}
\caption{Test table\strut} \label{} \setlength\tabcolsep{0pt} % let LaTeX figure out intercol. whitespace \begin{tabular
}{\textwidth}{@{\extracolsep{\fill}}l {8}{d{3.3}} } \toprule & \multicolumn{4}{c}{(1y)} & \multicolumn{4}{c}{(3y)} \ \cmidrule{2-5} \cmidrule{6-9} & \mc{[1]} & \mc{[2]} & \mc{[3]} & \mc{[4]} & \mc{[1]} & \mc{[2]} & \mc{[3]} & \mc{[4]} \
\midrule Test1 & -0.01 & -0.03 & 4.50 & -3.95 & -0.64 & 8.97^{
} & 2.31 & -10.22^{*} \
& (2.85) & (3.82) & (3.54) & (4.70) & (2.96) & (2.79) & (4.98) & (5.60) \
\addlinespace Test2 & 11.08^{
} & -6.27 & -5.59 & 1.26 & 5.07 & 6.50^{} & -4.30 & -6.95 \
& (3.00) & (3.95) & (3.63) & (4.87) & (3.09) & (2.93) & (5.06) & (5.76) \
\addlinespace Adjusted R\textsuperscript{2} & 0.46 & 0.43 & 0.37 & 0.52 & 0.43 & 0.41 & 0.35 & 0.50 \
\bottomrule \end{tabular
} \end{table*}

\end{document}

Mico
  • 506,678
  • The error actually occured due to the order of the bracket. My bad, credit to daleif. However, thanks to Mico for the detailed answer and other options, which help me a lot for my further work! – Heiko Mar 20 '22 at 12:59
  • May I add a question regarding your anser Mico. I don't understand the line \begin{tabular*}. The target is to set it at textwidth, but what happens with the following {@{\extracolsep{\fill}}l *{8}{d{3.3}} }? Further, I can't remove all $-signs, since it's a stargazer output and the original table is longer. So I leave out the * at the \begin{table*} command? Thanks! – Heiko Mar 20 '22 at 15:47
  • 1
    @Heiko - Please see this answer to the query How to force a table into page width? for more information about how to use the tabular* environment and about the @{\extracolsep{\fill}} particle. l stands for "1 column of type l[eft]", and *{8}{d{3.3}} means "8 columns of type d{3.3}, where the d column type is defined earlier in the example code with a \newcolumntype instruction. – Mico Mar 20 '22 at 16:46
  • 1
    @Heiko - I'm afraid I don't understand your final question. For sure, I took the table* setup straight from your posting. I was assuming that your document has a two-column setup. If this assumption is unwarranted, do let me know -- and do be willing to explain why you're using a table* environment to begin with. I also don't understand why you say that you cannot remove the $ symbols from the interior of the tabular (or tabular*) environment. Is the file produced by Stargazer really read-only? – Mico Mar 20 '22 at 16:48
  • Thanks again for your help! My document does not have a two-column setup. With the answers so far and the recommendation not to use resizebox, I think the question changes: the problem is not the '$' signs or the math-mode. The problem is the table being slightly too big for the page. Thus, tabular* or tabularx is what I was looking for. To answer your questions anyway: the file is not only read-only but I try to avoid changing to much of the output by hand, since it's not the only table. Thanks for your help! – Heiko Mar 22 '22 at 10:35