Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I’m trying to fit some text in a table, but the text keeps running through the table cells. I’ve already specified a fixed column width for the text using the p{} command. Initially, this worked, but then I also needed some forced linebreaks and once I implemented that using a new command, the p{} was overruled somehow, and the text runs through the cells again. Can someone help me fix this problem? The code I used looks something like this:

    \begin{landscape}

    \newcommand{\cellenter}[2][l]{%

    \begin{tabular}[#1]{@{}l@{}}#2\end{tabular}}
    \begin{table}[t]\footnotesize 
    \caption{Caption}\label{table:table_cable_lay_2}
      \begin{tabular*}{1.4\textwidth}{| p{0.32\textwidth} | p{0.32\textwidth} | p{0.32\textwidth} | p{0.32\textwidth} |}
          \hline\hline
          \textbf{Scenarios} &\textbf{Advantages} &\textbf{Disadvantages} &\textbf{Remarks} \\ [0.5ex]
          \hline

          \textit{Title of the cell} This text remains within the cell width because there are no forced linebreaks in this cell.

          &\cellenter[t]{+ Advantage 1 \\ + Advantage 2}

          &\cellenter[t]{- Disadvantage 1 \\ - Disadvantage 2}

          &\cellenter[t]{ There is some text here that runs through the cell width. \\ There is some text here that runs through the cell width.} \\[1ex]
          \hline
      \end{tabular*}
    \end{table}
    \end{landscape}

The pdf output that I get looks like this: table 1

share|improve this question
1  
Please make edit that into a complete document so we can run it locally, also you can upload the image but remove the ! so that it becomes a link rather than showing the image inline. Someone with edit privileges will put it back –  David Carlisle Oct 11 '12 at 14:13
    
Welcome to TeX.SE. –  Peter Grill Oct 11 '12 at 16:26
add comment

1 Answer

It is not clear what the intention of your \cellcenter is but it puts all its content into an inner tabular l column. l columns never linebreak they are always set to their natural width as a single line.

You can use a blank line to start a new paragraph within the table cells or \newline if you want a forced break without a new paragraph.


enter image description here

I fixed a few other things in your markup at the same time: (Using math mode to get matching +/ and using tabular rather than tabular* as you had no stretching between columns)

\documentclass{article}
\usepackage{pdflscape}

\begin{document}

   \begin{landscape}

    \begin{table}[t]\footnotesize 
    \caption{Caption}\label{table:table_cable_lay_2}
      \begin{tabular}{| p{0.32\textwidth} | p{0.32\textwidth} | p{0.32\textwidth} | p{0.32\textwidth} |}
          \hline\hline
          \textbf{Scenarios} &\textbf{Advantages} &\textbf{Disadvantages} &\textbf{Remarks} \\ [0.5ex]
          \hline

          \textit{Title of the cell} This text remains within the cell width because there are no forced linebreaks in this cell.

          &$+$ Advantage 1 \par  $+$ Advantage 2

          &$-$ Disadvantage 1 \par $-$ Disadvantage 2

          &There is some text here that runs through the cell width. \par There is some text here that runs through the cell width. \\[1ex]
          \hline
      \end{tabular}
    \end{table}
    \end{landscape}

\end{document}

To control alignment within the cells it is easiest to add

 \usepackage{array}

then you can inject \raggedright into each entry. If I understand your comment correctly you could use

\begin{tabular}{|%
>{\raggedright\arraybackslash}p{0.32\textwidth}|%
p{0.32\textwidth}|%
p{0.32\textwidth}|%
>{\raggedright\arraybackslash}p{0.32\textwidth}|%
}

or did you mean:

\begin{tabular}{|%
p{0.32\textwidth}|%
>{\raggedright\arraybackslash}p{0.32\textwidth}|%
>{\raggedright\arraybackslash}p{0.32\textwidth}|%
p{0.32\textwidth}|%
}
share|improve this answer
    
The text now runs inside the table cells, which I wanted. But the second column is now aligned both left and right, whereas I only want it to be aligned left. How can I fix this? Thanks for helping out. –  Wouter Oct 11 '12 at 15:14
    
I've used your code for the second column in the following manner. \begin{tabular}{| p{0.32\textwidth} | *4{>{\raggedright\arraybackslash}p{0.32\textwidth} | } p{0.32\textwidth} | p{0.32\textwidth} |} The first column is now fully aligned (left and right), whereas columns 2,3,4 are aligned left only. How do I make sure only columns 2 and 3 are aligned left? I would like to have column 1 and 4 to be fully aligned (both left and right). –  Wouter Oct 11 '12 at 15:50
    
No the *4 meant 4 columns, I meant use that line for the entire tabular specification. so that has 7 columns. forget the *4 if all columns are not teh same I'll update the answer as code formatting in comments is rubbish –  David Carlisle Oct 11 '12 at 16:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.