TransWikia.com

How to split cell text into multiline in table?

TeX - LaTeX Asked by typos on February 11, 2021

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 
hlineend{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?

4 Answers

Package tabularx is your friend:

enter image description here

documentclass[twocolumn]{paper}
usepackage{tabularx}
    newcolumntype{L}{>{raggedrightarraybackslash}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}

Answered by Zarko on February 11, 2021

If you want 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]
centeringrenewcommandcellalign{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
hlineend{tabular}
end{table}

end{document} 

enter image description here

Answered by Bernard on February 11, 2021

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

Answered by Wiebke on February 11, 2021

I found the answer in the link below: https://pt.overleaf.com/learn/latex/Tables I hope it helps!

Answered by Shabnam on February 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP