# Multiple nested tabular within longtable environment

I initially had a tabularx setup that was working well, and this included 3 tabular tables, each representing a single long column.

However, now the table is long and has to span multiple pages, so I need a longtable. For the sake of length I do not want to provide the tabularx setup that worked.

Here is my code:

\documentclass[12pt]{report}
\usepackage{array,ltablex,makecell}% ltablex loads tabularx and longtable also
% \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}
% \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
net_migration
population
population_growth_rate
prevalence_of_undernourisment
renewable_freshwater_per_capita
size_of_armed_forces
}
&
\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
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
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
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
unemployment_rate
}
\\\bottomrule
\end{longtable}\label{listofproperties}

\end{document}


I am currently getting:

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


At the end of the first multicolumn. What am I doing wrong here? I tried multirow with a fixed 4cm gap and 25 rows but still had the same error.

The original table with tabularx looked like:

• the error is becouse of _ chaning it into \_ is much better than to change the catcode for _, which could introduce some errors. Haven't looked into why yet. But I don't really understand why you put all of those variables in a single cell. Shouldn't they be put on seperate lines? Longtable cannot split a cell, but you essiantially have some cells which are taller than a page. – Runar Sep 10 '16 at 10:58
• move \catcode_=13 above \begin{longtable}… and it will solve your error. when placed inside the environment, it will only have an effect on the first cell. You should also probably put a curly brace in front of \catcod… and end of longtable, to limit the scope. – Runar Sep 10 '16 at 11:01
• why are you doing \multicolumn{1}{C{4cm}}{ when the column is already specified as C{4cm} and all your settings of \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 "_" will be lost at the first \ which is on the next line, so they do nothing. but this appears to be a 1-row longtable so longtable can not do anything at all. – David Carlisle Sep 10 '16 at 11:09
• Does a longtable with nested inner tabulars break at all any longer? – user31729 Sep 10 '16 at 11:31
• @ChristianHupfer as single row longtable will never break as longtable allows page breaks between rows, so it doesn't matter if the inner tabular is 3 times the size of the page, it still won't break. – David Carlisle Sep 10 '16 at 12:17

In this answer, I have given a solution to the problem, but also an alternate approach, which in my opinion, would not only look better, but also be more customizable and maintanable. The alternate approach is located below the solution to the problem

# Problematic table

The error occurs because the \catcode_=13 is set within the longtable, so that it's scope would be only effective for a single row. I've moved this code outside the longtable and added curly braces around it, and the longtable to limit the scope of this.

When you use \multicolumn and set add lots of text inside, the content will be treated as a single cell, so longtable is not able to the content, as it cannot break a cell, only rows.

### Further suggestions

• Use \endhead and friends from longtable-package to have the same headers and footers on every page.
• When printing variables, such as consumer_price_index, set them in monospace font, using \ttfamily or \texttt.
• Make hyphenations for _ possible with the underscore-package. There are multiple alternatives.
• Create some more space between lines to improve the readability, especially with these long variables.
• Make sure you use code you only know the effect of. For your column specifiers, you seem to be doing something unnecessary with \let\newline\\\arraybackslash. Who gave you that code?
• When commenting your code, make sure you don't write something that is plain wrong, or at least very redundant. tabulary is meant for tables, yes, but what does it actually do? You don't need it for tables, and the comment would just confuse you, and everybody reading your code.

### Output

Here is a comparison of 3,4 and 5-columns for your content. it can easilty be changed with changin before=\begin{multicols}{3}

### Code

\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
\caption[]{(continued)}\\
\toprule
\cf{Open Properties} & \cf{Closed Properties} &
\cf{Sentence Label Properties} \\
\midrule
\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\\
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\\
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\\
&                                      & 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\\
&                                      & 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\\
&                                      & unemployment_rate
\end{longtable}
}% end scope

\end{document}


# Alternate approach

As I interpret, you simply want a list of some variables, or properties. Therefore, a list would be more suited, than a tabular, which is more intented to compare and differentiate information, not list them. Therefore I suggest a modifieditemize-environment called propertyList. It should be highly modifieable. You can easaly adjust how many columns you want, spacing, font and such, with the enumitem-package.

### Code

\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 net_migration
\item population
\item population_growth_rate
\item prevalence_of_undernourisment
\item renewable_freshwater_per_capita
\item size_of_armed_forces
\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 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 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 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
`