3

I have the following table:

MWE

\documentclass{article}
    \usepackage{booktabs}

\begin{document}
    \begin{table}[htbp]
        \centering
        \caption{Add caption}
        \begin{tabular}{lcccc}
            \toprule
            Component & Var & Dif & Pr & Cum \\
            \midrule
            \multicolumn{5}{r}{Panel A:  Lorem ipsum} \\
            Pc1   & 25 & 16 & 0.4 & 0.54569845 \\
            Pc2   & 54 & 12 & 0.12 & 13.1234487 \\
            \multicolumn{5}{r}{Panel B: Lorem ipsum dolor sit amet, consectetur adipiscing elit.} \\
            Pc1   & 25 & 16 & 0.4 & 0.541687846 \\
            Pc2   & 54 & 12 & 0.12 & 13.31584 \\
            \multicolumn{5}{r}{Panel C: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec congue eros nunc.} \\
            Pc1   & 25 & 16 & 0.4 & 0.5448313 \\
            Pc2   & 54 & 12 & 0.12 & 13.168468 \\
            \bottomrule
        \end{tabular}%
        \label{tab:addlabel}%
    \end{table}%
\end{document}

However the last column appears to be the broadest. I tried using tabularx package with the command \newcolumntype{C}{>{\centering\arraybackslash}p{2em}} to make all the columns equally sized, but nothing changes. How can I make the width of all the columns the same?

  • what about \begin{tabularx}{\linewidth}{XXXXX}? Otherwise, since you know your total length, and the number of columns, you case simply define \begin{tabular}{*{5}{p{.2\linewidth}}} (i.e. 5 times p{.2\linewidth}, where .2 = 1/5)... – ebosi Jun 27 '16 at 14:14
2

At first sight, you could have to possibilities to achieve what you want:

  1. You know the total width of your table and the number of columns: you can easily guess the width of each column, and thus create column with adequate fixed width;
  2. Or you give the total width of your table, and you let tabularx compute the width of each column ('cuz you know, all the math stuff...).

The former solution need no additional package, when the latter is more robust (since you do not have to update the width if you add/remove a column, or if you change the total width).

Disclaimer: If you directly want the solution, see the TL;DR at the bottom of this answer (3rd MWE)]


Solution 1: without any package

[disclaimer: see output below, because it is not that easy!]

\documentclass{article}
    \usepackage{array}          %always good to load when you create tables
    \usepackage{booktabs}
    %\usepackage{showframe}     %if you want to show the text area borders

\begin{document}
    \begin{table}[htbp]
        \centering
        \caption{Solution using no extra package}
        \begin{tabular}{*{5}{p{.2\linewidth}}}
            \toprule
                Component & Var & Dif & Pr & Cum            \\
            \midrule
                \multicolumn{5}{r}{Panel A:  Lorem ipsum}   \\
                Pc1   & 25 & 16 & 0.4  & 0.54569845         \\
                Pc2   & 54 & 12 & 0.12 & 13.1234487         \\
                \multicolumn{5}{r}{Panel B: Lorem ipsum dolor sit amet, consectetur adipiscing elit.} \\
                Pc1   & 25 & 16 & 0.4  & 0.541687846        \\
                Pc2   & 54 & 12 & 0.12 & 13.31584           \\
                \multicolumn{5}{r}{Panel C: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec congue eros nunc.} \\
                Pc1   & 25 & 16 & 0.4  & 0.5448313          \\
                Pc2   & 54 & 12 & 0.12 & 13.168468          \\
            \bottomrule
        \end{tabular}%
        \label{tab:label1}%
    \end{table}%
\end{document}

(note that, instead of writing \begin{tabular}{p{.2\linewidth}p{.2\linewidth}p{.2\linewidth}p{.2\linewidth}p{.2\linewidth}}, you can use -- as in this MWE -- the form *{<number-of-times-you-repeat-the-column>}{<column-type-definition>})


