2

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.

0

1 Answer 1

3

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}
13
  • Thanks @Mico, how would I adapt to each column is top aligned (text starts from top of cell)? Sep 1, 2016 at 14:31
  • @DhruvGhulati - Change all three instances of \begin{tabular}{@{}L@{}} to \begin{tabular}[t]{@{}L@{}}. (I had assumed, apparently incorrectly, that you wanted centered alignment instead of top alignment of the columns since the code you posted uses m-type rather than p-type columns...)
    – Mico
    Sep 1, 2016 at 15:04
  • I would love to use this but it removes the fixed column spacing I needed for another table in my document, which is a longtable, created via - tex.stackexchange.com/questions/325873/…. By resetting this, and also loading tabulary, it messes things up. Is there a way of doing this using the same L and C setup as before? Sep 3, 2016 at 11:58
  • @DhruvGhulati - I really wish you had mentioned at the outset that you need to enable page breaks of the longtable variety. (For sure, the use of a table environment in the code you posted didn't indicate in any way that you would need to allow page breaks. Ditto for thetabulary environment -- it also doesn't allow page breaks.) I'm afraid that allowing pagebreaks is going to require a completely different setup. Shall I just delete this answer? It definitely appears to be of little or no value to you, doesn't it. Please advise.
    – Mico
    Sep 3, 2016 at 13:27
  • I'll revise the question so is clearer. I have tried to have a work around of having multiple commands e.g. allowing \begin{longtable}{@{}LL{4cm}LL{4cm}LL{6cm}@{}} for my long tables but also \begin{tabularx}{\textwidth}{|C|C|C|} Sep 3, 2016 at 13:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .