有问题的表

有问题的表

我最初有一个tabularx运行良好的设置,其中包括 3 个tabular表,每个表代表一个长列。

但是,现在表格很长,必须跨越多页,所以我需要一个longtable。出于长度的考虑,我不想提供tabularx有效的设置。

这是我的代码:

\documentclass[12pt]{report}
 \usepackage{array,ltablex,makecell}% ltablex loads tabularx and longtable also
\renewcommand\theadfont{\normalsize\bfseries}
% \renewcommand*\descriptionlabel[1]{\hspace\leftmargin$#1$}%This is for descriptions to appear on the LHS with an indent
\usepackage[]{multirow}%Essential for cells in table spanning multiple rows
\usepackage[autostyle]{csquotes}% This is for quotes
% \usepackage{tabulary}% This is for tables
\usepackage{ragged2e}% This is formatting for long tables
\usepackage[font={small}]{caption}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}%This is a wrapper to make everything a certain width -left aligned columns with stuff at the top.
\newcolumntype{U}{>{\raggedright\arraybackslash\hspace{0pt}}X}%This is a wrapper to make everything a certain width -left aligned columns with stuff at the top.
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}%C is for central aligned columns and middle aligned, p puts stuff at the bottom, m middles everything.
\newcolumntype{Y}{>{\centering\arraybackslash\hspace{0pt}}X}%C is for central aligned columns and middle aligned.
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
% \newcommand{\specialcell}[2][c]{\begin{tabular}[#1]{@{}c@{}}#2\end{tabular}}%This is to force new lines in cells
\usepackage{hhline}%
\usepackage[table, svgnames]{xcolor}%
\usepackage{graphicx}% This is for images
\usepackage{booktabs,dcolumn,caption}
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}%This is for precision tables per property
% \captionsetup{labelsep=newline,singlelinecheck=false} % optional, this makes the caption numbers appear on a new line.
\begin{document}

\begin{longtable}{C{4cm}C{4cm}C{4cm}}
\caption{List of Open and Closed Evaluation Properties}
\label{table:openclosed}
\scriptsize
% \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 "_"
\ra{1.0}\\
\toprule
\textbf{Open Properties} & \textbf{Closed Properties} & 
\textbf{Sentence Label Properties} \\ 
\midrule
\multicolumn{1}{C{4cm}}{consumer_price_index
cpi_inflation_rate
diesel_price_liter
fertility_rate
foreign_direct_investment_net_inflows
gdp_growth_rate
gdp_nominal
gdp_nominal_per_capita
gdp_real
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
time_required_to_start_a_business
trade_balance_as_percent_of_gdp
}
& 
\multicolumn{1}{C{4cm}}{consumer_price_index
cpi_inflation_rate
diesel_price_liter
fertility_rate
gdp_nominal
gdp_nominal_per_capita
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
population
population_growth_rate
prevalence_of_undernourisment
renewable_freshwater_per_capita 
}
&
\multicolumn{1}{C{4cm}}{
agriculture_as_percent_of_gdp
arithmetic_population_density
automobiles_per_capita
brain_drain_percent
broadband_penetration_rate
child_labor_percent
co2_emissions_commercial
co2_emissions_industrial
co2_emissions_mobile
co2_emissions_per_capita
co2_emissions_residential
co2_emissions_total
consumer_price_index
cpi_inflation_rate
debt_service_as_percent_of_trade_volume
deposit_interest_rate
diesel_price_liter
electricity_consumption_per_capita
electricity_production
energy_use_per_capita
exports_as_percent_of_gdp
external_debt_stock
fertility_rate
foreign_direct_investment_net_inflows
gas_price_liter
gdp_deflator_change
gdp_growth_rate
gdp_nominal
gdp_nominal_per_capita
gdp_real
global_competitiveness_index
gni_in_ppp_dollars
gni_per_capita_in_ppp_dollars
government_debt_percent_gdp
greenhouse_gas_emission
greenhouse_gas_emission_intensity
greenhouse_gas_emissions_per_capita
gross_capital_formation_as_percent_of_gdp
gross_savings_as_percent_of_gdp
health_expenditure_as_percent_of_gdp
high_tech_as_percent_of_manufactured_exports
hiv_prevalence_rate
household_consumption_expenditure_as_percent_of_gdp
imports_as_percent_of_gdp
industry_as_percent_of_gdp
internet_users
internet_users_percent_population
labor_participation_rate
lending_interest_rate
life_expectancy
literacy_rate
long_term_unemployment_rate
market_cap_of_listed_companies_as_percent_of_gdp
merchandise_trade_percent_of_gdp
military_expenditure_percent_gdp
minimum_wage
natural_gas_production
net_migration
net_workers_remittances
official_development_assistance
oil_production
part_time_employment_percent
population
population_growth_rate
poverty_rate_2dollars_per_day
prevalence_of_undernourisment
renewable_freshwater_per_capita
services_as_percent_of_gdp
size_of_armed_forces
smoking_prevalence_rate
tax_revenue_percent_of_gdp
time_required_to_start_a_business
trade_balance_as_percent_of_gdp
unemployment_rate
}
\\\bottomrule
\end{longtable}\label{listofproperties}

\end{document}

我目前得到:

Missing $ inserted.
<inserted text> 
                $
l.279 }

在第一个结束时multicolumn。我在这里做错了什么?我尝试multirow使用固定的 4cm 间隙和 25 行,但仍然出现相同的错误。

原始表格tabularx如下:

在此处输入图片描述

答案1

在这个答案中,我给出了问题的解决方案,但也给出了另一种方法,在我看来,这种方法不仅看起来更好,而且更可定制和可维护。替代方法位于问题解决方案下方

有问题的表

出现此错误的原因是,\catcode`_=13在 内设置了longtable,因此其范围仅对单行有效。我已将此代码移出 ,longtable并在其周围添加了花括号,并 以longtable限制此范围。

当您使用\multicolumn并设置在其中添加大量文本时,内容将被视为单个单元格,因此longtable无法显示内容,因为它无法打破单元格,只能打破行。

进一步的建议

  • 使用-package\endhead中的及其朋友longtable使每一页都有相同的页眉和页脚。
  • 打印变量时,例如,使用或consumer_price_index将其设置为等宽字体。\ttfamily\texttt
  • 使用 -package使连字符成为_可能underscore。有多种替代方案。
  • 在行之间留出更多空间以提高可读性,尤其是对于这些长变量。
  • 确保只使用您知道其效果的代码。对于您的列说明符,您似乎对 做了一些不必要的事情\let\newline\\\arraybackslash。谁给您的代码?
  • 在注释代码时,请确保不要写出明显错误或至少非常多余的内容。tabulary是用于表格的,是的,但它实际上有什么用呢?您不需要它用于表格,注释只会让您和阅读您代码的每个人都感到困惑。

输出

以下是 3、4 和 5 列内容的比较。它可以通过更改轻松更改before=\begin{multicols}{3}

在此处输入图片描述

代码

\documentclass[12pt]{report}
\usepackage{underscore}% To hyphenate \_
\usepackage{array}
\usepackage{calc}
\usepackage{ltablex}
\usepackage[font={small}]{caption}
\usepackage{booktabs}
\usepackage{showframe}

\newcolumntype{L}[1]{>{\raggedright\arraybackslash\hspace{0pt}}m{#1}}%This is a wrapper to make everything a certain width -left aligned columns with stuff at the top. lines in cells
\newcommand{\cf}[1]{%
\multicolumn{1}{c}{\bfseries#1}
}
\begin{document}
  {% start a group to limit scope
  \catcode`_=13 % Make _ "active";
  \def_{\textunderscore\hspace{0pt}} % "meaning" of "_"
  % \scriptsize
  \renewcommand{\arraystretch}{1.4}% some exstra space between lines, for clarity

  \begin{longtable}{*{3}{>{\ttfamily}L{\textwidth/3-4\tabcolsep}|}}
    \caption{List of Open and Closed Evaluation Properties}
    \label{table:openclosed}\\
    \toprule
    \cf{Open Properties} & \cf{Closed Properties} &
    \cf{Sentence Label Properties} \\
    \midrule
    \endfirsthead
    \caption[]{(continued)}\\
    \toprule
    \cf{Open Properties} & \cf{Closed Properties} &
    \cf{Sentence Label Properties} \\
    \midrule
    \endhead
    \bottomrule
    \endfoot
    consumer_price_index                  & consumer_price_index                 & agriculture_as_percent_of_gdp\\
    cpi_inflation_rate                    & cpi_inflation_rate                   & arithmetic_population_density\\
    diesel_price_liter                    & diesel_price_liter                   & automobiles_per_capita\\
    fertility_rate                        & fertility_rate                       & brain_drain_percent\\
    foreign_direct_investment_net_inflows & gdp_nominal                          & broadband_penetration_rate\\
    gdp_growth_rate                       & gdp_nominal_per_capita               & child_labor_percent\\
    gdp_nominal                           & gni_in_ppp_dollars                   & co2_emissions_commercial\\
    gdp_nominal_per_capita                & gni_per_capita_in_ppp_dollars        & co2_emissions_industrial\\
    gdp_real                              & health_expenditure_as_percent_of_gdp & co2_emissions_mobile\\
    gni_in_ppp_dollars                    & internet_users_percent_population    & co2_emissions_per_capita\\
    gni_per_capita_in_ppp_dollars         & labor_participation_rate             & co2_emissions_residential\\
    health_expenditure_as_perce\nt_of_gdp & life_expectancy                      & co2_emissions_total\\
    internet_users_percent_population     & population                           & consumer_price_index\\
    labor_participation_rate              & population_growth_rate               & cpi_inflation_rate\\
    life_expectancy                       & prevalence_of_undernourisment        & debt_service_as_percent_of_trade_volume\\
    merchandise_trade_percent_of_gdp      & renewable_freshwater_per_capita      & deposit_interest_rate\\
    net_migration                         &                                      & diesel_price_liter\\
    population                            &                                      & electricity_consumption_per_capita\\
    population_growth_rate                &                                      & electricity_production\\
    prevalence_of_undernourisment         &                                      & energy_use_per_capita\\
    renewable_freshwater_per_capita       &                                      & exports_as_percent_of_gdp\\
    size_of_armed_forces                  &                                      & external_debt_stock\\
    time_required_to_start_a_business     &                                      & fertility_rate\\
    trade_balance_as_percent_of_gdp       &                                      & foreign_direct_investment_net_inflows\\
                                          &                                      & gas_price_liter\\
                                          &                                      & gdp_deflator_change\\
                                          &                                      & gdp_growth_rate\\
                                          &                                      & gdp_nominal\\
                                          &                                      & gdp_nominal_per_capita\\
                                          &                                      & gdp_real\\
                                          &                                      & global_competitiveness_index\\
                                          &                                      & gni_in_ppp_dollars\\
                                          &                                      & gni_per_capita_in_ppp_dollars\\
                                          &                                      & government_debt_percent_gdp\\
                                          &                                      & greenhouse_gas_emission\\
                                          &                                      & greenhouse_gas_emission_intensity\\
                                          &                                      & greenhouse_gas_emissions_per_capita\\
                                          &                                      & gross_capital_formation_as_percent_of_gdp\\
                                          &                                      & gross_savings_as_percent_of_gdp\\
                                          &                                      & health_expenditure_as_percent_of_gdp\\
                                          &                                      & high_tech_as_percent_of_manufactured_exports\\
                                          &                                      & hiv_prevalence_rate\\
                                          &                                      & household_consumption_expenditure_as_percent_of_gdp \\
                                          &                                      & imports_as_percent_of_gdp\\
                                          &                                      & industry_as_percent_of_gdp\\
                                          &                                      & internet_users\\
                                          &                                      & internet_users_percent_population\\
                                          &                                      & labor_participation_rate\\
                                          &                                      & lending_interest_rate\\
                                          &                                      & life_expectancy\\
                                          &                                      & literacy_rate\\
                                          &                                      & long_term_unemployment_rate\\
                                          &                                      & market_cap_of_listed_companies_as_percent_of_gdp\\
                                          &                                      & merchandise_trade_percent_of_gdp\\
                                          &                                      & military_expenditure_percent_gdp\\
                                          &                                      & minimum_wage\\
                                          &                                      & natural_gas_production\\
                                          &                                      & net_migration\\
                                          &                                      & net_workers_remittances\\
                                          &                                      & official_development_assistance\\
                                          &                                      & oil_production\\
                                          &                                      & part_time_employment_percent\\
                                          &                                      & population\\
                                          &                                      & population_growth_rate\\
                                          &                                      & poverty_rate_2dollars_per_day\\
                                          &                                      & prevalence_of_undernourisment\\
                                          &                                      & renewable_freshwater_per_capita\\
                                          &                                      & services_as_percent_of_gdp\\
                                          &                                      & size_of_armed_forces\\
                                          &                                      & smoking_prevalence_rate\\
                                          &                                      & tax_revenue_percent_of_gdp\\
                                          &                                      & time_required_to_start_a_business\\
                                          &                                      & trade_balance_as_percent_of_gdp\\
                                          &                                      & unemployment_rate
  \end{longtable}
  }% end scope