Solution2: with tabularx

[disclaimer: see output below, because it is not that easy!]

\documentclass{article}
    \usepackage{array}          %always good to load when you create tables
    \usepackage{booktabs}
    \usepackage{tabularx}
    %\usepackage{showframe}     %if you want to show the text area borders

\begin{document}
    \begin{table}[htbp]
        \centering
        \caption{Solution with \texttt{tabularx}}
        \begin{tabularx}{\linewidth}{XXXXX}                 % or *{5}{X}, it's the same
            \toprule
                Component & Var & Dif & Pr & Cum            \\
            \midrule
                \multicolumn{5}{r}{Panel A:  Lorem ipsum}   \\
                Pc1   & 25 & 16 & 0.4  & 0.54569845         \\
                Pc2   & 54 & 12 & 0.12 & 13.1234487         \\
                \multicolumn{5}{r}{Panel B: Lorem ipsum dolor sit amet, consectetur adipiscing elit.} \\
                Pc1   & 25 & 16 & 0.4  & 0.541687846        \\
                Pc2   & 54 & 12 & 0.12 & 13.31584           \\
                \multicolumn{5}{r}{Panel C: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec congue eros nunc.} \\
                Pc1   & 25 & 16 & 0.4  & 0.5448313          \\
                Pc2   & 54 & 12 & 0.12 & 13.168468          \\
            \bottomrule
        \end{tabularx}%
        \label{tab:label2}%
    \end{table}%
\end{document}

Result

(we used \usepackage{showframe} in order to display the margin limits)

MWEs 1 and 2

You can see that the fist solution is not satisfactory, since we did not considered all margins (e.g. between columns).
The second solution is not good neither, since the lines runs up in the margin...

so what should I do?!

The solution is to use tabularx, but to create a multicol cell that can be split into several lines: \multicolumn{5}{@{}p{\linewidth}@{}}{\centering <cell-content>}
(The two @{} are there to remove side margins, so that the actual cell width is really \linewidth.)


TL;DR: the real solution

\documentclass{article}
    \usepackage{array}          %always good to load when you create tables
    \usepackage{booktabs}
    \usepackage{tabularx}
    %\usepackage{showframe}     %if you want to show the text area borders

\begin{document}
    \begin{table}[htbp]
        \centering
        \caption{Solution with \texttt{tabularx}}
        \begin{tabularx}{\linewidth}{XXXXX}                 % or *{5}{X}, it's the same
            \toprule
                Component & Var & Dif & Pr & Cum            \\
            \midrule
                \multicolumn{5}{@{}p{\linewidth}@{}}{Panel A:  Lorem ipsum}   \\
                Pc1   & 25 & 16 & 0.4  & 0.54569845         \\
                Pc2   & 54 & 12 & 0.12 & 13.1234487         \\
                \multicolumn{5}{@{}p{\linewidth}@{}}{Panel B: Lorem ipsum dolor sit amet, consectetur adipiscing elit.} \\
                Pc1   & 25 & 16 & 0.4  & 0.541687846        \\
                Pc2   & 54 & 12 & 0.12 & 13.31584           \\
                \multicolumn{5}{@{}p{\linewidth}@{}}{Panel C: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec congue eros nunc.} \\
                Pc1   & 25 & 16 & 0.4  & 0.5448313          \\
                Pc2   & 54 & 12 & 0.12 & 13.168468          \\
            \bottomrule
        \end{tabularx}%
        \label{tab:label3}%
    \end{table}%
\end{document}

enter image description here

  • PS: for further refinement, you can have a look at Aligning numbers by decimal points in table columns – ebosi Jun 27 '16 at 15:20
  • Thank you for your reply. I have adjusted my inputs. Would you mind changing the output as this is a part of the master thesis and there are some problems with plagiarism checks due to the table contents? – glarys Jun 28 '16 at 20:14
  • @glarys: it's done! – ebosi Jun 29 '16 at 7:32

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.