5

I'm writing a paper using the ACM double column template, and I have a table which I want to fit to just one column, and in order to do so, I want to split the text inside the cells to multiple lines. I have the following code segment:

\documentclass{paper}
\begin{document}

\begin{table}[thb]
\centering
\begin{tabular}{|l|c|l|} \hline
\textbf{Col1} & \textbf{Col2} & \textbf{Col3} \\ \hline
This is a \\very long line \\of text & Short text & Another long \\line of text \\ \hline
$\pi$ & 1 in 5& Common in math\\ 
\hline\end{tabular}
\end{table}

\end{document}

But, what it gives me an output which is not quite right, as can be seen below.

enter image description here

First of all, the second part of the text of the last column is pushed into the first column. Then, also the last columns borders are not full. Any idea how to split a text to multiline in table cell?

7

Package tabularx is your friend:

enter image description here

\documentclass[twocolumn]{paper}
\usepackage{tabularx}
    \newcolumntype{L}{>{\raggedright\arraybackslash}X}

\begin{document}
    \begin{table}
    \centering
\begin{tabularx}{\linewidth}{|L|c|L|} 
    \hline
\textbf{Col1} & \textbf{Col2} & \textbf{Col3} \\ 
    \hline
This is a very long line of text & Short text & Another long line of text \\ 
    \hline
$\pi$ & 1 in 5& Common in math\\
    \hline
\end{tabularx}
    \end{table}
\end{document}
  • It is my understanding that the tabu package provides improvements with respect to tabularx, which may be interesting when typesetting long texts into tables, such as footnotes compatibility with hyperref and table use, etc. Maybe this is to be recommended as well. – Sergei Poulp Sep 24 '18 at 9:36
  • 1
    @SergeiPoulp, that was intention, but package is buggy and not maintained. with recent changes in the array (which is used in it) package it is not compatible with it anymopre, so it is not reliable. i would avoid it. – Zarko Sep 24 '18 at 9:39
6

Your wrong column alignment results from missing column delimiters (&) in your source code and can be fixed easily. This also fixes the broken vertical lines.

\begin{table}[thb]
    \centering
    \begin{tabular}{|l|c|l|}
        \hline
        \textbf{Col1}  & \textbf{Col2} & \textbf{Col3}  \\ \hline
        This is a      &               &    \\ % <===== note the empty cells in this line
        very long line &  Short text   & Another long   \\
        of text        &               &  line of text\\ \hline % <===== and in this
        $\pi$          &    1 in 5     & Common in math \\ \hline
    \end{tabular}
\end{table}

fixed columns A less manual way would be to use the p column type, but you have to specify the column width and LaTeX will do the linebreaks for you, but your last cell will probably also break:

\begin{table}[thb]
    \centering
    \begin{tabular}{|p{1.8cm}|c|p{2cm}|}
        \hline
        \textbf{Col1}                                                                & \textbf{Col2} & \textbf{Col3}                                                         \\ \hline
        This is a very long line of text &  Short text   & Another long line of text \\ \hline
        $\pi$                                                                        &    1 in 5     & Common in math                                                        \\ \hline
    \end{tabular}
\end{table}

tabular with {p} column type

Or you can use the multirow package. Load \usepackage{multirow} in your preamble {anywhere between \documentclass{paper} and \begin{document}, and then you can do:

\begin{table}[thb]
\centering
\begin{tabular}{|l|c|l|}
    \hline
    \textbf{Col1}                                                     & \textbf{Col2} & \textbf{Col3}                                            \\ \hline
    \multirow{3}{*}{\parbox{1.8cm}{This is a very long line of text}} &               & \multirow{3}{*}{\parbox{2cm}{Another long line of text}} \\
                                                                      &  Short text   &  \\
                                                                      &               &  \\ \hline
    $\pi$                                                             &    1 in 5     & Common in math                                           \\ \hline
\end{tabular}
\end{table}

Note that this aligns the columns vertically centered. The \parbox{length} defines when your text should be broken.

multirow

3

If you cant to control line breaks in cells, you can use the \makecell command from the homonymous package. In addition, it has tools to add some vertical padding to cells:

\documentclass{paper}

\usepackage{array, makecell} %

\begin{document}

\begin{table}[thb]
\centering\renewcommand\cellalign{lc}
\setcellgapes{3pt}\makegapedcells
\begin{tabular}{|l|c|l|} \hline
\textbf{Col1} & \textbf{Col2} & \textbf{Col3} \\ \hline
\makecell{This is a \\very long line \\of text} & Short text &\makecell{ Another long \\line of text} \\ \hline
$\pi$ & 1 in 5& Common in math\\
\hline\end{tabular}
\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.