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 had gone thro this question and still in my case, i have the overlapping text in the table

Here is my MWE

\documentclass[a4paper,10pt]{book}
\usepackage{pdflscape}    % to change certain pages to landscape mode
\usepackage{longtable}  % for tables spanning multiple pages
\begin{document}

This is my example


\begin{landscape}
 \begin{tabular}{|p{5cm}|p{2cm}|p{2cm}|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}
\hline 
ABCD EFGH  ABCD EFGH  ABCD EFGH ABCD EFGH  ABCD EFGH  & H & H & H & H & OCH2-CH=C(CH3)CH2CH2CH=C(CH3)2 & H  \tabularnewline
\hline 
\end{tabular}
\newline
\end{landscape}
\end{document}

I can not expand / compress the column size.

The question is how to fix the overflowing text in 6th column. Whereas column 1 fits properly.

Should i manually hyphenate the text to fit into the desired cell width?

share|improve this question
    
I think that the problem is related to the hyphenation. As TeX doesn't know how to split that long string. If you hyphenate it, then it doesn't overflow. –  adn Mar 20 '12 at 7:17
add comment

1 Answer

up vote 7 down vote accepted

enter image description here

This version switches category codes, so can not be used in the argument to another command, but works OK here I think. The penalty0 allows breaks at those points, if some are more desirable than others you could give the desirable ones negative penalties and the undesirable ones positive penalties.

\documentclass[a4paper,10pt]{book}
\usepackage{pdflscape}    % to change certain pages to landscape mode
\usepackage{longtable}  % for tables spanning multiple pages
\usepackage{array}
\newcolumntype{P}[1]{>{\raggedright\arraybackslash\hspace{0pt}\mybreaks}p{#1}}

\begingroup
\catcode`=\active
\catcode`-\active
\catcode`)\active
\gdef\mybreaks{%
\catcode`=\active\edef={\string=\penalty0 }%
\catcode`)\active\edef){\string)\penalty0 }%
\catcode`-\active\edef-{\string-\penalty0 }}
\endgroup
\begin{document}

This is my example


\begin{landscape}
 \begin{tabular}{|P{5cm}|P{2cm}|P{2cm}|P{2cm}|P{2cm}|P{2cm}|P{2cm}|}
\hline 
ABCD EFGH  ABCD EFGH  ABCD EFGH ABCD EFGH  ABCD EFGH  & H & H & H & H & OCH2-CH=C(CH3)CH2CH2CH=C(CH3)2 & H  \tabularnewline
\hline 
\end{tabular}
\newline
\end{landscape}
\end{document}
share|improve this answer
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.