1

I am getting a really strange error while using longtable.

When I run the following code in a standalone file, I get a neat table with perfect alignment (see picture).

\documentclass{article}

\usepackage[utf8]{inputenc} \usepackage{longtable} \usepackage{geometry}

\author{Parul Gupta} \date{March 2021}

\begin{document}

\maketitle

\section{Introduction} { \def\sym#1{\ifmmode^{#1}\else(^{#1})\fi} \begin{longtable}{l{4}{c}} \caption{Private tutoring demand, hurdle model (IHDS, 2012) \label{ihds}}\ \hline\hline\endfirsthead\hline\endhead\hline\endfoot\endlastfoot &\multicolumn{2}{c}{Rural}&\multicolumn{2}{c}{Urban}\ \cline{2-3} \cline{4-5} &\multicolumn{1}{c}{1st stage}&\multicolumn{1}{c}{2nd stage}&\multicolumn{1}{c}{1st stage}&\multicolumn{1}{c}{2nd stage}\ &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&\multicolumn{1}{c}{(4)}\ &\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}\ \hline Male 1 & 0.0372\sym{}& 0.0698\sym{}& 0.0365\sym{}& 0.0451 \ & (8.45) & (3.30) & (4.37) & (1.69) \ [1em] Attends government school=1& -0.0453\sym{}& -0.122\sym{} & -0.0236 & 0.0403 \ & (-6.03) & (-3.01) & (-1.83) & (1.03) \ [1em] Distance from school (km)& 0.000139 & 0.00576\sym{} &-0.000536 & 0.00651\sym{} \ & (0.29) & (2.95) & (-0.38) & (2.18) \ [1em] classes=1 & 0.0278\sym{*}& 0.171\sym{}& 0.00682 & 0.216\sym{}\ & (5.00) & (5.20) & (0.60) & (5.96) \ [1em] classes=2 & 0.111\sym{}& 0.511\sym{}& 0.0631\sym{}& 0.518\sym{}\ & (13.69) & (13.16) & (4.70) & (12.67) \ [1em] EQ4 2.11 Homework hours/week& 0.00139\sym{}& 0.00303\sym{} & 0.00273\sym{} & 0.00691\sym{}\ & (3.34) & (2.75) & (3.28) & (3.86) \ [1em] EQ4 2.13 Days/month absent& -0.00151\sym{} &-0.0000962 & -0.00209 & -0.00328 \ & (-2.93) & (-0.05) & (-1.85) & (-0.98) \ [1em] EQ5 2.24 Scholarship (rupees)&0.0000165\sym{*}&0.0000475\sym{}&-0.00000440 &0.0000617 \ & (3.33) & (5.51) & (-0.55) & (1.58) \ [1em] Attends English medium school=1& 0.0157 & 0.151\sym{} & 0.0443\sym{} & 0.166\sym{}\ & (1.43) & (3.11) & (2.95) & (3.96) \ [1em] Muslim 2 & 0.0110 & -0.0243 & 0.0349\sym{} & -0.0548 \ & (1.16) & (-0.48) & (2.49) & (-1.14) \ [1em] Others 3 & 0.0184 & 0.0783 & -0.0211 & -0.0877 \ & (1.27) & (1.10) & (-0.94) & (-1.48) \ [1em] OBC & -0.00470 & -0.0141 & 0.0231 & -0.0353 \ & (-0.61) & (-0.44) & (1.70) & (-0.94) \ [1em] SC & -0.0178\sym{} & -0.0323 & -0.00242 & 0.000542 \ & (-2.16) & (-0.89) & (-0.15) & (0.01) \ [1em] ST & -0.0260\sym{*} & -0.0268 & 0.0273 & -0.0435 \ & (-2.14) & (-0.36) & (0.93) & (-0.41) \ [1em] Father's years of education& 0.00256\sym{}& 0.0202\sym{}& 0.00223 & 0.0122\sym{*} \ & (3.30) & (4.35) & (1.42) & (2.46) \ [1em] Mother's years of education& 0.00333\sym{}& 0.00209 &-0.000313 & 0.0157\sym{} \ & (4.14) & (0.51) & (-0.20) & (3.14) \ [1em] Yes 1 & -0.0176\sym{} & 0.0135 & -0.0301\sym{} & -0.0731 \ & (-3.03) & (0.40) & (-2.32) & (-1.94) \ [1em] No. of siblings in age group 0-5& -0.0113\sym{} & -0.0222 & -0.0244\sym{} & -0.0291 \ & (-2.28) & (-0.67) & (-2.26) & (-0.73) \ [1em] Eldest child=1 & 0.0275\sym{*}& 0.00254 & 0.0673\sym{}& 0.0524 \ & (6.50) & (0.12) & (8.05) & (1.95) \ [1em] HQ23 14. household expenditure /capita&0.000000725\sym{} &0.00000147\sym{} &0.000000744\sym{} &0.00000389\sym{}\ & (2.90) & (2.01) & (2.99) & (6.47) \ [1em] Average tuition participation in village& 0.00579\sym{}& & 0.00726\sym{}& \ & (42.37) & & (37.34) & \ [1em] Average tuition spending in village (Rs.)& & 0.000127\sym{}& &0.0000489\sym{}\ & & (9.10) & & (5.37) \ \hline State controls & Yes & & Yes & \ N & 20955 & 20955 & 9211 & 9211 \ \hline\hline \multicolumn{5}{l}{\footnotesize \textit{t} statistics in parentheses}\ \multicolumn{5}{l}{\footnotesize Source: IHDS, 2012}\ \multicolumn{5}{l}{\footnotesize Robust standard errors, clustered at PSU level}\ \multicolumn{5}{l}{\footnotesize \sym{} (p<0.05), \sym{} (p<0.01), \sym{*} (p<0.001)}\ \end{longtable} }

\end{document}

nice table

But when I run the exact same code in the actual file where I want it to be placed, it gives me a strange-looking table with the last column runnning into the right margin (see picture). I am using longtable and geometry packages in the preamble. However, I am writing a dissertation, and am using a LaTeX template designed for my institute. Could that be causing a problem? Please suggest a workaround, if any. Thank you!

not so nice table

PGupta
  • 681
  • 1
    Apparently your table is simply wider than the available space. For some general advice on how to make sure a table fits onto a page, you might want to take a look at the following question and its answer: My table doesn't fit; what are my options? – leandriis Mar 12 '21 at 16:54
  • The issue is, it works fine in one file but not in the other. So I doubt it is a table width problem. Have edited the question with a picture of the correct table. I tried shrinking the table using scalebox as well but it didn't work. – PGupta Mar 12 '21 at 16:59
  • Using scalebox on a table is never a good ide since this method will inevitably lead to inconsistent font sizes throughout the document, so better aviod scaling tables. – leandriis Mar 12 '21 at 17:05
  • Different documents can use different fonts, font sizes margins,... and other settings, all of which can have an influence on a table and might lead to a table that fits on the page in one document and overflows into the margin in another document. – leandriis Mar 12 '21 at 17:06
  • Yes, it appears to be a problem due to the document class. Once I change it, the table fits properly again. But I can't use article class and need to use the template for the thesis. Can I try something different that might work? Using a table and tabular also doesn't give a very nice table. – PGupta Mar 12 '21 at 17:09
  • I just tested the example code in your question. I assume, this is the one you claimed was working. Are you aware, that already with this code, your table does not fit into the textwidth? Upon compiling, I get multiple warnings such as "Overfull \hbox (48.32985pt too wide) ". – leandriis Mar 12 '21 at 17:47
  • What to do depends on how much too wide your table is. Is the thesis template publicly available somewhere? If not, please find out the widht of the textblock, the font and font size that is used in the template. From this document, you should also get multiple overfull box warnings. Which value do they show? – leandriis Mar 12 '21 at 17:49
  • The easiest way to make this specific table narrower is by allowing linebreaks in the comparably lenghty texts in the first column. Doing so should already result in a table that fits. – leandriis Mar 12 '21 at 17:51
  • @leandriis I get overfull badness but the code still works. Sharing the link to view https://www.overleaf.com/read/schvppqmzrnr

    And yes, the template is available publicly at https://github.com/Saipraneet/iitdthesis

    – PGupta Mar 13 '21 at 06:50

2 Answers2

1

I would redesign your table as follows:

  • for the first column would use p{...} column type and insert text in cels in \multirow cells
  • for columns with numbers would use S column type defined in the siunitx package
  • for table notes would use tablenotes defined in threetablex package:
\documentclass{article}
\usepackage{geometry}
\usepackage{booktabs, makecell, multirow, longtable}
\usepackage[referable]{threeparttablex}
\NewExpandableDocumentCommand\mcc{O{1}m}
    {\multicolumn{#1}{c}{#2}}
\NewExpandableDocumentCommand\mcr{O{1}m}
    {\multicolumn{#1}{r}{#2}}
\NewExpandableDocumentCommand\mrc{O{2}m}
    {\multirow[t]{#1}{=}{#2}}
\usepackage{siunitx}

\title{My longtable} \author{Parul Gupta} \date{March 2021}

\begin{document} \maketitle

\section{Introduction} \begingroup \sisetup{table-space-text-pre={(}, table-align-text-pre=false, table-space-text-post={$^{***}$}, table-align-text-post=false, input-open-uncertainty = , input-close-uncertainty = , %round-mode=places, %round-precision=4, table-format=-1.7 }

\def\sym#1{\ifmmode^{#1}\else(^{#1})\fi} \setlength\LTleft{0pt} \setlength\LTright{0pt} \setlength\tabcolsep{3pt} \begin{longtable}{@{\extracolsep{\fill}} @{\hspace{\tabcolsep}} >{\raggedright}p{11em} {4}{S}} \caption{Private tutoring demand, hurdle model (IHDS, 2012)} \label{ihds} \ \toprule & \mcc[2]{Rural} & \mcc[2]{Urban} \ \cmidrule{2-3} \cmidrule(l){4-5} & {\makecell{1st stage\ (1)}} & {\makecell{2nd stage\ (2)}} & {\makecell{1st stage\ (3)}} & {\makecell{2nd stage\ (4)}} \ \midrule \endfirsthead \toprule & \mcc[2]{Rural} & \mcc[2]{Urban} \ \cmidrule{2-3} \cmidrule(l){4-5} & {\makecell{1st stage\ (1)}} & {\makecell{2nd stage\ (2)}} & {\makecell{1st stage\ (3)}} & {\makecell{2nd stage\ (4)}} \ \midrule \endhead \midrule[\heavyrulewidth] \mcr[5]{Continued on the next page} \endfoot \bottomrule \endlastfoot \mrc{Male 1}
& 0.0372\sym{
} & 0.0698\sym{}
& 0.0365\sym{
} & 0.0451 \* & (8.45) & (3.30)
& (4.37) & (1.69) \ \addlinespace \mrc{Attends government school=1} & -0.0453\sym{
} & -0.122\sym{} & -0.0236 & 0.0403 \* & (-6.03) & (-3.01)
& (-1.83) & (1.03) \ \addlinespace \mrc{Distance from school (km)} & 0.000139 & 0.00576\sym{
} & -0.000536 & 0.00651\sym{} \* & (0.29) & (2.95)
& (-0.38) & (2.18) \ \addlinespace \mrc{classes=1}
& 0.0278\sym{*} & 0.171\sym{} & 0.00682 & 0.216\sym{} \* & (5.00) & (5.20)
& (0.60) & (5.96) \ \addlinespace \mrc{classes=2}
& 0.111\sym{
} & 0.511\sym{} & 0.0631\sym{} & 0.518\sym{} \* & (13.69) & (13.16)
& (4.70) & (12.67) \ \addlinespace \mrc{EQ4 2.11 Homework hours/week} & 0.00139\sym{
}& 0.00303\sym{} & 0.00273\sym{} & 0.00691\sym{} \* & (3.34) & (2.75)
& (3.28) & (3.86) \ \addlinespace \mrc{EQ4 2.13 Days/month absent} & -0.00151\sym{
} & -0.0000962
& -0.00209 & -0.00328 \ & (-2.93) & (-0.05)
& (-1.85) & (-0.98) \ \addlinespace \mrc{EQ5 2.24 Scholarship (rupees)} & 0.0000165\sym{*} & 0.0000475\sym{} & -0.00000440 & 0.0000617 \* & (3.33) & (5.51)
& (-0.55) & (1.58) \ \addlinespace \mrc{Attends English medium school=1} & 0.0157 & 0.151\sym{
} & 0.0443\sym{} & 0.166\sym{} \* & (1.43) & (3.11)
& (2.95) & (3.96) \ \addlinespace \mrc{Muslim 2}
& 0.0110 & -0.0243
& 0.0349\sym{} & -0.0548 \* & (1.16) & (-0.48)
& (2.49) & (-1.14) \ \addlinespace \mrc{Others 3}
& 0.0184 & 0.0783
& -0.0211 & -0.0877 \* & (1.27) & (1.10)
& (-0.94) & (-1.48) \ \addlinespace \mrc{OBC} & -0.00470 & -0.0141
& 0.0231 & -0.0353 \* & (-0.61) & (-0.44)
& (1.70) & (-0.94) \ \addlinespace \mrc{SC}
& -0.0178\sym{
} & -0.0323
& -0.00242 & 0.000542 \ & (-2.16) & (-0.89) & (-0.15) & (0.01) \ \addlinespace \mrc{ST}
& -0.0260\sym{*} & -0.0268 & 0.0273 & -0.0435 \ & (-2.14) & (-0.36) & (0.93) & (-0.41) \ \addlinespace \mrc{Father's years of education} & 0.00256\sym{
}& 0.0202\sym{} & 0.00223 & 0.0122\sym{*} \* & (3.30) & (4.35)
& (1.42) & (2.46) \ \addlinespace \mrc{Mother's years of education} & 0.00333\sym{
}& 0.00209 &-0.000313 & 0.0157\sym{} \ & (4.14) & (0.51) & (-0.20) & (3.14) \ \addlinespace \mrc{Yes 1}
& -0.0176\sym{
} & 0.0135 & -0.0301\sym{} & -0.0731 \ & (-3.03) & (0.40) & (-2.32) & (-1.94) \ \addlinespace \mrc{No. of siblings in age group 0-5} & -0.0113\sym{} & -0.0222 & -0.0244\sym{} & -0.0291 \ & (-2.28) & (-0.67) & (-2.26) & (-0.73) \ \addlinespace \mrc{Eldest child=1} & 0.0275\sym{*}& 0.00254 & 0.0673\sym{}& 0.0524 \ & (6.50) & (0.12) & (8.05) & (1.95) \ \addlinespace \mrc{HQ23 14. household expenditure /capita} &0.000000725\sym{} &0.00000147\sym{} &0.000000744\sym{} &0.00000389\sym{}\ & (2.90) & (2.01) & (2.99) & (6.47) \ \addlinespace \mrc{Average tuition participation in village} & 0.00579\sym{}& & 0.00726\sym{}& \ & (42.37) & & (37.34) & \ \addlinespace \mrc{Average tuition spending in village (Rs.)} & & 0.000127\sym{} & & 0.0000489\sym{} \ & & (9.10) & & (5.37) \ \addlinespace \mrc{State controls}
& {Yes} &
& {Yes} & \ N & {20955} & {20955}
& {9211} & {9211} \ \end{longtable} \vspace{-\baselineskip} \begin{tablenotes}[para]\footnotesize \note{Statistics in parentheses:\ Source: IHDS, 2012 Robust standard errors, clustered at PSU level} \ \item[
] (p<0.05), \item[**] (p<0.01), \item[***] (p<0.001). \end{tablenotes} \endgroup

\end{document}

(I prettify only part of table, which is on the first page)

which after at least two compilation gives:

enter image description here

Zarko
  • 296,517
1

Answering your specific question I will show two ways to control the wideness of the table with minimum changes in your code.

(Assuming you are not allowed to use the geometry package in your document class, but you are allowed to use the margins with wide tables or figures. Otherwise other approaches will be needed to be considered, maybe landscape.)

  1. Use the package array and define a new type of column that will be applied to the first column, to limit its width. Long lines will be separated into lines by the TeX paragraph algorithm.

  2. Use the makecell package and make the cuts yourself (with the command \thead{<content>} and using \\ inside) in the places that you consider most grammatically correct.

I added the package showframe to show the text area and the margins

First choice: using array (one line changed in the table)

......
\author{Parul Gupta}
\date{March 2021}

\usepackage{array} % <<< added \newcolumntype{P}[1]{>{\raggedright\arraybackslash}p{#1\textwidth}}

\begin{document}

%\maketitle

\section{Introduction} { \def\sym#1{\ifmmode^{#1}\else(^{#1})\fi} \begin{longtable}{P{0.3}*{4}{c}} %<<<< changed \caption{Private tutoring demand, hurdle model (IHDS, 2012) \label{ihds}}\ \hline\hline\endfirsthead\hline\endhead\hline\endfoot\endlastfoot &\multicolumn{2}{c}{Rural}&\multicolumn{2}{c}{Urban}\

......

you will get

A

Second choice: using makecell (eight lines changed in the table)

b

with this code

\documentclass{article}

\usepackage[utf8]{inputenc} \usepackage{longtable} %\usepackage{geometry}

\usepackage{showframe} % <<< to be commented

\usepackage{makecell} % <<<<<< added \renewcommand\theadalign{cl} \renewcommand\theadfont{\normalfont}

\author{Parul Gupta} \date{March 2021}

\begin{document}

%\maketitle

\section{Introduction} { \def\sym#1{\ifmmode^{#1}\else(^{#1})\fi} \begin{longtable}{l{4}{c}} \caption{Private tutoring demand, hurdle model (IHDS, 2012) \label{ihds}}\ \hline\hline\endfirsthead\hline\endhead\hline\endfoot\endlastfoot &\multicolumn{2}{c}{Rural}&\multicolumn{2}{c}{Urban}\ \cline{2-3} \cline{4-5} &\multicolumn{1}{c}{1st stage}&\multicolumn{1}{c}{2nd stage}&\multicolumn{1}{c}{1st stage}&\multicolumn{1}{c}{2nd stage}\ &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&\multicolumn{1}{c}{(4)}\ &\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}\ \hline Male 1 & 0.0372\sym{}& 0.0698\sym{}& 0.0365\sym{}& 0.0451 \ & (8.45) & (3.30) & (4.37) & (1.69) \ [1em] \thead{Attends\ government school=1}& -0.0453\sym{}& -0.122\sym{} & -0.0236 & 0.0403 \ & (-6.03) & (-3.01) & (-1.83) & (1.03) \ [1em] Distance from school (km)& 0.000139 & 0.00576\sym{} &-0.000536 & 0.00651\sym{} \ & (0.29) & (2.95) & (-0.38) & (2.18) \ [1em] classes=1 & 0.0278\sym{*}& 0.171\sym{}& 0.00682 & 0.216\sym{}\ & (5.00) & (5.20) & (0.60) & (5.96) \ [1em] classes=2 & 0.111\sym{}& 0.511\sym{}& 0.0631\sym{}& 0.518\sym{}\ & (13.69) & (13.16) & (4.70) & (12.67) \ [1em] \thead{EQ4 2.11 Homework \hours/week}& 0.00139\sym{}& 0.00303\sym{} & 0.00273\sym{} & 0.00691\sym{}\ & (3.34) & (2.75) & (3.28) & (3.86) \ [1em] \thead{EQ4 2.13 Days/month \absent}& -0.00151\sym{} &-0.0000962 & -0.00209 & -0.00328 \ & (-2.93) & (-0.05) & (-1.85) & (-0.98) \ [1em] \thead{EQ5 2.24 Scholarship\ (rupees)}&0.0000165\sym{*}&0.0000475\sym{}&-0.00000440 &0.0000617 \ & (3.33) & (5.51) & (-0.55) & (1.58) \ [1em] \thead{Attends English \medium school=1}& 0.0157 & 0.151\sym{} & 0.0443\sym{} & 0.166\sym{}\ & (1.43) & (3.11) & (2.95) & (3.96) \ [1em] Muslim 2 & 0.0110 & -0.0243 & 0.0349\sym{} & -0.0548 \ & (1.16) & (-0.48) & (2.49) & (-1.14) \ [1em] Others 3 & 0.0184 & 0.0783 & -0.0211 & -0.0877 \ & (1.27) & (1.10) & (-0.94) & (-1.48) \ [1em] OBC & -0.00470 & -0.0141 & 0.0231 & -0.0353 \ & (-0.61) & (-0.44) & (1.70) & (-0.94) \ [1em] SC & -0.0178\sym{} & -0.0323 & -0.00242 & 0.000542 \ & (-2.16) & (-0.89) & (-0.15) & (0.01) \ [1em] ST & -0.0260\sym{*} & -0.0268 & 0.0273 & -0.0435 \ & (-2.14) & (-0.36) & (0.93) & (-0.41) \ [1em] Father's years of education& 0.00256\sym{}& 0.0202\sym{}& 0.00223 & 0.0122\sym{*} \ & (3.30) & (4.35) & (1.42) & (2.46) \ [1em] Mother's years of education& 0.00333\sym{}& 0.00209 &-0.000313 & 0.0157\sym{} \ & (4.14) & (0.51) & (-0.20) & (3.14) \ [1em] Yes 1 & -0.0176\sym{} & 0.0135 & -0.0301\sym{} & -0.0731 \ & (-3.03) & (0.40) & (-2.32) & (-1.94) \ [1em] \thead{No. of siblings in \ age group 0-5}& -0.0113\sym{} & -0.0222 & -0.0244\sym{} & -0.0291 \ & (-2.28) & (-0.67) & (-2.26) & (-0.73) \ [1em] Eldest child=1 & 0.0275\sym{*}& 0.00254 & 0.0673\sym{}& 0.0524 \ & (6.50) & (0.12) & (8.05) & (1.95) \ [1em] \thead{HQ23 14. household \expenditure /capita}&0.000000725\sym{} &0.00000147\sym{} &0.000000744\sym{} &0.00000389\sym{}\ & (2.90) & (2.01) & (2.99) & (6.47) \ [1em] \thead{Average tuition \participation in village}& 0.00579\sym{}& & 0.00726\sym{}& \ & (42.37) & & (37.34) & \ [1em] \thead{Average tuition \spending in village (Rs.)}& & 0.000127\sym{}& &0.0000489\sym{}\ & & (9.10) & & (5.37) \ \hline State controls & Yes & & Yes & \ N & 20955 & 20955 & 9211 & 9211 \ \hline\hline \multicolumn{5}{l}{\footnotesize \textit{t} statistics in parentheses}\ \multicolumn{5}{l}{\footnotesize Source: IHDS, 2012}\ \multicolumn{5}{l}{\footnotesize Robust standard errors, clustered at PSU level}\ \multicolumn{5}{l}{\footnotesize \sym{} (p<0.05), \sym{} (p<0.01), \sym{*} (p<0.001)}\ \end{longtable} }

\end{document}

Simon Dispa
  • 39,141