[Tex/LaTex] Wrapping text in tables which contains underscores

tablestabularxtabularyverbatimwrap

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:

enter image description here

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 the L 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 the table 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.

enter image description here

\documentclass[12pt]{report}
% preamble stripped down to the bare minimum
\usepackage{tabularx,ragged2e,caption}
\newcolumntype{L}{>{\ttfamily\RaggedRight\arraybackslash\hspace{0pt}}X}
\newcolumntype{C}{>{\centering\arraybackslash\hspace{0pt}}X}

\begin{document}
\begin{table}[!htbp]
\setlength\extrarowheight{3pt}
\setlength\tabcolsep{4pt} % default value: 6pt
\catcode`_=13 % Make _ "active"; 
%% This setting expires at end of current group, i.e., at end of "table" environment
\def_{\textunderscore\hspace{0pt}} % "meaning" of "_"

\caption{List of Open and Closed Evaluation Properties}
\label{table:openclosed}

\begin{tabularx}{\textwidth}{|C|C|C|}
\hline
\textbf{Open Properties} &  \textbf{Closed Properties} & \textbf{Sentence Label Properties} \\
\hline
\begin{tabular}{@{}L@{}}
gni_per_capita_in_ppp_dollars\\
health_expenditure_as_percent_of_gdp\\
\end{tabular}
&
\begin{tabular}{@{}L@{}}
gni_in_ppp_dollars\\
gni_per_capita_in_ppp_dollars\\
health_expenditure_as_percent_of_gdp\\
internet_users_percent_population\\
labor_participation_rate\\
life_expectancy\\
merchandise_trade_percent_of_gdp\\
net_migration\\
population\\
population_growth_rate\\
prevalence_of_undernourisment\\
renewable_freshwater_per_capita\\
size_of_armed_forces\\
\end{tabular}
&
\begin{tabular}{@{}L@{}}
consumer_price_index\\
cpi_inflation_rate\\
diesel_price_liter\\
\end{tabular}\\
\hline
\end{tabularx}
\end{table}

\end{document}