我在使用 longtable 时遇到了一个非常奇怪的错误。
当我在独立文件中运行以下代码时,我得到了一个完美对齐的整洁表格(见图)。
\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}
但是,当我在实际文件中运行完全相同的代码时,它会给我一个奇怪的表格,最后一列延伸到右边距(见图)。我在序言中使用了 longtable 和 geometry 包。但是,我正在写一篇论文,并使用为我的研究所设计的 LaTeX 模板。这会造成问题吗?如果有的话,请提出解决方法。谢谢!
答案1
回答您的具体问题,我将展示两种方法来控制表的宽度,同时对代码进行最少的更改。
(假设你不能使用文档类中的几何包,但您可以在宽表格或图形中使用边距。否则,将需要考虑其他方法,也许是横向的。)
使用该包
array
并定义一种新的列类型,该列将应用于第一列,以限制其宽度。长行将由 TeX 段落算法分成几行。使用该包并在您认为语法最正确的地方
makecell
自行进行剪切(使用里面的命令\thead{<content>}
和使用)。\\
我添加了包showframe
来显示文本区域和边距
首选:使用array
(表中更改一行)
......
\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}\\
......
你会得到
第二种选择:使用makecell
(表中更改了八行)
使用此代码
\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}
答案2
我会重新设计你的表格如下:
- 对于第一列,将使用
p{...}
列类型并在单元格中插入\multirow
文本 - 对于带有数字的列将使用包
S
中定义的列类型siunitx
- 对于表格注释将使用包
tablenotes
中定义的:threetablex
\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}
(我只美化了第一页的部分表格)
经过至少两次编译后得到: