Create a table with double title - pdf

I have this code to create a table, but I want to create a table like the image below. How can I modify the code to create the table?
\begin{tabular}{|l|l|l|l|l|}
\hline
& AB & A & B & O \\ \hline
AB & 0 & 1 & 1 & 2 \\ \hline
A & 1 & 0 & 2 & 2 \\ \hline
B & 1 & 2 & 0 & 2 \\ \hline
O & 3 & 3 & 3 & 0 \\ \hline
\end{tabular}
\end{table}

\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{table}
\centering
\renewcommand{\arraystretch}{1.5}%
\begin{tabular}{c c *{4}{| c} |}
& \multicolumn{1}{c}{} & \multicolumn{4}{c}{Classified As} \\
& & AB & A & B & O \\
\cline{2-6}
& AB & 0 & 1 & 1 & 2 \\
\cline{2-6}
\multirow{2}*{\renewcommand{\arraystretch}{1}%
\begin{tabular}{#{}c#{}}
True \\ Blood \\ Type
\end{tabular}} & A & 1 & 0 & 2 & 2 \\
\cline{2-6}
& B & 1 & 2 & 0 & 2 \\
\cline{2-6}
& O & 3 & 3 & 3 & 0 \\
\cline{2-6}
\end{tabular}
\end{table}
\end{document}

Hopefully easy:
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{table}
\centering
\begin{tabular}{cc|c|c|c|c|}
& \multicolumn{1}{c}{ } & \multicolumn{4}{c}{Classified As} \\
& & AB & A & B & O \\ \cline{2-6}
\multirow{4}{11mm}{True Blood Type} %
& AB & 0 & 1 & 1 & 2 \\ \cline{2-6}
& A & 1 & 0 & 2 & 2 \\ \cline{2-6}
& B & 1 & 2 & 0 & 2 \\ \cline{2-6}
& O & 3 & 3 & 3 & 0 \\ \cline{2-6}
\end{tabular}
\end{table}
\end{document}
The above MWE outputs the following table:
This first sketch can then be improved aesthetically in several ways!

Related

Large table in Latex gets cut off when outputting to PDF?

I haven't worked with much Latex syntax so I'm not quite sure what stylization changes I need to make here.
I have a large piece of Latex as such:
\documentclass{article}
\usepackage{longtable}
\begin{document}
\begin{center}
\begin{longtable}{|l|l|l|} \centering
\begin{tabular}{#{\extracolsep{5pt}}lcccccccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{8}{c}{\textit{Dependent variable:}} \\
\cline{2-9}
\\[-1.8ex] & \multicolumn{2}{c}{Female} & \multicolumn{4}{c}{NonWhite} & \multicolumn{2}{c}{Black.or.African.American} \\
\\[-1.8ex] & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8)\\
\hline \\[-1.8ex]
factor(ELECTORAL\_TERM)2018 & 0.104$^{***}$ & 0.104$^{***}$ & $-$0.104$^{***}$ & $-$0.104$^{***}$ & $-$0.103$^{***}$ & $-$0.103$^{***}$ & $-$0.047$^{***}$ & $-$0.047$^{***}$ \\
& (0.010) & (0.010) & (0.009) & (0.009) & (0.009) & (0.009) & (0.006) & (0.006) \\
& & & & & & & & \\
factor(ELECTORAL\_TERM)2019 & 0.129$^{***}$ & 0.128$^{***}$ & 0.088$^{***}$ & 0.088$^{***}$ & 0.092$^{***}$ & 0.092$^{***}$ & 0.063$^{***}$ & 0.063$^{***}$ \\
& (0.011) & (0.011) & (0.009) & (0.010) & (0.009) & (0.009) & (0.007) & (0.007) \\
& & & & & & & & \\
WOMENS\_RIGHTS\_PROTEST & $-$0.016 & $-$0.025 & & & & & & \\
& (0.018) & (0.019) & & & & & & \\
& & & & & & & & \\
WOMENS\_RIGHTS\_COUNTERPROTEST & & 0.052 & & & & & & \\
& & (0.043) & & & & & & \\
& & & & & & & & \\
RACIAL\_PROTEST & & & 0.032$^{***}$ & 0.032$^{***}$ & & & 0.020$^{***}$ & 0.021$^{***}$ \\
& & & (0.010) & (0.011) & & & (0.007) & (0.008) \\
& & & & & & & & \\
RACIAL\_COUNTERPROTEST & & & & 0.001 & & & & $-$0.007 \\
& & & & (0.039) & & & & (0.028) \\
& & & & & & & & \\
OTHER\_IDENTITY\_PROTEST & & & & & 0.057 & 0.068 & & \\
& & & & & (0.077) & (0.078) & & \\
& & & & & & & & \\
OTHER\_IDENTITY\_COUNTERPROTEST & & & & & & $-$0.116 & & \\
& & & & & & (0.156) & & \\
& & & & & & & & \\
Republican.Party & 0.061$^{***}$ & 0.061$^{***}$ & 0.068$^{***}$ & 0.068$^{***}$ & 0.068$^{***}$ & 0.068$^{***}$ & 0.027$^{***}$ & 0.027$^{***}$ \\
& (0.014) & (0.014) & (0.012) & (0.012) & (0.012) & (0.012) & (0.009) & (0.009) \\
& & & & & & & & \\
Democratic.Party & 0.386$^{***}$ & 0.386$^{***}$ & 0.162$^{***}$ & 0.162$^{***}$ & 0.162$^{***}$ & 0.162$^{***}$ & 0.083$^{***}$ & 0.083$^{***}$ \\
& (0.016) & (0.016) & (0.014) & (0.014) & (0.014) & (0.014) & (0.010) & (0.010) \\
& & & & & & & & \\
CIT\_EST\_White & $-$0.002 & $-$0.002 & 0.001 & 0.001 & 0.001 & 0.001 & 0.0002 & 0.0002 \\
& (0.002) & (0.002) & (0.001) & (0.001) & (0.001) & (0.001) & (0.001) & (0.001) \\
& & & & & & & & \\
CIT\_EST\_African\_American & 0.007$^{**}$ & 0.007$^{**}$ & 0.001 & 0.001 & 0.001 & 0.001 & 0.0005 & 0.0005 \\
& (0.004) & (0.004) & (0.003) & (0.003) & (0.003) & (0.003) & (0.002) & (0.002) \\
& & & & & & & & \\
CIT\_EST\_NonWhite & 0.014 & 0.014 & $-$0.004 & $-$0.004 & $-$0.004 & $-$0.004 & $-$0.002 & $-$0.002 \\
& (0.011) & (0.011) & (0.009) & (0.009) & (0.009) & (0.009) & (0.007) & (0.007) \\
& & & & & & & & \\
CVAP\_EST\_White & 0.007 & 0.007 & $-$0.002 & $-$0.002 & $-$0.002 & $-$0.002 & $-$0.001 & $-$0.001 \\
& (0.004) & (0.004) & (0.004) & (0.004) & (0.004) & (0.004) & (0.003) & (0.003) \\
& & & & & & & & \\
CVAP\_EST\_African\_American & $-$0.007 & $-$0.007 & $-$0.002 & $-$0.002 & $-$0.002 & $-$0.002 & $-$0.001 & $-$0.001 \\
& (0.006) & (0.006) & (0.005) & (0.005) & (0.005) & (0.005) & (0.004) & (0.004) \\
& & & & & & & & \\
CVAP\_EST\_NonWhite & $-$0.020 & $-$0.020 & 0.006 & 0.006 & 0.006 & 0.006 & 0.002 & 0.002 \\
& (0.016) & (0.016) & (0.014) & (0.014) & (0.014) & (0.014) & (0.010) & (0.010) \\
& & & & & & & & \\
Constant & $-$23.099 & $-$23.099 & 5.245 & 5.245 & 5.244 & 5.241 & 1.899 & 1.898 \\
& (14.561) & (14.561) & (12.598) & (12.599) & (12.608) & (12.609) & (8.972) & (8.972) \\
& & & & & & & & \\
\hline \\[-1.8ex]
Observations & 9,594 & 9,594 & 9,594 & 9,594 & 9,594 & 9,594 & 9,594 & 9,594 \\
R$^{2}$ & 0.778 & 0.778 & 0.660 & 0.660 & 0.659 & 0.659 & 0.664 & 0.664 \\
Adjusted R$^{2}$ & 0.654 & 0.654 & 0.468 & 0.468 & 0.468 & 0.468 & 0.476 & 0.476 \\
Residual Std. Error & 0.403 (df = 6145) & 0.403 (df = 6144) & 0.348 (df = 6145) & 0.348 (df = 6144) & 0.349 (df = 6145) & 0.349 (df = 6144) & 0.248 (df = 6145) & 0.248 (df = 6144) \\
F Statistic & 6.250$^{***}$ (df = 3448; 6145) & 6.249$^{***}$ (df = 3449; 6144) & 3.452$^{***}$ (df = 3448; 6145) & 3.450$^{***}$ (df = 3449; 6144) & 3.443$^{***}$ (df = 3448; 6145) & 3.442$^{***}$ (df = 3449; 6144) & 3.524$^{***}$ (df = 3448; 6145) & 3.523$^{***}$ (df = 3449; 6144) \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{8}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}
\end{longtable}
\end{center}
\end{document}
The outputted PDF, however, looks like this:
Where both the bottom and right side of the table are cut off. I tried using longtable as you can see, but that doesn't seem to do much. I'm not sure if I need to rotate the table, or shift the table right, or allow for extra pages, or what.
A couple of ideas:
don't use a tabular inside the longtable. Longtable is meant to be used instead and not around a tabular
if your table is already too big to fit the page, don't make it even bigger by manual adding extracolsep
use the paper in landscape to get more space
reduce the margins
use fixed width columns to force line breaks to make the columns smaller
the variables names in the first columns are very long and latex won't be able to hyphenate them automatically. Define possible break points
place the df values in a new row to make the columns smaller
if this all does not help, use bigger paper or split the table in multiple tables with fewer columns
\documentclass{article}
\usepackage{longtable}
\usepackage[landscape,hmargin=0.5cm]{geometry}
\begin{document}
\begin{longtable}{p{4.6cm}*{8}{p{1.7cm}}}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{8}{c}{\textit{Dependent variable:}} \\
\cline{2-9}
\\[-1.8ex] & \multicolumn{2}{c}{Female} & \multicolumn{4}{c}{NonWhite} & \multicolumn{2}{c}{Black.or.Afr.Am.} \\
\\[-1.8ex] & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8)\\
\hline \\[-1.8ex]
factor(\-ELECTORAL\-\_TERM)2018 & 0.104$^{***}$ & 0.104$^{***}$ & $-$0.104$^{***}$ & $-$0.104$^{***}$ & $-$0.103$^{***}$ & $-$0.103$^{***}$ & $-$0.047$^{***}$ & $-$0.047$^{***}$ \\
& (0.010) & (0.010) & (0.009) & (0.009) & (0.009) & (0.009) & (0.006) & (0.006) \\
& & & & & & & & \\
factor(\-ELECTORAL\-\_TERM)2019 & 0.129$^{***}$ & 0.128$^{***}$ & 0.088$^{***}$ & 0.088$^{***}$ & 0.092$^{***}$ & 0.092$^{***}$ & 0.063$^{***}$ & 0.063$^{***}$ \\
& (0.011) & (0.011) & (0.009) & (0.010) & (0.009) & (0.009) & (0.007) & (0.007) \\
& & & & & & & & \\
WOMENS\-\_RIGHTS\-\_PROTEST & $-$0.016 & $-$0.025 & & & & & & \\
& (0.018) & (0.019) & & & & & & \\
& & & & & & & & \\
WOMENS\-\_RIGHTS\-\_COUNTERPROTEST & & 0.052 & & & & & & \\
& & (0.043) & & & & & & \\
& & & & & & & & \\
RACIAL\-\_PROTEST & & & 0.032$^{***}$ & 0.032$^{***}$ & & & 0.020$^{***}$ & 0.021$^{***}$ \\
& & & (0.010) & (0.011) & & & (0.007) & (0.008) \\
& & & & & & & & \\
RACIAL\-\_COUNTERPROTEST & & & & 0.001 & & & & $-$0.007 \\
& & & & (0.039) & & & & (0.028) \\
& & & & & & & & \\
OTHER\-\_IDENTITY\-\_PROTEST & & & & & 0.057 & 0.068 & & \\
& & & & & (0.077) & (0.078) & & \\
& & & & & & & & \\
OTHER\-\_IDENTITY\-\_COUNTERPROTEST & & & & & & $-$0.116 & & \\
& & & & & & (0.156) & & \\
& & & & & & & & \\
Republican.Party & 0.061$^{***}$ & 0.061$^{***}$ & 0.068$^{***}$ & 0.068$^{***}$ & 0.068$^{***}$ & 0.068$^{***}$ & 0.027$^{***}$ & 0.027$^{***}$ \\
& (0.014) & (0.014) & (0.012) & (0.012) & (0.012) & (0.012) & (0.009) & (0.009) \\
& & & & & & & & \\
Democratic.Party & 0.386$^{***}$ & 0.386$^{***}$ & 0.162$^{***}$ & 0.162$^{***}$ & 0.162$^{***}$ & 0.162$^{***}$ & 0.083$^{***}$ & 0.083$^{***}$ \\
& (0.016) & (0.016) & (0.014) & (0.014) & (0.014) & (0.014) & (0.010) & (0.010) \\
& & & & & & & & \\
CIT\-\_EST\-\_White & $-$0.002 & $-$0.002 & 0.001 & 0.001 & 0.001 & 0.001 & 0.0002 & 0.0002 \\
& (0.002) & (0.002) & (0.001) & (0.001) & (0.001) & (0.001) & (0.001) & (0.001) \\
& & & & & & & & \\
CIT\-\_EST\-\_African\-\_American & 0.007$^{**}$ & 0.007$^{**}$ & 0.001 & 0.001 & 0.001 & 0.001 & 0.0005 & 0.0005 \\
& (0.004) & (0.004) & (0.003) & (0.003) & (0.003) & (0.003) & (0.002) & (0.002) \\
& & & & & & & & \\
CIT\-\_EST\-\_NonWhite & 0.014 & 0.014 & $-$0.004 & $-$0.004 & $-$0.004 & $-$0.004 & $-$0.002 & $-$0.002 \\
& (0.011) & (0.011) & (0.009) & (0.009) & (0.009) & (0.009) & (0.007) & (0.007) \\
& & & & & & & & \\
CVAP\-\_EST\-\_White & 0.007 & 0.007 & $-$0.002 & $-$0.002 & $-$0.002 & $-$0.002 & $-$0.001 & $-$0.001 \\
& (0.004) & (0.004) & (0.004) & (0.004) & (0.004) & (0.004) & (0.003) & (0.003) \\
& & & & & & & & \\
CVAP\-\_EST\-\_African\-\_American & $-$0.007 & $-$0.007 & $-$0.002 & $-$0.002 & $-$0.002 & $-$0.002 & $-$0.001 & $-$0.001 \\
& (0.006) & (0.006) & (0.005) & (0.005) & (0.005) & (0.005) & (0.004) & (0.004) \\
& & & & & & & & \\
CVAP\-\_EST\-\_NonWhite & $-$0.020 & $-$0.020 & 0.006 & 0.006 & 0.006 & 0.006 & 0.002 & 0.002 \\
& (0.016) & (0.016) & (0.014) & (0.014) & (0.014) & (0.014) & (0.010) & (0.010) \\
& & & & & & & & \\
Constant & $-$23.099 & $-$23.099 & 5.245 & 5.245 & 5.244 & 5.241 & 1.899 & 1.898 \\
& (14.561) & (14.561) & (12.598) & (12.599) & (12.608) & (12.609) & (8.972) & (8.972) \\
& & & & & & & & \\
\hline \\[-1.8ex]
Observations & 9,594 & 9,594 & 9,594 & 9,594 & 9,594 & 9,594 & 9,594 & 9,594 \\
R$^{2}$ & 0.778 & 0.778 & 0.660 & 0.660 & 0.659 & 0.659 & 0.664 & 0.664 \\
Adjusted R$^{2}$ & 0.654 & 0.654 & 0.468 & 0.468 & 0.468 & 0.468 & 0.476 & 0.476 \\
Residual Std. Error & 0.403 & 0.403 & 0.348 & 0.348 & 0.349 & 0.349 & 0.248 & 0.248 \\
df & xxx & xxx & xxx & xxx & xxx & xxx & xxx & xxx \\
F Statistic & 6.250$^{***}$ & 6.249$^{***}$ & 3.452$^{***}$ & 3.450$^{***}$ & 3.443$^{***}$ & 3.442$^{***}$ & 3.524$^{***}$ & 3.523$^{***}$ \\
df & xxx & xxx & xxx & xxx & xxx & xxx & xxx & xxx \\
\hline
\hline \\[-1.8ex]
\textit{Note:} & \multicolumn{8}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{longtable}
\end{document}

