Search code examples
latextabularstargazeroverleaf

Why are the vertical lines in my table different lengths?


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


Solution

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

    enter image description here

    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}
    

    enter image description here

    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