[Tex/LaTex] Automatic line-breaks in a table

formattingline-breakingtables

I need a short table that I have to add to my document. I'm afraid, LaTeX does not seem to break the lines at the spaces. My table currently looks like that:

\begin{table}[!ht]
    \centering
    \begin{tabular}{|c|c|c|}\hline
        \textbf{Column 1} & \textbf{Column 2} & \textbf{Column 3}  \\\hline
        This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line  \\\hline
        This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line  \\\hline
        This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line  \\\hline
    \end{tabular}
    \caption{This is the caption}
\end{table}

Does anybody know a quick way to fix this?

Best Answer

I suggest you use a tabularx environment, with its width set to \textwidth, and use a modified version of the X column type so that the columns' contents are centered rather than fully justified.

enter image description here

\documentclass{article}
\usepackage{tabularx,ragged2e}
\newcolumntype{C}{>{\Centering\arraybackslash}X} % centered "X" column
\begin{document} 

\begin{table}[!ht]
\setlength\extrarowheight{2pt} % for a bit of visual "breathing space"
\begin{tabularx}{\textwidth}{|C|C|C|}
\hline
\textbf{Column 1} & \textbf{Column 2} & \textbf{Column 3}  \\\hline
This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line  \\
\hline
This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line  \\
\hline
This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line & This is a very long text which has to be broken into the next line  \\
\hline
\end{tabularx}
\caption{This is the caption}
\end{table}
\end{document}