5

I'm trying to shrink table to fit on a page (as described here) but code doesn't compile. Also, my table has some tiny gaps in margins, how do I remove those?

My code:

\adjustbox{max width=\textwidth}{
\begin{table}[!htbp]
    \centering
    \begin{tabular}{@{}|l|c|c|c|c|l|c|@{}}
        \toprule
        \multicolumn{1}{|c|}{\multirow{2}{*}{\textbf{Sub-Item}}} & \multirow{2}{*}{\textbf{Projeto}} & \multirow{2}{*}{\textbf{Processos}} & \multicolumn{2}{c|}{\textbf{Valor individual por}} & \multirow{2}{*}{\textbf{Quantidade}} & \multirow{2}{*}{\textbf{Total}} \\ \cmidrule(lr){4-5}
        \multicolumn{1}{|c|}{} &  &  & \textbf{Projeto} & \textbf{Processo} &  &  \\ \midrule
        Serviço de medição & 1 & 4 &  &  & \multicolumn{1}{c|}{2} &  \\ \midrule
        Serviço de apoio a projeto & 1 & 4 &  &  & \multicolumn{1}{c|}{1} &  \\ \midrule
        \multicolumn{6}{|r|}{\textbf{Valor total}} & \multicolumn{1}{l|}{} \\ \bottomrule
    \end{tabular}
    \caption{Proposta de preço para item 1}
    \label{PropostaItem1}
\end{table}
}

Compiler outputs this:

Not in outer par mode. \begin{table}[!htbp]
Undefined control sequence. \begin{table}[!htbp]
Missing number, treated as zero. \begin{table}[!htbp]
Overfull \hbox (0.69336pt too wide) in paragraph

See the table without \adjustbox{max width=\textwidth}:

Output

  • 1
    The argument of \adjustbox has to be the tabular environment, not the table environment. – Mico Sep 12 '16 at 16:26
  • 1
    Never use vertical rules when employing booktabs rules (\toprule, \midrule and \bottomrule). – egreg Sep 12 '16 at 16:33
  • This problem is very similar in nature to How to use figure inside a minipage? – Werner Sep 12 '16 at 16:37
  • 2
    scaling a table to make it fit is so horrible it just produces inconsistent font sizes. You do not justify paragraphs of text by arbitrarily scaling the text to make it fit, so why do that if the text happens to be in a table? It is far better to use a consistent documentclass font size such as \small if the text needs to be smaller. – David Carlisle Sep 12 '16 at 16:44
5

The object that (potentially) has to be resized via \adjustbox is the tabular material, not the table environment.

Also, as @egreg has already pointed out in a comment, don't use vertical rules if you use the line-drawing macros of the booktabs package. Actually, coming to think of it, don't use vertical rules at all, whether or not you use the line-drawing macros of the booktabs package...

enter image description here

\documentclass[portuguese]{article} % or spanish?!
\usepackage{lmodern,babel,adjustbox,booktabs,multirow}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\begin{document}

\begin{table}[!htbp]
\adjustbox{max width=\textwidth}{%
\centering
\begin{tabular}{@{}l*{6}{c}@{}}
\toprule
\multicolumn{1}{c}{\multirow{2}{*}{\textbf{Sub-Item}}} & 
\multirow{2}{*}{\textbf{Projeto}} & 
\multirow{2}{*}{\textbf{Processos}} & 
\multicolumn{2}{c}{\textbf{Valor individual por}} & 
\multirow{2}{*}{\textbf{Quantidade}} & 
\multirow{2}{*}{\textbf{Total}} \\ \cmidrule(lr){4-5}
 &  &  & \textbf{Projeto} & \textbf{Processo} \\ 
\midrule
Serviço de medição & 1 & 4 &  &  & 2 &  \\ %%\midrule
Serviço de apoio a projeto & 1 & 4 &  &  & 1 &  \\ 
\midrule
\multicolumn{6}{r}{\textbf{Valor total}} &  \\ 
\bottomrule
\end{tabular}}
\caption{Proposta de preço para item 1}
\label{PropostaItem1}
\end{table}

\end{document}

Addendum with two alternative specifications of some or all of the tabular material:

  • If you're looking to give your table an cleaner and simpler, i.e., less turgid, look, don't use bold. I strongly suggest you (a) get rid of the bold font weights in the header cells and (b) dispense with the \multirow directives as well.

  • Using \adjustbox to make the tabular-like material fit inside the width of the textblock "works", but only in an extremely narrow sense. What's wrong with the \adjustbox approach, from a typographic experience? Most importantly, you run the serious risk of reducing the font size so much as to make the table well-nigh illegible. Thus, instead of taking the mechanical approach, think about (a) using automatic line breaks and text wrapping in some of the columns, (b) reducing the amount of intercolumn whitespace, and (c) shortening the labels used in the header row. The second table below implements all three ideas. I hope you will agree that the result is far more readable than the compressed table above it is.

enter image description here

\documentclass[portuguese]{article}
\usepackage{lmodern,babel,adjustbox,booktabs,
            tabularx,ragged2e,xcolor}
\newcolumntype{L}{>{\RaggedRight\hangindent=1em \hangafter=1 \arraybackslash}X}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\begin{document}

\begin{table}[!htbp]

\textcolor{red}{A\@. tabular environment, compressed}

\adjustbox{max width=\textwidth}{%
\begin{tabular}{@{}l*{6}{c}@{}}
\toprule
Sub-Item & Projeto & Processos & \multicolumn{2}{c}{Valor individual por} & Quantidade & Total \\ 
\cmidrule(lr){4-5}
 &  &  & Projeto & Processo \\ 
\midrule
Serviço de medição & 1 & 4 &  &  & 2 &  \\ %%\midrule
Serviço de apoio a projeto & 1 & 4 &  &  & 1 &  \\ 
\midrule
\multicolumn{6}{r}{Valor total} &  \\ 
\bottomrule
\end{tabular}}

\bigskip
\textcolor{red}{B\@. tabularx environment, uncompressed}

\setlength\tabcolsep{3pt} % default is 6pt
\begin{tabularx}{\textwidth}{@{}L*{6}{c}@{}}
\toprule
Sub-Item & Projeto & Processos & \multicolumn{2}{c}{Valor individual por} & Quant. & Total \\ 
\cmidrule(lr){4-5}
 &  &  & Projeto & Processo \\ 
\midrule
Serviço de medição & 1 & 4 &  &  & 2 &  \\ %%\midrule
Serviço de apoio a projeto & 1 & 4 &  &  & 1 &  \\ 
\midrule
\multicolumn{6}{r}{Valor total} &  \\ 
\bottomrule
\end{tabularx}

\end{table}

\end{document}
| improve this answer | |
  • 2
    In my opinion, a warning "Is this really something you want to do?" of some kind would improve this answer. See David Carlisle's comment. – Runar Sep 12 '16 at 17:04
  • @RunarTrollet - Thanks for this. I've added a third example: a tabularx environment that manages to typeset the table without resorting to compression. – Mico Sep 12 '16 at 19:17

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.