0

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}
  • 1
    First: Are you really trying to typeset long strings of A's? The reason your tabularx environment currently exceeds the width of the text block by far is that LaTeX -- quite justifiably and unsurprisingly, in my opinion -- has no rules whatsoever for hyphenating strings such as AAAAAAAAAAAAAAAAAAAAAAAAA or BBBBBBBBBBBBB. Once you input shorter words -- especially if they're recognizable as belonging to a real language -- you'll find that LaTeX's line-breaking capabilities are pretty good. – Mico Jun 7 '15 at 19:18
  • 1
    Remove the tabular\ environment? tabularx` is enough, and replace the tabularx preamble with {XXX}.. – Bernard Jun 7 '15 at 19:18
  • @bernard - Removing the "inner" tabular environment won't help the OP much (though I agree it's probably there by mistake). – Mico Jun 7 '15 at 19:20
  • 1
    Also, at least one of the columns in a tabularx environment must be an X. – cfr Jun 7 '15 at 19:22
  • 1
    argh my mistake. I somehow thought I had to use the tabular environment. stupid of me and the X too... That comes around when you copy paste the example code from some sites explaining booktabs. yeah some words could be really long. heck it is the German language, we have some crazy long words :D but thanks I am satisfied, my initial problem with linewidth is solved. – sceiler Jun 7 '15 at 19:37
4

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}
| improve this answer | |

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.