7

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?

CarLaTeX
  • 62,716
Ferit
  • 371
  • 3
    |c| should be |p{6cm}|, i.e. use the p columntype, which requires some length in addition. Please note that this won't be centered any longer this way. Replace the 6cm value by some length of your own choice –  Jul 11 '16 at 10:37
  • 1
    For centering p{...} column type you need to load array package and than for column type write: >{\centering\arraybackslash}p{<desired width>}. Maybe use of tabularx would be of interest: it automatically determine column width for given width of table. – Zarko Jul 11 '16 at 10:52

3 Answers3

11

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}
Mico
  • 506,678
4

The makecell package; with its thead and \makecell commands, is made for that:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier, erewhon}
\usepackage{geometry} \usepackage{array, makecell}%
\setcellgapes{4pt}

\begin{document}

\begin{table}[!ht]
    \centering\makegapedcells
    \begin{tabular}{|c|c|c|}\hline
        \textbf{Column 1} & \textbf{Column 2} & \textbf{Column 3} \\
        \hline
        \makecell{This is a very long text\\ which has to be broken\\ into the next line} & \makecell{This is a very long text\\ which has to be broken\\ into the next line} & \makecell{This is a very long text\\ which has to be broken\\ into the next line} \\
        \hline
        \makecell{This is a very long text\\ which has to be broken\\ into the next line} &\makecell{This is a very long text\\ which has to be broken\\ into the next line} & \makecell{This is a very long text\\ which has to be broken\\ into the next line} \\
        \hline
        \makecell{This is a very long text\\ which has to be broken\\ into the next line} &\makecell{This is a very long text\\ which has to be broken\\ into the next line} & \makecell{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}

\end{document} 

enter image description here

Bernard
  • 271,350
1

Although a bit late I guess, but I currently had the same problem and solved it thanks to a ShareLaTeX example.

Here is a short example which worked for me:

\documentclass{article}

%-----------------PREAMBLE-START

\usepackage[utf8]{inputenc}

\usepackage{tabu}

%-----------------PREAMBLE-END

\begin{document}

\listoftables

\newpage

    %Example of table with fixed length (tabu package required)
    \begin{table}[htbp]
    \caption{Advantages and drawbacks for using an commercial audio cable}
    \begin{tabu} to 1\textwidth { | X[l] | X[l] | }
       \hline
       \textbf{Advantages} & \textbf{Drawbacks} \\
       \hline
       no annoying sound anymore & signal transmission not anymore on 'real' material as with piezo-transducers \\
        \hline
        fast and easy rebuild of replica  & no control of build-in DAC at laptop's jack output, like sampling rate\\
        \hline
        replaces signal and noise generators a input equipment & not enough control when converting data to audio file \\
        \hline
        can still be used with piezo-transducers & annoying sound if again used with piezo-transducers \\
        \hline
   \end{tabu}
   \label{tab:audio}
   \end{table}

\end{document}

Here is the link to the example on my ShareLaTeX project space: https://www.sharelatex.com/project/58a0600c1eeba6d05d1c28aa

Or here is how it looks like for me:

this worked for me, I think it is important that not all rows have two lines, otherwise the table will look strange

CarLaTeX
  • 62,716