0

I have the following code:

    \documentclass[a4paper,12pt,fleqn,twoside,openright]{book}
    \usepackage{caption}

    \begin{document}

    \begin{table}
    \centering
    \caption{table}
    \label{table:table}
    \begin{tabular}{c|c|c}
    \textbf{1}     & \textbf{2} & \textbf{3}    \\ \hline \hline
    A     &   blahblahblahblahblahblahblah blahblahblahblahblahblahblahblah   &   blahblahblahblah blahblahblahblahblahblah      \\ \hline
    B            &   b   &   b     \\ \hline
    C      &     c     &    c   
    \end{tabular}
    \end{table}
\end{document}

The table is not contained between the page margins. I want it to automatically adapt to the width of the page, or, alternativelly, I want to be able to fix the width of each column manually, in such a way that the text in each cell fill several cell lines if necessary.

I already checked this and this but I think no one gives an explanation for what I am looking for.

2
  • 1
    You write that your objective is "I want [the table] to automatically adapt to the width of the page". In table design, there are lots of parameters that can be tweaked. In consequence, there are potentially lots of degrees of freedom. However, there are always lots of constraints that must be met as well. Two such constraints might be "all columns must be equally wide" and "automatic text wrapping not allowed in some columns". Your stated objective doesn't list any constraints other than that the overall width of the table must not exceed \textwidth. Please state a few more.
    – Mico
    Feb 19, 2020 at 16:55
  • Take a look at the tabularx package
    – leandriis
    Feb 19, 2020 at 16:55

3 Answers 3

1

Welcome to TeX.SE!

What you are trying to do is often achieved with the tabularx package. It provides an X column that takes an automatically calculated width. The width of each of the X columns is calculated from the argument to the tabularx environment, minus the space reservations for other, non-X columns, and divided equally between the X columns. You can further modify the X columns with the > syntax before the column specifier, e.g. by centring the column: >{\centering}X. Note that if the last column is also modified in that way, it needs to include \arraybackslash for reasons explained, e.g., here, or in the tabularx manual.

I took the liberty to load the booktabs package as well and inserted its nice \midrule to replace the vertical bars from your original table.

\documentclass[a4paper,12pt,fleqn,twoside,openright]{book}
\usepackage{caption}

\usepackage{tabularx,booktabs}

\begin{document}

    \begin{table}
    \centering
    \caption{table}
    \label{table:table}
    \begin{tabularx}{\textwidth}{>{\centering}X>{\centering}X>{\centering\arraybackslash}X}
    \toprule
    \textbf{1}     & \textbf{2} & \textbf{3}    \\ \midrule
    A     &   A very long line that automatically breaks  &   Somtimes it can be a good idea to consider having less characters in a table cell if at all possible\\ 
    B            &   b   &   b     \\ 
    C      &     c     &    c   \\
    \bottomrule
    \end{tabularx}
    \end{table}
\end{document}

As a further modification, your column specification could also look like:

\begin{tabularx}{\textwidth}{l >{\centering}X>{\centering\arraybackslash}X}

as the first column does not include any multi-line text. In this case, the width for the two X columns will be

(\textwidth minus 6\tabcolsep minus (width of first l column)) divided by 2

If you wanted to have a fixed width for one of the columns, you could use the usual p{width}, e.g.

\begin{tabularx}{\textwidth}{p{1cm} >{\centering}X>{\centering\arraybackslash}X}

and the width for the remaining X columns would update according to the remaining space.

0

Consider p{0.4\textwidth} option instead of plain |c|. You might also find booktab package helpful. Your table will look nicer.

    \documentclass[a4paper,12pt,fleqn,twoside,openright]{book}
    \usepackage{caption}

    \begin{document}

    \begin{table}
    \centering
    \caption{table}
    \label{table:table}
    \begin{tabular}{c|p{0.4\textwidth}|p{0.4\textwidth}}
    \textbf{1}     & \textbf{2} & \textbf{3}    \\ \hline \hline
    A     &   blahblahblahblahblahblahblah blahblahblahblahblahblahblahblah   &   blahblahblahblah blahblahblahblahblahblah      \\ \hline
    B            &   b   &   b     \\ \hline
    C      &     c     &    c   
    \end{tabular}
    \end{table}
\end{document}

enter image description here

1
  • Obviously you can change 0.4\textlength with whatever you want. Feb 19, 2020 at 16:46
0

tabulary is similar to tabularx but tries to balance the cells vertically, that is, cells with more text will be automatically wider. You may also consider using booktabs for a nicer look.

\documentclass[a4paper,12pt,fleqn,twoside,openright]{book}
\usepackage{caption,tabulary}

\begin{document}

\begin{table}
\centering
\caption{table}
\label{table:table}
\begin{tabulary}{\textwidth}{c|C|C}
\textbf{1} & \textbf{2}                                                                & \textbf{3}    \\ \hline \hline
A          & blahbla hbl ahbl ahbl ahb lahblah bla hblahb lahb la hblah blahb lahb lah & blah bla hblah blah bla hbla h blah blahbl ahblah      \\ \hline
B          & b                                                                         & b     \\ \hline
C          & c                                                                         & c
\end{tabulary}
\end{table}

\end{document}

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.