为什么我的表格中的垂直线长度不同? [英] Why are the vertical lines in my table different lengths?

查看:33
本文介绍了为什么我的表格中的垂直线长度不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

\renewcommand{\arraystretch}{1.5} % 默认值:1\begin{table}[!htbp] \centering\caption{与上一年相比作物种植面积的百分比变化}\label{tab:per_change}\begin{tabular}{@{\extracolsep{5pt}} |c|c|c|c|c|c|c|c|c|}\\[-1.8ex]\hline\hline \\[-1.8ex]年&生菜&西兰花&小杂粮甜瓜&百慕大草&苏丹草苜蓿 \\\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{表格}\茶几}

我的建议:

\documentclass{article}\usepackage{几何}\usepackage{booktabs}\usepackage{siunitx}\usepackage{标题}\begin{文档}\renewcommand{\arraystretch}{1.2} % 默认值:1\begin{table}[!htbp] \centering\caption{与上一年相比作物种植面积的百分比变化}\label{tab:per_change}\begin{tabular}{@{}S[table-format=4.0] *{7}{S[table-format=-2.2]} @{}}\toprule{年} &{生菜} &{西兰花} &{小粒} &{甜瓜} &{百慕大草} &{苏丹草} &{苜蓿} \\\中间规则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 \\\底部规则\end{表格}\茶几}\end{文档}

我用于此表的设置是:

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

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

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

  • @{} 删除最后一列后的额外空格

I'm working in overleaf, and the vertical lines I've inserted into my table (using tabular) are different lengths. I've attached code and a pic of the output.

Specifically, I'm wondering why the two lines on either side of the "year" column are longer than all the others and why they extend beyond the top and bottom horizontal lines. I'm also wondering why the vertical lines on either side of the other columns are not continuous (see the gaps under the horizontal line under each crop name and below the double horizontal line at the top of the table). If it's important, this table was generated using stargazer in RStudio. I manually added the vertical lines. Thanks!

\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

解决方案

  • the stray lines below and above the table are caused by all these \\[-1.8ex] used even though there is no line to finish

  • the stray lines on the right comes from a mismatch of how many columns there are actually in the table (8) and how many columns you tell latex there would be in the table (9).

  • please reconsider such a table layout. Using vertical lines does not exactly look like a professional done table, see the booktabs package documentation for further inspiration http://mirrors.ctan.org/macros/latex/contrib/booktabs/booktabs.pdf

  • please also have a look at the siunitx package to correctly align and format your numbers. At the very minimum, you shouldn't abuse hyphens as minus signs.

  • If your table is already too wide to fit in the available text area, the last thing you should do is adding extra wide space between the columns


Your fixed 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}

My suggestion:

\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}

The settings I used for this table are:

  • @{} remove additional space before the first column, without it the lines and text won't line up

  • S[table-format=4.0] a numeric column with 4 digits before the decimal separator and 0 digits after it

  • *{7}{S[table-format=-2.2]} seven numeric columns which can have a minus sign, 2 digits before the decimal separator and 2 digits after it (I know that you sometimes have 3 digits before the separator, but they all don't have a minus sign, so there is enough space reserved)

  • @{} remove additional space after the last column

这篇关于为什么我的表格中的垂直线长度不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