1

I have the following code:

\begin{landscape}

\setlength\tabcolsep{3pt} \begin{longtable}{m{0.5cm}|m{3cm}|m{3cm}|m{3cm}|m{9cm}} \hline \textbf{C1}&\textbf{Col2}&\textbf{Col3}&\textbf{Col4}&\textbf{Col5}\ \hline \endfirsthead \endfoot 1&Self-report&-&D_alcohol_consumption&\\hline 2&Self-report&-&Trastorno del pensamiento controlado&\\hline 3&Self-report&-&D-alcohol-consumption&\\hline \end{longtable}

\end{landscape}

It produces this table

enter image description here

I need to put very long variable names in the column "Col4" and many of them have underscores, like the first row. But it does not fit in the cell and extends to "Col5". What I expect is to automatically break the line like the second row.

I think it is something related the underscore, because in the third line I replaced them with hyphens and it is what I expected. But I need to use the underscores.

Any suggestion to keep the underscores?

Lev
  • 145

2 Answers2

1

Basically, and maybe unsurprisingly, nobody has informed LaTeX so far how to hyphenate the "word"

D\_alcohol\_consumption

Fortuntately, there is an easy way to inform LaTeX that it may treat the substring consumption as an individual word: Just replace D\_alcohol\_consumption with

D\_alcohol\_\hspace{0pt}consumption

A separate comment: By default, LaTeX doesn't hyphenate words that contain one or more instances of - (the hyphen character). If you want to allow LaTeX to hyphenate the word consumption in the compound hyphenated word D-alcohol-consumption, all you need to do is rewrite it as

D-alcohol-\hspace{0pt}consumption

Final comment: In view of the narrowness of some of the columns, I'd use a ragged-right layout for these columns instead of full justification (which is the default), while still allowing hyphenation when needed. This may be achieved automatically by prefixing the column specifier with >{\RaggedRight}; see the code below for how to implement this thought. (If you actually want to suppress hyphenation, just replace >{\RaggedRight} with >{\raggedright\arraybackslash}.


enter image description here

\documentclass{article}
\usepackage{array}
\usepackage{ragged2e} % for '\RaggedRight' macro
\begin{document}

\begin{tabular}{@{} >{\RaggedRight}p{3.1cm} @{}} \hline D_alcohol_\hspace{0pt}consumption \ \hline D-alcohol-consumption \ \hline D-alcohol-\hspace{0pt}consumption \ \hline \end{tabular}

\end{document}

Mico
  • 506,678
0

I figured out by using the hyphenat package

\usepackage{hyphenat}
\begin{landscape}

\setlength\tabcolsep{3pt} \begin{longtable}{m{0.5cm}|m{3cm}|m{3cm}|m{3cm}|m{9cm}} \hline \textbf{C1}&\textbf{Col2}&\textbf{Col3}&\textbf{Col4}&\textbf{Col5}\ \hline \endfirsthead \endfoot 1&Self-report&-&D_alcohol_consumption&\\hline 2&Self-report&-&Trastorno del pensamiento controlado&\\hline 3&Self-report&-&D-alcohol-consumption&\\hline \end{longtable}

\end{landscape}

enter image description here

Lev
  • 145