27

I am creating a two-column document using latex. I use the option \columnwidth to autofit a table to the column width, as shown in the following figure. The second column Value is too wide. How do I set equal width for two columns?


enter image description here


Here is my source code.

\begin{table}
    \centering
    \caption{Simulation parameters}
    \begin{tabular}{l|l}
        \hline
        Key                 & Value    \\
        \hline
        simulation duration & 12 hours \\
        update interval     & 1s       \\
        time-to-live        & 12 hours \\
        buffer size         & infinite \\
        message interval    & 20s      \\
        \hline
    \end{tabular}
\end{table}

\begin{table}
    \centering
    \caption{Simulation parameters}
    \begin{tabularx}{\columnwidth}{l|l}
        \hline
        Key                 & Value    \\
        \hline
        simulation duration & 12 hours \\
        update interval     & 1s       \\
        time-to-live        & 12 hours \\
        buffer size         & infinite \\
        message interval    & 20s      \\
        \hline
    \end{tabularx}
    \label{table: simulation parameters}
\end{table}
1
  • 3
    Do you want to fix the length of both columns to the same? You can use p{'width'} instead of l as the second tabular parameter to do this. If not, could you describe what you mean with "the second column is to wide"? I'm not exactly sure what's the problem in your first table.
    – Nijin22
    May 30, 2016 at 14:36

1 Answer 1

27

EDIT

@Mico pointed out a much better solution to this problem, as tabularx already comes with the column type X, which automatically does what you want:

\documentclass{article}

\usepackage{tabularx}

\begin{document}

\begin{table}
    \caption{Simulation parameters}
    \begin{tabularx}{\columnwidth}{X|X}
        \hline
        Key                 & Value    \\
        \hline
        simulation duration & 12 hours \\
        update interval     & 1s       \\
        time-to-live        & 12 hours \\
        buffer size         & infinite \\
        message interval    & 20s      \\
        \hline
    \end{tabularx}
    \label{table: simulation parameters}
\end{table}

\end{document}

As you already know the total width of your table, you can define the width of the columns as a fraction of \columnwidth

\documentclass{article}

\newlength\mylength
\setlength\mylength{\dimexpr.5\columnwidth-2\tabcolsep-0.5\arrayrulewidth\relax}

\begin{document}

\begin{table}
    %% \centering % not needed
    \caption{Simulation parameters}
    \begin{tabular}{p{\mylength}|p{\mylength}}
        \hline
        Key                 & Value    \\
        \hline
        simulation duration & 12 hours \\
        update interval     & 1s       \\
        time-to-live        & 12 hours \\
        buffer size         & infinite \\
        message interval    & 20s      \\
        \hline
    \end{tabular}
    \label{table: simulation parameters}
\end{table}

\end{document}

enter image description here

7
  • 3
    Since you're using a tabularx environment, you should write \begin{tabularx}{\columnwidth}{X|X}. (Note that with your method, the total width is \columnwidth+4\tabcolsep+\arrayrulewidth.) Separately, if the width is set to \columnwidth, the instruction \centering may be omitted.
    – Mico
    May 30, 2016 at 15:11
  • 1
    @Mico Thanks for the suggestion, I edited my answer. Jun 3, 2016 at 10:14
  • 1
    +1. I've taken the liberty of editing the code of your second example a bit, e.g., by performing the calculation of the p columns in the preamble and by using a tabular environment instead of a tabularx environment. (The facilities of the tabularx environment aren't utilized in the second example.)
    – Mico
    Jun 3, 2016 at 11:13
  • 1
    @Viesturs p is a column with specific width. Jan 25, 2018 at 12:21
  • 2
    @Viesturs Then you google and find answer like tex.stackexchange.com/a/199747/36296 Jan 25, 2018 at 12:25

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.