6

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?

  • 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 – user31729 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
8

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}
  • How do I make a narrow column at the very left containing say serial numbers 1, 2, 3, ...? And make them align centered vertically? – Subhajit Paul Aug 1 at 15:02
  • @SubhajitPaul - Did you see the posting How can I make a column that the number is counted automatically?? – Mico Aug 1 at 15:04
  • I added the lines \newcounter{rownum} and \newcolumntype{N}{>{\refstepcounter{rownum}\therownum}l} in my preamble and added an extra empty column in the body. Did not work. Do I need to change to longtable? – Subhajit Paul Aug 1 at 15:23
  • @SubhajitPaul - Sorry, but "Did not work" is not an actionable piece of information. (My divination skills are utterly worthless, incidentally.) You haven't provided nearly enough information so far to allow a confident diagnosis of the situation, let alone suggest a cure. Please post a new query, in which you show exactly that you're doing and where things aren't working. – Mico Aug 1 at 15:27
  • Sorry about that part. I only have a blank left column which is of the same width as of the others. What I have now is an exact copy of this answer of yours (with generic modifications only in the table body) and I have added the lines \newcounter{rownum} and \newcolumntype{N}{>{\refstepcounter{rownum}\therownum}l} from your suggested answer. – Subhajit Paul Aug 1 at 15:33
3

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

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

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.