\end{document}

替代方法

根据我的理解,您只需要一些变量或属性的列表。因此,列表比表格更合适,表格更适合比较和区分信息,而不是列出信息。因此,我建议使用itemize名为 的经过修改的环境propertyList。它应该是高度可修改的。您可以使用 -package 轻松调整所需的列数、间距、字体等enumitem

输出

在此处输入图片描述

代码

\documentclass{article}
\usepackage{multicol}
\usepackage{underscore}
\usepackage{enumitem}
\usepackage[margin=0.5cm]{geometry}

\newlist{propertyList}{itemize}{1}
\setlist[propertyList]{
  label=\textbullet,
  noitemsep,
  leftmargin=10pt,
  font=\rmfamily,
  before=\begin{multicols}{3}%
    \ttfamily,
  after=\end{multicols}
  }

\begin{document}
\section*{List of Open and Closed Evaluation Properties}
\subsection*{Open Properties}
\begin{propertyList}
\item cpi_inflation_rate
\item diesel_price_liter
\item fertility_rate
\item foreign_direct_investment_net_inflows
\item gdp_growth_rate
\item gdp_nominal
\item gdp_nominal_per_capita
\item gdp_real
\item gni_in_ppp_dollars
\item gni_per_capita_in_ppp_dollars
\item health_expenditure_as_percent_of_gdp
\item internet_users_percent_population
\item labor_participation_rate
\item life_expectancy
\item merchandise_trade_percent_of_gdp
\item net_migration
\item population
\item population_growth_rate
\item prevalence_of_undernourisment
\item renewable_freshwater_per_capita
\item size_of_armed_forces
\item time_required_to_start_a_business
\item trade_balance_as_percent_of_gdp
\end{propertyList}