How to group regressions by model in LaTeX output table in Stata esttab

I am trying to have it so I have 3 different models each with two regressions underneath of them. Ideally I'd like the table columns to be organized like
Group 1 Group 2 Group 3
Reg1 Reg2 Reg3 Reg4 Reg5 Reg6
I tried coopting the code from this link but got an error saying
0.0179 & -0.0149\sym {**} & 0.0320 & 0.00967 & 0.0320 & 0.00967 \\ & \ETC.
! File ended while scanning use of \###cmidrule.
<inserted text>
\par
<*> test.tex
I suspect you have forgotten a `}', causing me
to read past where you wanted me to stop.
I'll try to recover; but if the error is serious,
you'd better type `E' or `X' now and fix your file.
My code with all packages I'm using is
\documentclass[AER,reviewmode]{AEA}
\usepackage{natbib}
\usepackage{subfiles}
\usepackage{graphicx}
\graphicspath{{images/}{../images/}}
\usepackage{amsmath,amsfonts}
\usepackage{mathptmx}
\usepackage{hyperref}
\usepackage{booktabs}
\usepackage{dcolumn}
\begin{document}
\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{IV Regression Results, 1990\label{tab2}}
\begin{tabular}{l*{6}{D{.}{.}{-1}}}
\toprule
&\multicolumn{2}{c}{OLS} &\multicolumn{2}{c}{First Stage} &\multicolumn{2}{c}{Second Stage} \\\cmidrule(lr){#spam}\cmidrule(lr){#spam}\cmidrule(lr){#spam}
&\multicolumn{1}{c}{Isolation, 1990}&\multicolumn{1}{c}{Dissimilarity, 1990}&\multicolumn{1}{c}{Isolation, 1990}&\multicolumn{1}{c}{Dissimilarity, 1990}&\multicolumn{1}{c}{Isolation, 1990}&\multicolumn{1}{c}{Dissimilarity, 1990}\\
\midrule
Housing Supply Elasticity& -0.0179 & -0.0149\sym{**} & 0.0320 & 0.00967 & 0.0320 & 0.00967 \\
& (0.0116) & (0.00642) & (0.0373) & (0.0244) & (0.0373) & (0.0244) \\
\addlinespace
Population & 5.69e-08\sym{***}& 2.83e-08\sym{***}& 5.58e-08\sym{***}& 2.78e-08\sym{***}& 5.58e-08\sym{***}& 2.78e-08\sym{***}\\
& (1.26e-08) & (6.56e-09) & (1.05e-08) & (6.84e-09) & (1.05e-08) & (6.84e-09) \\
\addlinespace
WRLURI & -0.0757\sym{***}& -0.0503\sym{***}& 0.0742 & 0.0235 & 0.0742 & 0.0235 \\
& (0.0203) & (0.0126) & (0.109) & (0.0710) & (0.109) & (0.0710) \\
\addlinespace
Constant & 0.269\sym{***}& 0.580\sym{***}& 0.167\sym{**} & 0.530\sym{***}& 0.167\sym{**} & 0.530\sym{***}\\
& (0.0346) & (0.0198) & (0.0801) & (0.0524) & (0.0801) & (0.0524) \\
\midrule
Observations & 228 & 228 & 228 & 228 & 228 & 228 \\
\(R^{2}\) & 0.188 & 0.130 & . & 0.017 & . & 0.017 \\
Adjusted \(R^{2}\) & 0.177 & 0.119 & . & 0.004 & . & 0.004 \\
\bottomrule
\multicolumn{7}{l}{\footnotesize Standard errors in parentheses}\\
\multicolumn{7}{l}{\footnotesize \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\
\end{tabular}
\end{table}
\end{document}
It looks like you have a typo in your esttab command, though I can't tell for sure since you haven't posted that code. You want the range of columns in your \cmidrule statement, e.g. \cmidrule(lr){2-3}. The latex code you posted has \cmidrule(lr){#spam} instead.
Note the apparent typo (#spam for #span). According to the estout documentation, using #span within erepeat should return the column range that you're looking for. So fixing the typo should do it.
Instead of #span you should insert the range of columns the \cmidrule is supposed to span:
\documentclass[AER,reviewmode]{AEA}
%\documentclass{article}
\usepackage{natbib}
\usepackage{subfiles}
\usepackage{graphicx}
\graphicspath{{images/}{../images/}}
\usepackage{amsmath,amsfonts}
\usepackage{mathptmx}
\usepackage{hyperref}
\usepackage{booktabs}
\usepackage{dcolumn}
\begin{document}
\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{IV Regression Results, 1990\label{tab2}}
\begin{tabular}{l*{6}{D{.}{.}{-1}}}
\toprule
&\multicolumn{2}{c}{OLS} &\multicolumn{2}{c}{First Stage} &\multicolumn{2}{c}{Second Stage} \\\cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}
&\multicolumn{1}{c}{Isolation, 1990}&\multicolumn{1}{c}{Dissimilarity, 1990}&\multicolumn{1}{c}{Isolation, 1990}&\multicolumn{1}{c}{Dissimilarity, 1990}&\multicolumn{1}{c}{Isolation, 1990}&\multicolumn{1}{c}{Dissimilarity, 1990}\\
\midrule
Housing Supply Elasticity& -0.0179 & -0.0149\sym{**} & 0.0320 & 0.00967 & 0.0320 & 0.00967 \\
& (0.0116) & (0.00642) & (0.0373) & (0.0244) & (0.0373) & (0.0244) \\
\addlinespace
Population & 5.69e-08\sym{***}& 2.83e-08\sym{***}& 5.58e-08\sym{***}& 2.78e-08\sym{***}& 5.58e-08\sym{***}& 2.78e-08\sym{***}\\
& (1.26e-08) & (6.56e-09) & (1.05e-08) & (6.84e-09) & (1.05e-08) & (6.84e-09) \\
\addlinespace
WRLURI & -0.0757\sym{***}& -0.0503\sym{***}& 0.0742 & 0.0235 & 0.0742 & 0.0235 \\
& (0.0203) & (0.0126) & (0.109) & (0.0710) & (0.109) & (0.0710) \\
\addlinespace
Constant & 0.269\sym{***}& 0.580\sym{***}& 0.167\sym{**} & 0.530\sym{***}& 0.167\sym{**} & 0.530\sym{***}\\
& (0.0346) & (0.0198) & (0.0801) & (0.0524) & (0.0801) & (0.0524) \\
\midrule
Observations & 228 & 228 & 228 & 228 & 228 & 228 \\
\(R^{2}\) & 0.188 & 0.130 & . & 0.017 & . & 0.017 \\
Adjusted \(R^{2}\) & 0.177 & 0.119 & . & 0.004 & . & 0.004 \\
\bottomrule
\multicolumn{7}{l}{\footnotesize Standard errors in parentheses}\\
\multicolumn{7}{l}{\footnotesize \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(p<0.01\)}\\
\end{tabular}
\end{table}
\end{document}

Wrap column headers in a latex table exported by pandas.df.to_latex()

Problem: Tables output by Pandas.DataFrame.to_latex() do not wrap long column headers. The line_width= parameter has been removed as of Pandas 0.24.
Discussion:
I'm exporting many tables with the df.to_latex() command, for inclusion in a master document. Many of the tables have wordy column headers, mostly owing to the need to include parenthesized units.
The end result is a set of absurdly sparse tables that often won't fit on the page.
Here's a sample of the too-sparse generated Tex:
\begin{tabular}{lrrrrrr}
\toprule
{} & Odometer (km/y) & Fuel (L/y) & Elec (kWh/y) & Economy (L/100km) & GHG (kg CO2e) & GHG (g/km) \\
Type & & & & & & \\
\midrule
Type 1 & 70753.62 & 9721.06 & 0.00 & 13.74 & 21386.33 & 302.26 \\
Type RRR & 56167.39 & 5285.40 & 1627.60 & 9.41 & 11642.54 & 207.28 \\
Type X & 195756.35 & 42957.04 & 0.00 & 21.94 & 94505.48 & 482.77 \\
Type Huh & 187384.66 & 18118.07 & 73.07 & 9.67 & 39860.40 & 212.72 \\
\bottomrule
\end{tabular}
Questions:
Is there an alternate method of specifying wrapping in my generated .tex?
Alternately, is there latex magic that will let my master latex document \input these tables and specify a wrap width?
The problem relies on the way columns are declared. When specifying 'r', you say 'do a column wide enough for the larger cell and right aligned'.
What you need to do is use the column_format argument to give a width of the column with p{width}, in which case columns items will be formatted as a paragraph of the given width with line breaking as required.
E.g., the Pandas command:
df.to_latex(column_format='lp{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}')
Should result in an output .tex file that looks more-or-less like this:
\documentclass{article}
\begin{document}
\begin{tabular}{lp{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}}
\hline
{} & Odometer (km/y) & Fuel (L/y) & Elec (kWh/y) & Economy (L/100km) & GHG (kg CO2e) & GHG (g/km) \\
Type & & & & & & \\
\hline
Type 1 & 70753.62 & 9721.06 & 0.00 & 13.74 & 21386.33 & 302.26 \\
Type RRR & 56167.39 & 5285.40 & 1627.60 & 9.41 & 11642.54 & 207.28 \\
Type X & 195756.35 & 42957.04 & 0.00 & 21.94 & 94505.48 & 482.77 \\
Type Huh & 187384.66 & 18118.07 & 73.07 & 9.67 & 39860.40 & 212.72 \\
\hline
\end{tabular}
\bigskip
If you want to control line breaking, just add a $\backslash${newline} where you want.
\begin{tabular}{lp{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}}
\hline
{} & Odometer\newline (km/y) & Fuel \newline (L/y) & Elec\newline (kWh/y) & Economy\newline (L/100km) & GHG\newline (kg CO2e) & GHG\newline (g/km) \\
Type & & & & & & \\
\hline
Type 1 & 70753.62 & 9721.06 & 0.00 & 13.74 & 21386.33 & 302.26 \\
Type RRR & 56167.39 & 5285.40 & 1627.60 & 9.41 & 11642.54 & 207.28 \\
Type X & 195756.35 & 42957.04 & 0.00 & 21.94 & 94505.48 & 482.77 \\
Type Huh & 187384.66 & 18118.07 & 73.07 & 9.67 & 39860.40 & 212.72 \\
\hline
\end{tabular}
\bigskip
You can also consider adding an extra row for the units.
\begin{tabular}{lp{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}p{1.8cm}}
\hline
{} & Odometer & Fuel & Elec & Economy & GHG & GHG \\
Type & (km/y) & (L/y) & (kWh/y) & (L/100km) & (kg CO2e) & (g/km) \\
\hline
Type 1 & 70753.62 & 9721.06 & 0.00 & 13.74 & 21386.33 & 302.26 \\
Type RRR & 56167.39 & 5285.40 & 1627.60 & 9.41 & 11642.54 & 207.28 \\
Type X & 195756.35 & 42957.04 & 0.00 & 21.94 & 94505.48 & 482.77 \\
Type Huh & 187384.66 & 18118.07 & 73.07 & 9.67 & 39860.40 & 212.72 \\
\hline
\end{tabular}
\end{document}

LaTex large table

I'm trying to make a large table fit onto a normal A4 in LaTeX, but the table is too wide and too long to fit onto the page.
Is there any way to either resize or edit the table so that it still looks good?
This is the LaTeX code:
\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{lrrrrrr}
\hline
\multicolumn{1}{r}{1.000 kr.} & Gennemsnit & Median & Maximum & Minimum & Std. Afv. & Obs \\ \hline
\multicolumn{7}{l}{} \\
\multicolumn{7}{l}{\textit{Gruppe 1 - arbejdende kapital over 75 mia. kr.}} \\ \hline \\
LLP & 2.021.677 & 1.020.805 & 21.118.106 & -1.161.518 & 3.757.255 & 55 \\
ASSETS & 677.772.504 & 243.796.860 & 2.678.868.060 & 84.162.315 & 799.425.914 & 55 \\
LLR & 7.412.215 & 3.795.000 & 38.291.000 & 29.000 & 10.139.636 & 55 \\
NPL & 10.886.748 & 3.141.000 & 62.634.000 & 3.000 & 17.167.682 & 55 \\
ΔNPL & 740.568 & 272.500 & 38.934.000 & -21.082.000 & 7.046.437 & 50 \\
LOANS & 274.996.591 & 113.144.318 & 1.120.719.254 & 19.483.778 & 335.501.730 & 55 \\
AUDITFEE & 2.705 & 1.763 & 9.000 & 1.000 & 2.472 & 51 \\
CEO & 8,4 & 7,0 & 27,0 & 1,0 & 7,0 & 55 \\
Tier\_1 ratio & 13,66\% & 13,10\% & 25,10\% & 7,00\% & 4,86\% & 55 \\
\multicolumn{7}{l}{} \\
\multicolumn{7}{l}{\textit{Gruppe 2 - arbejdende kapital over 12 mia. kr.}} \\ \hline \\
LLP & 194.863 & 129.306 & 1.514.755 & -173.648 & 266.366 & 121 \\
ASSETS & 20.061.790 & 13.692.504 & 79.691.370 & 1.947.303 & 17.403.150 & 121 \\
LLR & 682.680 & 465.600 & 3.733.900 & -23.590 & 710.980 & 121 \\
NPL & 1.771.651 & 1.152.917 & 8.494.802 & 19.925 & 1.821.055 & 120 \\
ΔNPL & 171.948 & 135.632 & 2.357.300 & -7.369.011 & 948.672 & 109 \\
LOANS & 12.093.382 & 8.757.828 & 44.153.113 & 1.971.284 & 9.234.746 & 121 \\
AUDITFEE & 836 & 756 & 2.769 & 275 & 1.642 & 121 \\
CEO & 10,4 & 8,0 & 30,0 & 1,0 & 6,9 & 121 \\
Tier\_1 ratio & 14,57\% & 15,20\% & 20,90\% & 5,60\% & 3,82\% & 121 \\
& \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
\multicolumn{7}{l}{\textit{Gruppe 3 - arbejdende kapital over 500 mio. kr.}} \\ \hline
\multicolumn{7}{l}{} \\
LLP & 52.570 & 11.572 & 1.794.700 & -34.671 & 153.635 & 420 \\
ASSETS & 5.112.874 & 2.219.054 & 131.547.004 & 204.893 & 13.932.241 & 421 \\
LLR & 166.170 & 51.431 & 2.424.900 & -9.916 & 336.828 & 386 \\
NPL & 359.430 & 104.147 & 7.797.700 & 700 & 810.633 & 407 \\
ΔNPL & 39.559 & 7.778 & 2.973.200 & -3.330.200 & 333.519 & 365 \\
LOANS & 2.761.302 & 1.318.042 & 66.833.444 & 89.680 & 7.150.800 & 421 \\
AUDITFEE & 475 & 399 & 2.600 & 82 & 302 & 411 \\
CEO & 11,1 & 9,0 & 36,0 & 1,0 & 8,8 & 416 \\
Tier\_1 ratio & 17,67\% & 16,40\% & 86,30\% & 4,10\% & 7,56\% & 418 \\
& \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
\multicolumn{7}{l}{\textit{Gruppe 4 - arbejdende kapital under 500 mio. kr.}} \\ \hline
\multicolumn{7}{l}{} \\
LLP & 1.088 & 332 & 16.274 & -6.521 & 2.359 & 182 \\
ASSETS & 193.815 & 188.888 & 506.999 & 15.074 & 122.881 & 186 \\
LLR & 6.284 & 3.725 & 41.694 & -84 & 7.727 & 183 \\
NPL & 13.401 & 8.887 & 99.212 & 69 & 15.348 & 166 \\
ΔNPL & 1.822 & 500 & 25.455 & -10.545 & 5.636 & 148 \\
LOANS & 111.274 & 102.792 & 287.727 & 3.712 & 77.169 & 186 \\
AUDITFEE & 144 & 121 & 1.584 & 18 & 146 & 185 \\
CEO & 12,0 & 11,0 & 30,0 & 1,0 & 7,6 & 186 \\
Tier\_1 ratio & 23,19\% & 22,60\% & 47,80\% & -0,20\% & 7,85\% & 186
\end{tabular}
\end{table}
I'm very new to LaTeX so if you need anymore information please ask and I'll supply.
The margins for the document is:
\setmarginsrb { 1.5in} % left margin
{ 0.6in} % top margin
{ 1.0in} % right margin
{ 1in} % bottom margin
{ 20pt} % head height
{0.25in} % head sep
{ 9pt} % foot height
{ 0.3in} % foot sep
I'm using the premium version of sharelatex.com
You can use \resizebox command from graphicx package. See the following code:
\documentclass{article}
\usepackage{blindtext}
\usepackage{graphicx}
\usepackage{vmargin}
\setmarginsrb { 1.5in} % left margin
{ 0.6in} % top margin
{ 1.0in} % right margin
{ 1in} % bottom margin
{ 20pt} % head height
{0.25in} % head sep
{ 9pt} % foot height
{ 0.3in} % foot sep
\begin{document}
\begin{table}[t!]
\centering
\caption{My caption}
\label{my-label}
\resizebox{\textwidth}{!}{%
\begin{tabular}{lrrrrrr}
\hline
\multicolumn{1}{r}{1.000 kr.} & Gennemsnit & Median & Maximum & Minimum & Std. Afv. & Obs \\ \hline
\multicolumn{7}{l}{} \\
\multicolumn{7}{l}{\textit{Gruppe 1 - arbejdende kapital over 75 mia. kr.}} \\ \hline \\
LLP & 2.021.677 & 1.020.805 & 21.118.106 & -1.161.518 & 3.757.255 & 55 \\
ASSETS & 677.772.504 & 243.796.860 & 2.678.868.060 & 84.162.315 & 799.425.914 & 55 \\
LLR & 7.412.215 & 3.795.000 & 38.291.000 & 29.000 & 10.139.636 & 55 \\
NPL & 10.886.748 & 3.141.000 & 62.634.000 & 3.000 & 17.167.682 & 55 \\
NPL & 740.568 & 272.500 & 38.934.000 & -21.082.000 & 7.046.437 & 50 \\
LOANS & 274.996.591 & 113.144.318 & 1.120.719.254 & 19.483.778 & 335.501.730 & 55 \\
AUDITFEE & 2.705 & 1.763 & 9.000 & 1.000 & 2.472 & 51 \\
CEO & 8,4 & 7,0 & 27,0 & 1,0 & 7,0 & 55 \\
Tier\_1 ratio & 13,66\% & 13,10\% & 25,10\% & 7,00\% & 4,86\% & 55 \\
\multicolumn{7}{l}{} \\
\multicolumn{7}{l}{\textit{Gruppe 2 - arbejdende kapital over 12 mia. kr.}} \\ \hline \\
LLP & 194.863 & 129.306 & 1.514.755 & -173.648 & 266.366 & 121 \\
ASSETS & 20.061.790 & 13.692.504 & 79.691.370 & 1.947.303 & 17.403.150 & 121 \\
LLR & 682.680 & 465.600 & 3.733.900 & -23.590 & 710.980 & 121 \\
NPL & 1.771.651 & 1.152.917 & 8.494.802 & 19.925 & 1.821.055 & 120 \\
NPL & 171.948 & 135.632 & 2.357.300 & -7.369.011 & 948.672 & 109 \\
LOANS & 12.093.382 & 8.757.828 & 44.153.113 & 1.971.284 & 9.234.746 & 121 \\
AUDITFEE & 836 & 756 & 2.769 & 275 & 1.642 & 121 \\
CEO & 10,4 & 8,0 & 30,0 & 1,0 & 6,9 & 121 \\
Tier\_1 ratio & 14,57\% & 15,20\% & 20,90\% & 5,60\% & 3,82\% & 121 \\
& \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
\multicolumn{7}{l}{\textit{Gruppe 3 - arbejdende kapital over 500 mio. kr.}} \\ \hline
\multicolumn{7}{l}{} \\
LLP & 52.570 & 11.572 & 1.794.700 & -34.671 & 153.635 & 420 \\
ASSETS & 5.112.874 & 2.219.054 & 131.547.004 & 204.893 & 13.932.241 & 421 \\
LLR & 166.170 & 51.431 & 2.424.900 & -9.916 & 336.828 & 386 \\
NPL & 359.430 & 104.147 & 7.797.700 & 700 & 810.633 & 407 \\
NPL & 39.559 & 7.778 & 2.973.200 & -3.330.200 & 333.519 & 365 \\
LOANS & 2.761.302 & 1.318.042 & 66.833.444 & 89.680 & 7.150.800 & 421 \\
AUDITFEE & 475 & 399 & 2.600 & 82 & 302 & 411 \\
CEO & 11,1 & 9,0 & 36,0 & 1,0 & 8,8 & 416 \\
Tier\_1 ratio & 17,67\% & 16,40\% & 86,30\% & 4,10\% & 7,56\% & 418 \\
& \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{1}{l}{} \\
\multicolumn{7}{l}{\textit{Gruppe 4 - arbejdende kapital under 500 mio. kr.}} \\ \hline
\multicolumn{7}{l}{} \\
LLP & 1.088 & 332 & 16.274 & -6.521 & 2.359 & 182 \\
ASSETS & 193.815 & 188.888 & 506.999 & 15.074 & 122.881 & 186 \\
LLR & 6.284 & 3.725 & 41.694 & -84 & 7.727 & 183 \\
NPL & 13.401 & 8.887 & 99.212 & 69 & 15.348 & 166 \\
NPL & 1.822 & 500 & 25.455 & -10.545 & 5.636 & 148 \\
LOANS & 111.274 & 102.792 & 287.727 & 3.712 & 77.169 & 186 \\
AUDITFEE & 144 & 121 & 1.584 & 18 & 146 & 185 \\
CEO & 12,0 & 11,0 & 30,0 & 1,0 & 7,6 & 186 \\
Tier\_1 ratio & 23,19\% & 22,60\% & 47,80\% & -0,20\% & 7,85\% & 186
\end{tabular}}
\end{table}
\blindtext
\end{document}

Resize threepartable for beamer

I would like to use the resize function from thegraphicx package to fit a large threeparttable into a beamer presentation, however I can't seem to get code from examples that you use tabular environments to work. Using the following code:
\documentclass{beamer}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{graphicx,threeparttable,caption}
\begin{document}
\begin{frame}
\begin{table}[h!] \small
\begin{threeparttable}
\caption{\textbf{Descriptive Statistics} \label{tab:descriptivestats}}
\begin{tabular}{ p{3.5cm} p{1.0cm} p{1.0cm} p{1.0cm} p{1.0cm} p{1.0cm}}
\hline
\textbf{Var} & \textbf{Min} & \textbf{Max} & \textbf{Mean} & \textbf{SD} & \textit{N} \\
\hline
\hline
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
XYZ & 0 & 1 & 2 & 3 & 4 \\
\hline
\hline
\end{tabular}
\begin{tablenotes}
\footnotesize
\item \textbf{Note:} XYZ
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{frame}
\end{document}
Any thoughts on how to get this to fit, using resize or another command?
One first possible solution is to use \tiny instead of \small: it makes the job in this specific case, despite it may be puzzling to manage if you have more than one such matrices in your document.
The second way (reference) is probably more appropriate and systematic:
\documentclass{beamer}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage{graphicx,threeparttable,caption}
\begin{document}
\begin{frame}
\begin{table}[h!]
\resizebox{.75\textwidth}{!}{%% begins here
\begin{threeparttable}
%% contents of threeparttable
\end{threeparttable}
}%% ends here
\end{table}
\end{frame}
\end{document}