[Tex/LaTex] Make a linewidth table with booktabs

booktabstables

How do I create a table with booktabs which is \linewidth?

I tried different approaches among others to use tabularx environment or to put everything in a table environment but that did not change anything. See my MWE:

\documentclass[fontsize=12pt,a4paper,oneside, 
listof=totoc,                   % Tabellen- und Abbildungsverzeichnis ins Inhaltsverzeichnis
bibliography=totoc,             % Literaturverzeichnis ins Inhaltsverzeichnis aufnehmen
titlepage,                      % Titlepage-Umgebung statt \maketitle
headsepline,                    % horizontale Linie unter Kolumnentitel
%abstracton,                    % Überschrift beim Abstract einschalten, Abstract muss dazu in {abstract}-Umgebung stehen
DIV12,                          % auskommentieren, um den Seitenspiegel zu vergrößern
BCOR=0mm,                       % Bindekorrektur, die den Seitenspiegel um 6mm nach rechts verschiebt. geometry package überschreibt diesen Wert
]{scrreprt}

\usepackage{booktabs}
\usepackage{tabularx}

\begin{document}
\begin{table}[H]
\begin{tabularx}{\linewidth}{lll}
\begin{tabular}{lll}
\toprule
\textbf{AAAAAAAAAAAAAA} & \textbf{BBBBBBBBBBBBB} & \textbf{CCCCCCCCC} \\
\midrule
AAAAAAAAAAAAAAAAAAAAAAAAA & AAAAAAAAAAAAAAAAAAAAAAAAA & AAAAAAAAAAAAAAAAAAAAAAAAA  \\
AAAAAAAAAAAAAAAAAAAAAAAAA  & AAAAAAAAAAAAAAAAAAAAAAAAA & - \\ 
AAAAAAAAAAAAAAAAAAAAAAAAA  & AAAAAAAAAAAAAAAAAAAAAAAAA & AAAAAAAAAAAAAAAAAAAAAAAAA \\ 
\bottomrule
\end{tabular}
\end{tabularx}
\end{table}
\end{document}

Best Answer

Putting together some of the comments produces a table which fits within the text width although, as Mico noted, the contents of the cells spill over if they are left as long strings of characters TeX can't hyphenate.

Replacing the content with real words helps. The table fits the space. But you get lots of overfull and underfull boxes. This is because it is hard to line break narrow columns of text tidily. Adding \raggedright would help but using ragged2e may be a better option.

As Bernard noted, in your original code, you asked LaTeX to fit your entire table into the first cell of a tabular environment which was also meant to include another 2 columns (lll).

Also, an l column must fit into a single line. There is no line breaking. You can use p{<width>} for paragraph-type cells with line breaking.

For tabularx, however, at least one column must be X. If you want the columns to be of equal width, you can use 3 X-type columns, for example.

Putting these things together, I get:

tabular Kant

\documentclass[fontsize=12pt,a4paper,oneside,
listof=totoc,                   % Tabellen- und Abbildungsverzeichnis ins Inhaltsverzeichnis
bibliography=totoc,             % Literaturverzeichnis ins Inhaltsverzeichnis aufnehmen
titlepage,                      % Titlepage-Umgebung statt \maketitle
headsepline,                    % horizontale Linie unter Kolumnentitel
%abstracton,                    % Überschrift beim Abstract einschalten, Abstract muss dazu in {abstract}-Umgebung stehen
DIV12,                          % auskommentieren, um den Seitenspiegel zu vergrößern
BCOR=0mm,                       % Bindekorrektur, die den Seitenspiegel um 6mm nach rechts verschiebt. geometry package überschreibt diesen Wert
]{scrreprt}

\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{ragged2e}
\usepackage{kantlipsum}

\begin{document}
  \begin{table}[H]
    \begin{tabularx}{\linewidth}{*{3}{>{\RaggedRight\arraybackslash}X}}
        \toprule
        \textbf{Kant} & \textbf{More Kant} & \textbf{Further Kant} \\
        \midrule
        \kant[1] & \kant[2] & \kant[3]  \\
        \bottomrule
    \end{tabularx}
  \end{table}
\end{document}