0

I am using a resizebox to fit a table into the page width, however it gives me a strange Error: Undefined control sequence. } at the end of the resizebox. I found that it may be related to the & symbol that needs to be escaped but I am not getting how. Any idea how to fix this?

This is my table:

\begin{table}[ht]
    \caption{Cars Database} % title of Table
    \label{tab:carsDB}
    \centering % used for centering table
    \resizebox{\textwidth}{!}{
        \begin{tabular}{@{\makebox[0em][r]{\rownumber\space}}|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}
            \cline{1-14}
            \rowcolor{LightGray}
            Make & Model & Price & Location & Year & \thead{Start\\ Production\\ Year} & Color & Mileage & \thead{Length\\ (mm)} & \thead{Power\\ (kw)} & EngineCC & \thead{Torque\\ (Nm)} & \thead{Consumption (US mpg)\\ Urban/Extra Urban/Combined} & Fuel
            \gdef\rownumber{\stepcounter{magicrownumbers}\arabic{magicrownumbers}}
            \\
            \cline{1-14}
            Ford & Focus 1.6tdci & 6500 & Pittsburgh (PA) & 2007 & 2004 & Red & 60000 & 4342 & 80 & 1560 & 260 & 37.97/58.78/49.04 & Diesel\\
            \cline{1-14}
            Mazda & 3 td & 8000 & Denver (CO) & 2009 & 2009 & Gray & 45000 & 4580 & 80 & 1560 & 240 & 44.38/60.28/53.45 & Diesel\\
            \cline{1-14}
            Ford & Focus 1.3tdci & 11000 & San Diego(CA) & 2010 & 2010 & Blue & 15000 & 4337 & 85 & 1560 & 270 & 52.29/69.19/61.86 & Diesel\\
            \cline{1-14}
            Ford & Focus 1.6i 16V & 3000 & Austin(TX) & 2005 & 2004 & Blue & 48000 & 4342 & 73.8 & 1596 & 150 & 21.56/38.55/30.14 & Gasoline\\
            \cline{1-14}
            Volkswagen & Golf 1.6tdi & 12000 & Las Vegas(NV) & 2008 & 2008 & Black & 70000 & 4199 & 77 & 1598 & 250 & 41.30/60.28/52.29 & Diesel\\
            \cline{1-14}
            Ford & Focus 1.6tdci & 12300 & Seattle(WA) & 2011 & 2010 & Gray & 8000 & 4337 & 85 & 1560 & 270 & 52.29/69.19/61.86 & Diesel\\
            \cline{1-14}
            Volkswagen & Golf 1.6tdi & 14000 & Miami(FL) & 2009 & 2008 & Gray & 50000 & 4199 & 77 & 1598 & 250 & 41.30/60.28/52.29 & Diesel\\
            \cline{1-14}
            Toyota & Avensis 1.8 16V SW & 6000 & New Orleans(LU) & 2005 & 2003 & Red & 38000 & 4700 & 95 & 1794 & 170 & 25.06/40.55/32.64 & Gasoline\\
            \cline{1-14}
            Ford & Focus 1.6tdci & 5000 & Chicago(IL) & 2006 & 2004 & White & 66000 & 4342 & 80 & 1560 & 260 & 37.97/58.78/49.04 & Diesel\\
            \cline{1-14}
            Ford & Fiesta 1.4 16V & 7000 & Charleston(SC) & 2009 & 2008 & Black & 27000 & 3950 & 71 & 1388 & 125 & 31.39/51.12/41.30 & Gasoline\\
            \cline{1-14}
        \end{tabular}
    }
\end{table}

Update

\usepackage{makecell}
\usepackage{array,etoolbox}
\preto\tabular{\setcounter{magicrownumbers}{0}}
\newcounter{magicrownumbers}
4
  • your definitions are not available to us --magicrownumber
    – js bibra
    Jan 27 at 9:43
  • 2
    Don't use \resizebox with tables, as it leads to inconsistent font sizes.
    – Bernard
    Jan 27 at 9:43
  • I added the definition of magicrownumber
    – 1Z10
    Jan 27 at 9:46
  • So what is the alternative to resizebox?
    – 1Z10
    Jan 27 at 9:46
1

For such a wide table you should consider sidewaystable from the rotating package.

Anyway, you need to define \rownumber also outside the table for the business to work.

\documentclass{article}
\usepackage{graphicx} % <- for \resizebox
\usepackage[table,svgnames]{xcolor} % <- for \rowcolor
\usepackage{makecell}
\usepackage{array,etoolbox}

\preto\tabular{\setcounter{magicrownumbers}{0}}
\newcounter{magicrownumbers}

\begin{document}

\begin{table}[ht]

    \caption{Cars Database} % title of Table
    \label{tab:carsDB}

\gdef\rownumber{}

    \resizebox{\textwidth}{!}{% <- important %
        \begin{tabular}{@{\makebox[0em][r]{\rownumber\space}}|c|c|c|c|c|c|c|c|c|c|c|c|c|c|}
            \cline{1-14}
            \rowcolor{LightGray}
            Make & Model & Price & Location & Year & \thead{Start\\ Production\\ Year} & Color & Mileage & \thead{Length\\ (mm)} & \thead{Power\\ (kw)} & EngineCC & \thead{Torque\\ (Nm)} & \thead{Consumption (US mpg)\\ Urban/Extra Urban/Combined} & Fuel%
            \gdef\rownumber{\stepcounter{magicrownumbers}\arabic{magicrownumbers}}
            \\
            \cline{1-14}
            Ford & Focus 1.6tdci & 6500 & Pittsburgh (PA) & 2007 & 2004 & Red & 60000 & 4342 & 80 & 1560 & 260 & 37.97/58.78/49.04 & Diesel\\
            \cline{1-14}
            Mazda & 3 td & 8000 & Denver (CO) & 2009 & 2009 & Gray & 45000 & 4580 & 80 & 1560 & 240 & 44.38/60.28/53.45 & Diesel\\
            \cline{1-14}
            Ford & Focus 1.3tdci & 11000 & San Diego(CA) & 2010 & 2010 & Blue & 15000 & 4337 & 85 & 1560 & 270 & 52.29/69.19/61.86 & Diesel\\
            \cline{1-14}
            Ford & Focus 1.6i 16V & 3000 & Austin(TX) & 2005 & 2004 & Blue & 48000 & 4342 & 73.8 & 1596 & 150 & 21.56/38.55/30.14 & Gasoline\\
            \cline{1-14}
            Volkswagen & Golf 1.6tdi & 12000 & Las Vegas(NV) & 2008 & 2008 & Black & 70000 & 4199 & 77 & 1598 & 250 & 41.30/60.28/52.29 & Diesel\\
            \cline{1-14}
            Ford & Focus 1.6tdci & 12300 & Seattle(WA) & 2011 & 2010 & Gray & 8000 & 4337 & 85 & 1560 & 270 & 52.29/69.19/61.86 & Diesel\\
            \cline{1-14}
            Volkswagen & Golf 1.6tdi & 14000 & Miami(FL) & 2009 & 2008 & Gray & 50000 & 4199 & 77 & 1598 & 250 & 41.30/60.28/52.29 & Diesel\\
            \cline{1-14}
            Toyota & Avensis 1.8 16V SW & 6000 & New Orleans(LU) & 2005 & 2003 & Red & 38000 & 4700 & 95 & 1794 & 170 & 25.06/40.55/32.64 & Gasoline\\
            \cline{1-14}
            Ford & Focus 1.6tdci & 5000 & Chicago(IL) & 2006 & 2004 & White & 66000 & 4342 & 80 & 1560 & 260 & 37.97/58.78/49.04 & Diesel\\
            \cline{1-14}
            Ford & Fiesta 1.4 16V & 7000 & Charleston(SC) & 2009 & 2008 & Black & 27000 & 3950 & 71 & 1388 & 125 & 31.39/51.12/41.30 & Gasoline\\
            \cline{1-14}
        \end{tabular}% <- important %
    }
\end{table}

\end{document}

Check the <- fixes I made.

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.