I normally use the table
and tabu
environment to create my tables. I am new to LaTeX and after I while I realized tables look nicer when they use the whole page width rather than being just centered. I first tried \begin{tabu} to \textwidth {ccccccc}
with the tabu
environment as this is my preferred env. But this seemed to have no effect at all so I tried the adjustbox
package but the problem is that even if I just define width=\textwidth
and remove totalheight=\textheight
and keepaspectratio
from the parameters the table still gets scaled in both directions. I am not sure if you can use adjustbox
really only to expand the table width? Perfect would be to really just use the tabu
environment if that is somehow possible.
After doing a lot of google searches it looks like that the tabularx
environment is the simplest to use when tables should be spanned across the page width. My problem is, however, that I don't even get a simple tabularx
example to run. I have no clue why it does not work. The package as you will see is included. That means I wasn't able to try the following command to expand the table to page width: \begin{tabularx}{\textwidth}{ccccccc}
Here is my working example. I uncommented the tabularx
example otherwise it would not compile. I know that I did not remove fontsize
and and \changefont
to keep the example as simple as possible but I definitely need the table to work like this that is why I kept it in:
\documentclass[10pt,DIV=12,a4paper,numbers=noenddot]{scrreprt}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{appendix}
\usepackage{tabu}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{adjustbox}
\usepackage{tabularx}
\usepackage{caption}
\usepackage{anyfontsize}
\newcommand{\changefont}[3]{\fontfamily{#1} \fontseries{#2} \fontshape{#3} \selectfont}
\begin{document}
% TABLE 1
\begin{table}
\centering
\changefont{phv}{m}{n} % Change font to Helvetica
\fontsize{6}{9}\selectfont{ % Change font size to size 6 and line space to 9
% \begin{adjustbox}{width=\textwidth,totalheight=\textheight,keepaspectratio}
\begin{adjustbox}{width=\textwidth}
\begin{tabu} to \textwidth {lrrrrrr}
\toprule
\rowfont{\bfseries\itshape}
First & Second & Third & \multicolumn{2}{r}{MulticColumn} & \multicolumn{2}{r}{MultiColumn} \\
& & & \textit{HB} & \textit{HC} & \textit{HB} & \textit{HC} \\
\midrule
AXXXXXX & 0.001 & 0.051 & 1.07 & -1.10 & -0.32 & -0.80 \\
AYYYYYY & 0.308 & 0.123 & -1.80 & -2.35 & -0.55 & -0.70 \\
AZZZZZZ & 0.227 & 0.432 & -1.99 & 1.12 & -0.88 & 0.14 \\
\bottomrule
\end{tabu}
\end{adjustbox}
}
\caption{This is the caption}
\label{tab:label}
\end{table}
% TABLE 2
% \begin{table}
% \begin{tabularx}{ccccccc}
% \begin{tabularx}{\textwidth}{ccccccc}
% \toprule
% \rowfont{\bfseries\itshape}
% First & Second & Third & \multicolumn{2}{r}{MulticColumn} & \multicolumn{2}{r}{MultiColumn} \\
% & & & \textit{HB} & \textit{HC} & \textit{HB} & \textit{HC} \\
% \midrule
% AXXXXXX & 0.001 & 0.051 & 1.07 & -1.10 & -0.32 & -0.80 \\
% AYYYYYY & 0.308 & 0.123 & -1.80 & -2.35 & -0.55 & -0.70 \\
% AZZZZZZ & 0.227 & 0.432 & -1.99 & 1.12 & -0.88 & 0.14 \\
% \bottomrule
%
% \end{tabularx}
% \end{table}
\end{document}
Here is my second table which I want to squeeze to page width:
\begin{table}
\changefont{phv}{m}{n} % Change font to Helvetica
\fontsize{6}{9}\selectfont{ % Change font size to size 6 and line space to 9
\begin{tabu} to \textwidth {*13{X[c]}}
\toprule
& & A 1 & A 2 & A 3 & A 4 & A 5 & A 6 & A 7 & A 8 & A 9 & A 10 & A 11 \\
\midrule
\end{tabu}
}
\end{table}
Best Answer
Summarizing my comments into an answer: you say you tried
as header for the table but didn't get a table that had
\textwidth
as width. That is because ac
column gets the width of its contents. In order to stretch (or shrink) columns in a way so they fit\textwidth
tabu
(and alsotabularx
) needs the special column typeX
. Soshould give a table of the specified width. However, the contents won't be centered any more. This is easily fixed since
tabu
'sX
columns have an optional argument where you can specify the behaviour:A tip: if you have a repeated number of the same column specifications it is possible to declare them all at once using the syntax
*{<num>}{<spec>}
.*4c
would be equivalent tocccc
. It would be better to use braces here, though, (*{4}{c}
) so you don't forget them when you need them: if<num>
or<spec>
are longer than one token you have to enclose them:*{13}{X[c]}
.All in all this now leads to
Let's see if it works: