表格中包含下划线的文本换行

表格中包含下划线的文本换行

我在表格中的多列中有大量文本,其中包含带下划线的项目。但是,这些文本不会围绕固定的列宽,因此我得到:

在此处输入图片描述

这是我的代码:

 \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}

如果这些值超出分配的宽度,我该如何让它们环绕并适合新行中的单元格?此外,如果任何列包含的行数少于其行中的最长列(例如,第 2 列的项目数少于第 1 列的项目数),我希望文本从顶部开始,而不是从中间开始。只需添加[t]按照以下方法,仅添加到内部表格似乎不起作用:tabularx 中单元格内容的顶部对齐

答案1

您的代码,正如所写,提供了三个不同的原因来解释为什么长“单词”中没有出现换行符:

  • 逐字类型组内不允许换行

  • 不允许在以下类型的列内换行l

  • LaTeX 根本不知道在这些长“单词”中在哪里插入换行符。即使 LaTeX 知道,它也会插入连字符,这可能不太理想。

你还没有表明在哪里_在长单词内允许换行;在下文中,我假设仅在(下划线)字符后允许换行。

以下示例对您的代码应用了以下修改:

  • 环境tabularx用于表格材料。

  • 所有\verb指令均被删除;它们被替换\ttfamily为定义中的指令L。这样,材料仍将使用等宽字体排版,但长单词将被换行如果可以找到合适的换行符。

  • 最重要的是,下划线字符被“激活”——按照 TeX 的字面意思——并被赋予了 的含义\textunderscore\hspace{0pt}。 的重新定义_是在环境内部完成的table;因此,当遇到 时,重新定义就会失效\end{table}。这样,每个下划线字符后都可以使用没有连字符的换行符。

在此处输入图片描述

\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}

相关内容