为什么我的 table 中的竖线长度不同?

Why are the vertical lines in my table different lengths?

我在背面工作,我插入 table(使用表格)的垂直线长度不同。我附上了代码和输出图片。

具体来说,我想知道为什么“年”列两侧的两条线比其他所有线都长,以及为什么它们超出了顶部和底部的水平线。我还想知道为什么其他列两侧的垂直线不连续(请参阅每个作物名称下水平线下方和 table 顶部双水平线下方的间隙)。如果它很重要,这个 table 是在 RStudio 中使用 stargazer 生成的。我手动添加了垂直线。谢谢!

\renewcommand{\arraystretch}{1.5} % Default value: 1
\begin{table}[!htbp] \centering 
  \caption{Percent Change in Crop Acreage from Previous Year} 
  \label{tab:per_change} 
\begin{tabular}{@{\extracolsep{5pt}} |c|c|c|c|c|c|c|c|c|} 
\[-1.8ex]\hline 
\hline \[-1.8ex] 
 Year & Lettuce & Broccoli & Small Grains & Melons & Bermuda Grass & Sudan Grass & Alfalfa \ 
\hline \[-1.8ex]
2000 & -7.71 & 43.25 & 76.26 & 12.09 & 50.96 & -66.45 & 1.32 \ 
2001 & -24.64 & -29.44 & -37.04 & 2.21 & -23.49 & 132.62 & 2.82 \ 
2002 & -9.7 & 29.79 & 62.95 & -5.05 & 3.91 & -11.52 & 4.38 \ 
2003 & -18.12 & -12.48 & 1.3 & -15.28 & 10.09 & 36.02 & -11.08 \ 
2004 & 12.1 & 4.08 & 36.01 & 18.2 & -26.47 & 14.91 & -8.67 \ 
2005 & -10.28 & -18.09 & -63.03 & -43.14 & -10.9 & -70.08 & -8.1 \ 
2006 & -9.35 & 53.81 & 9.34 & 26.2 & 5.55 & 163.44 & 21.39 \ 
2007 & 15.44 & -16.14 & -5.99 & -2.34 & 4.24 & -21.09 & -7.59 \ 
2008 & -48.65 & -14.82 & 153.4 & -19.57 & -11.12 & 84.66 & 2.99 \ 
2009 & 38.36 & -18.14 & -34.69 & -18.32 & 51.68 & -64.19 & -22.09 \ 
2010 & 31.66 & 20.83 & -32.23 & 12.69 & -21.28 & 186.98 & 7.86 \ 
2011 & 10.09 & -3.4 & -26.37 & -13.14 & 0.1 & -64.27 & -1.15 \ 
2012 & -13.75 & 11.73 & 25.88 & 31.32 & 130 & 81.55 & 36.56 \ 
2013 & 1.81 & -8.99 & 105.56 & -16.27 & -27.86 & -39.41 & -7.47 \ 
2014 & -41.44 & -8.71 & -2.22 & -7.26 & -16.77 & -8.25 & 4.93 \ 
\hline \[-1.8ex] 
\end{tabular} 
\end{table} 

table generated by code

  • table 下方和上方的杂散线是由所有这些 \[-1.8ex] 造成的,即使没有线可以完成

  • 右边的杂散线来自 table (8) 中实际有多少列与你告诉 latex [ 中会有多少列不匹配=76=] (9).

  • 请重新考虑这样的 table 布局。使用垂直线看起来并不像专业人士所做的那样 table,请参阅 booktabs 包文档以获取更多灵感 http://mirrors.ctan.org/macros/latex/contrib/booktabs/booktabs.pdf

  • 另请查看 siunitx 包以正确对齐和格式化您的数字。至少,您不应将连字符滥用为减号。

  • 如果您的 table 已经太宽而无法放入可用的文本区域,您最后应该做的就是在列之间添加额外的宽度 space


您的固定 MWE:

\documentclass{article}
\usepackage{geometry}

\begin{document}

\renewcommand{\arraystretch}{1.5} % Default value: 1
\begin{table}[!htbp] \centering 
  \caption{Percent Change in Crop Acreage from Previous Year} 
  \label{tab:per_change} 
\begin{tabular}{@{\extracolsep{5pt}} |c|c|c|c|c|c|c|c|} 
\hline 
\hline
 Year & Lettuce & Broccoli & Small Grains & Melons & Bermuda Grass & Sudan Grass & Alfalfa \ 
