How to split cell text into multiline in table?
Package tabularx
is your friend:
\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}
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]
\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}
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}
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}
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.