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 have a table with long heading names. I want to fix the the width of the columns. When I do so the text flows out if the column. Any suggestion about fixing it.

Here is the example

\begin{table*}[ht]
\begin{tabular}{|l|r|r|r|r|r|}
\hline
Image & Score  & here is a lot of text & agian a lot &a lot and a lot& and the same here\\ \hline
a \(left\) & 1 & 1 & 1 & 1 & 1  \\ \hline
b \(right\)&  1 &1 & 1 & 1 & 1  \\ 
\end{tabular}

\end{table*}
share|improve this question
1  
Welcome to TeX.SE. You can use the p{<width>} column type, and use a \parbox for the table content. It would be helpful if you composed a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages as that will help to ensure that the solution actually solves your specific case. –  Peter Grill Apr 6 '12 at 19:00
    
@peter, thanks , this \parbox will be used with every cell's content? –  Shan Apr 6 '12 at 19:01
    
Better if you compose a small example, and then we can show you how to use it. –  Peter Grill Apr 6 '12 at 19:02
    
@peter, here is an example –  Shan Apr 6 '12 at 19:12

1 Answer 1

up vote 7 down vote accepted

Here is an example of using the p{} column type, or wrapping the contents in a \parbox:

enter image description here

For your specific example, since you want the data columns aligned as per the table heading you could use something like:

\newcommand*{\TitleParbox}[1]{\parbox[c]{1.75cm}{\raggedright #1}}%
\begin{tabular}{|l|r|r|r|r|r|}
    \hline
    Image & Score  & \parbox[c]{1.5cm}{\raggedright here is a lot of text} & \TitleParbox{again a lot} &\TitleParbox{a lot and a lot}& \TitleParbox{and the same here}\\ \hline
    a \(left\) & 1 & 1 & 1 & 1 & 1  \\ \hline
    b \(right\)&  1 &1 & 1 & 1 & 1  \\  \hline
\end{tabular}

which yields:

enter image description here

Notes:

  • The showframe package was used to show the margins on the page.

Code:

\documentclass{article}
\usepackage{showframe}

\newcommand*{\Title}{A very long table heading}%
\newcommand*{\TitleInParbox}{\parbox[c]{0.3\linewidth}{\Title}}%

\begin{document}
\section{Tabular with left alignment}
\noindent
\begin{tabular}{lll}
\Title & \Title & \Title
\end{tabular}

\section{Using the p\{\} column type:}
\noindent
\begin{tabular}{p{0.3\linewidth} p{0.3\linewidth} p{0.3\linewidth}}
\Title & \Title & \Title
\end{tabular}

\section{Using a parbox:}
\noindent
\begin{tabular}{lll}
\TitleInParbox & \TitleInParbox & \TitleInParbox
\end{tabular}
\end{document}
share|improve this answer

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.