\hline
2000 & -7.71 & 43.25 & 76.26 & 12.09 & 50.96 & -66.45 & 1.32 \ 
2001 & -24.64 & -29.44 & -37.04 & 2.21 & -23.49 & 132.62 & 2.82 \ 
2002 & -9.7 & 29.79 & 62.95 & -5.05 & 3.91 & -11.52 & 4.38 \ 
2003 & -18.12 & -12.48 & 1.3 & -15.28 & 10.09 & 36.02 & -11.08 \ 
2004 & 12.1 & 4.08 & 36.01 & 18.2 & -26.47 & 14.91 & -8.67 \ 
2005 & -10.28 & -18.09 & -63.03 & -43.14 & -10.9 & -70.08 & -8.1 \ 
2006 & -9.35 & 53.81 & 9.34 & 26.2 & 5.55 & 163.44 & 21.39 \ 
2007 & 15.44 & -16.14 & -5.99 & -2.34 & 4.24 & -21.09 & -7.59 \ 
2008 & -48.65 & -14.82 & 153.4 & -19.57 & -11.12 & 84.66 & 2.99 \ 
2009 & 38.36 & -18.14 & -34.69 & -18.32 & 51.68 & -64.19 & -22.09 \ 
2010 & 31.66 & 20.83 & -32.23 & 12.69 & -21.28 & 186.98 & 7.86 \ 
2011 & 10.09 & -3.4 & -26.37 & -13.14 & 0.1 & -64.27 & -1.15 \ 
2012 & -13.75 & 11.73 & 25.88 & 31.32 & 130 & 81.55 & 36.56 \ 
2013 & 1.81 & -8.99 & 105.56 & -16.27 & -27.86 & -39.41 & -7.47 \ 
2014 & -41.44 & -8.71 & -2.22 & -7.26 & -16.77 & -8.25 & 4.93 \ 
\hline
\end{tabular} 
\end{table} 



\end{document}

我的建议:

\documentclass{article}
\usepackage{geometry}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{caption}

\begin{document}

\renewcommand{\arraystretch}{1.2} % Default value: 1
\begin{table}[!htbp] \centering 
  \caption{Percent Change in Crop Acreage from Previous Year} 
  \label{tab:per_change} 
\begin{tabular}{@{}S[table-format=4.0] *{7}{S[table-format=-2.2]} @{}} 
\toprule
 {Year} & {Lettuce} & {Broccoli} & {Small Grains} & {Melons} & {Bermuda Grass} & {Sudan Grass} & {Alfalfa} \ 
\midrule
2000 & -7.71 & 43.25 & 76.26 & 12.09 & 50.96 & -66.45 & 1.32 \ 
2001 & -24.64 & -29.44 & -37.04 & 2.21 & -23.49 & 132.62 & 2.82 \ 
2002 & -9.7 & 29.79 & 62.95 & -5.05 & 3.91 & -11.52 & 4.38 \ 
2003 & -18.12 & -12.48 & 1.3 & -15.28 & 10.09 & 36.02 & -11.08 \ 
2004 & 12.1 & 4.08 & 36.01 & 18.2 & -26.47 & 14.91 & -8.67 \ 
2005 & -10.28 & -18.09 & -63.03 & -43.14 & -10.9 & -70.08 & -8.1 \ 
2006 & -9.35 & 53.81 & 9.34 & 26.2 & 5.55 & 163.44 & 21.39 \ 
2007 & 15.44 & -16.14 & -5.99 & -2.34 & 4.24 & -21.09 & -7.59 \ 
2008 & -48.65 & -14.82 & 153.4 & -19.57 & -11.12 & 84.66 & 2.99 \ 
2009 & 38.36 & -18.14 & -34.69 & -18.32 & 51.68 & -64.19 & -22.09 \ 
2010 & 31.66 & 20.83 & -32.23 & 12.69 & -21.28 & 186.98 & 7.86 \ 
2011 & 10.09 & -3.4 & -26.37 & -13.14 & 0.1 & -64.27 & -1.15 \ 
2012 & -13.75 & 11.73 & 25.88 & 31.32 & 130 & 81.55 & 36.56 \ 
2013 & 1.81 & -8.99 & 105.56 & -16.27 & -27.86 & -39.41 & -7.47 \ 
2014 & -41.44 & -8.71 & -2.22 & -7.26 & -16.77 & -8.25 & 4.93 \ 
\bottomrule
\end{tabular} 
\end{table} 

\end{document}

我为此 table 使用的设置是:

  • @{} 删除第一列之前的额外 space,没有它,行和文本将不会对齐

  • S[table-format=4.0] 小数点前有 4 位,小数点后有 0 位的数字列

  • *{7}{S[table-format=-2.2]} 七个数字列,可以有一个负号,小数分隔符前 2 位数字,小数点分隔符后 2 位数字(我知道你有时在分隔符前有 3 位数字,但是他们都没有减号,所以有足够的 space 保留)

  • @{} 在最后一列

    之后删除额外的 space