I have lots of text in multiple columns in tables which contains items with underscores. However, these do not wrap around a fixed column width, so I get:
Here is my code:
\documentclass[12pt]{report}
\usepackage{array,ltablex, makecell}%
\renewcommand\theadfont{\normalsize\bfseries}
% \renewcommand*\descriptionlabel[1]{\hspace\leftmargin$#1$}%This is for descriptions to appear on the LHS with an indent
\newenvironment{conditions}
{\par\vspace{\abovedisplayskip}\noindent\begin{tabular}{>{$}l<{$} @{${}={}$} l}}
{\end{tabular}\par\vspace{\belowdisplayskip}}%This is for descriptions of equations
\usepackage[]{multirow}
\usepackage[autostyle]{csquotes}% This is for quotes
\usepackage{tabulary}% This is for tables
\usepackage{ragged2e}
\usepackage{longtable,array,ragged2e}% This is formatting for long tables
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\usepackage{graphicx}% This is for images
\usepackage{booktabs,dcolumn,caption}
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}%This is for precision tables per property
\newcolumntype{d}[1]{D{.}{.}{#1}} % "decimal" column type
\renewcommand{\ast}{{}^{\textstyle *}} % for raised "asterisks"
\begin{document}
\begin{table}[!htbp]
\centering
\caption{List of Open and Closed Evaluation Properties}
\label{table:openclosed}
\ra{1.0}%This stretches the contents of cells
\begin{tabular}{|C{4cm}|C{4cm}|C{4cm}|}
\hline
\textbf{Open Properties} & \textbf{Closed Properties} &
\textbf{Sentence Label Properties} \\
\hline
\begin{tabular}{@{}l@{}}
\verb|gni_per_capita_in_ppp_dollars|\\
\verb|health_expenditure_as_percent_of_gdp|\\
\end{tabular}
&
\begin{tabular}{@{}l@{}}
\verb|gni_in_ppp_dollars|\\
\verb|gni_per_capita_in_ppp_dollars|\\
\verb|health_expenditure_as_percent_of_gdp|\\
\verb|internet_users_percent_population|\\
\verb|labor_participation_rate|\\
\verb|life_expectancy|\\
\verb|merchandise_trade_percent_of_gdp|\\
\verb|net_migration|\\
\verb|population|\\
\verb|population_growth_rate|\\
\verb|prevalence_of_undernourisment|\\
\verb|renewable_freshwater_per_capita|\\
\verb|size_of_armed_forces|\\
\end{tabular}
&
\begin{tabular}{@{}l@{}}
\verb|consumer_price_index|\\
\verb|cpi_inflation_rate|\\
\verb|diesel_price_liter|\\
\end{tabular}\\
\hline
\end{tabular}
\end{table}
\end{document}
How can I get these values to wrap around and fit the cells in new lines if they go beyond the allocated width? In addition, if any column contains less rows than the longest column in its row (e.g. column 2 has less items than column 1), I would like the text to start at the top instead of the middle. Just adding [t]
to the inner tabulars didn't seem to work per this: Top alignment of cell content in tabularx.
Best Answer
Your code, as written, provides three separate reasons for why line breaks aren't occurring inside the long "words":
line breaks aren't allowed inside verbatim-type groups
line breaks aren't allowed inside columns of type
l
LaTeX simply doesn't know where to insert line breaks in these long "words". And even if LaTeX did know, it would insert hyphen characters, which is probably rather undesirable.
You haven't indicated where inside the long words line breaks are permissible; in what follows, I assume line breaks are permitted only after
_
(underscore) characters.The following example applies the following modifications to your code:
A
tabularx
environment is used for the tabular material.All
\verb
directives are removed; they are replaced with\ttfamily
directives in the definition of theL
column type. That way, the material will still be typeset using a monospaced font, but long words will be wrapped if a suitable line break can be found.Most importantly, the underscore characters are made "active" -- in the TeX sense of the word -- and are given the meaning
\textunderscore\hspace{0pt}
. The redefinition of_
is done inside thetable
environment; hence, the redefinition expires when\end{table}
is encountered. That way, line breaks without hyphen characters are made possible after each and every underscore character.