\subsection*{Closed Properties}

\begin{propertyList}
\item cpi_inflation_rate
\item diesel_price_liter
\item fertility_rate
\item gdp_nominal
\item gdp_nominal_per_capita
\item gni_in_ppp_dollars
\item gni_per_capita_in_ppp_dollars
\item health_expenditure_as_percent_of_gdp
\item internet_users_percent_population
\item labor_participation_rate
\item life_expectancy
\item population
\item population_growth_rate
\item prevalence_of_undernourisment
\item renewable_freshwater_per_capita
\end{propertyList}

\subsection*{Sentence Label Properties}

\begin{propertyList}
  \item agriculture_as_percent_of_gdp
  \item arithmetic_population_density
  \item automobiles_per_capita
  \item brain_drain_percent
  \item broadband_penetration_rate
  \item child_labor_percent
  \item co2_emissions_commercial
  \item co2_emissions_industrial
  \item co2_emissions_mobile
  \item co2_emissions_per_capita
  \item co2_emissions_residential
  \item co2_emissions_total
  \item consumer_price_index
  \item cpi_inflation_rate
  \item debt_service_as_percent_of_trade_volume
  \item deposit_interest_rate
  \item diesel_price_liter
  \item electricity_consumption_per_capita
  \item electricity_production
  \item energy_use_per_capita
  \item exports_as_percent_of_gdp
  \item external_debt_stock
  \item fertility_rate
  \item foreign_direct_investment_net_inflows
  \item gas_price_liter
  \item gdp_deflator_change
  \item gdp_growth_rate
  \item gdp_nominal
  \item gdp_nominal_per_capita
  \item gdp_real
  \item global_competitiveness_index
  \item gni_in_ppp_dollars
  \item gni_per_capita_in_ppp_dollars
  \item government_debt_percent_gdp
  \item greenhouse_gas_emission
  \item greenhouse_gas_emission_intensity
  \item greenhouse_gas_emissions_per_capita
  \item gross_capital_formation_as_percent_of_gdp
  \item gross_savings_as_percent_of_gdp
  \item health_expenditure_as_percent_of_gdp
  \item high_tech_as_percent_of_manufactured_exports
  \item hiv_prevalence_rate
  \item household_consumption_expenditure_as_percent_of_gdp
  \item imports_as_percent_of_gdp
  \item industry_as_percent_of_gdp
  \item internet_users
  \item internet_users_percent_population
  \item labor_participation_rate
  \item lending_interest_rate
  \item life_expectancy
  \item literacy_rate
  \item long_term_unemployment_rate
  \item market_cap_of_listed_companies_as_percent_of_gdp
  \item merchandise_trade_percent_of_gdp
  \item military_expenditure_percent_gdp
  \item minimum_wage
  \item natural_gas_production
  \item net_migration
  \item net_workers_remittances
  \item official_development_assistance
  \item oil_production
  \item part_time_employment_percent
  \item population
  \item population_growth_rate
  \item poverty_rate_2dollars_per_day
  \item prevalence_of_undernourisment
  \item renewable_freshwater_per_capita
  \item services_as_percent_of_gdp
  \item size_of_armed_forces
  \item smoking_prevalence_rate
  \item tax_revenue_percent_of_gdp
  \item time_required_to_start_a_business
  \item trade_balance_as_percent_of_gdp
  \item unemployment_rate
\end{propertyList}


\end{document}

相关内容