添加数组包会改变长表中的垂直空间

添加数组包会改变长表中的垂直空间

我需要将列表放在长表的其中一列中。我实际的长表更复杂,但我制作了一个 MWE 来展示。

我正在使用给出的解决方案如何删除长表列表后的多余换行符 它可以很好地消除表格内列表底部显示的多余空间。

但是一旦我包含了该array包,上述解决方案就不再起作用。

将显示 MWE,而无需array首先使用,这正是我想要的

\documentclass[11pt]{article}%ext    
\usepackage{amsmath,mathtools,amssymb}    
\usepackage{longtable} %2/24/16  does not work in two columns
%\usepackage{array}  %uncomment to see the problem

\usepackage{listings}
\lstset{
basicstyle=\small\ttfamily,
columns=flexible,
breaklines=true}

\begin{document}     
\begin{longtable}[c]{|p{3in}|}\hline 
 \vspace{-1.2\baselineskip}
 \begin{lstlisting}
this is code
which can be long
 \end{lstlisting}
 \vspace{-1.2\baselineskip} 
 \\\hline
%%%%%%%%%
\vspace{-1.2\baselineskip}
 \begin{lstlisting}
this is also code
 \end{lstlisting}
 \vspace{-1.2\baselineskip}
 \\\hline
\end{longtable}

\end{document}

现在lualatex foo.tex给出

Mathematica 图形

但是当添加array包(我需要在文档的其他地方添加)时,输出变成

Mathematica 图形

我看过以下答案为什么 longtable 不处理行内的 medskip 但 tabular 可以因为它看起来相关。它是关于array和之间的相互作用longtable。但经过多次反复尝试,我还是无法弄清楚如何在我的代码中使用上述任何解决方案来使其不产生额外的空间。

我希望能够array在上面的 MWE 中包含包,但不要再次弹出列表下方的额外空间。

使用 TL 2018

文件列表:

 *File List*
 article.cls    2014/09/29 v1.4h Standard LaTeX document class
  size11.clo    2014/09/29 v1.4h Standard LaTeX file (size option)
 amsmath.sty    2017/09/02 v2.17a AMS math features
 amstext.sty    2000/06/29 v2.01 AMS text
  amsgen.sty    1999/11/30 v2.0 generic functions
  amsbsy.sty    1999/11/29 v1.2d Bold Symbols
  amsopn.sty    2016/03/08 v2.02 operator names
mathtools.sty    2018/01/08 v1.21 mathematical typesetting tools
  keyval.sty    2014/10/28 v1.15 key=value parser (DPC)
    calc.sty    2017/05/25 v4.3 Infix arithmetic (KKT,FJ)
 mhsetup.sty    2017/03/31 v1.3 programming setup (MH)
 amssymb.sty    2013/01/14 v3.01 AMS font symbols
amsfonts.sty    2013/01/14 v3.01 Basic AMSFonts support
longtable.sty    2014/10/28 v4.11 Multi-page Table package (DPC)
   array.sty    2018/04/30 v2.4h Tabular extension package (FMi)
listings.sty    2018/09/02 1.7 (Carsten Heinz)
 lstmisc.sty    2018/09/02 1.7 (Carsten Heinz)
listings.cfg    2018/09/02 1.7 listings configuration
graphicx.sty    2017/06/01 v1.1a Enhanced LaTeX Graphics (DPC,SPQR)
graphics.sty    2017/06/25 v1.2c Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    2016/01/03 v1.10 sin cos tan (DPC)
graphics.cfg    2016/06/04 v1.11 sample graphics configuration
  luatex.def    2018/01/08 v1.0l Graphics/color driver for luatex
supp-pdf.mkii
epstopdf-base.sty    2016/05/15 v2.6 Base part for package epstopdf
infwarerr.sty    2016/05/16 v1.4 Providing info/warning/error messages (HO)
  grfext.sty    2016/05/16 v1.2 Manage graphics extensions (HO)
kvdefinekeys.sty    2016/05/16 v1.4 Define keys (HO)
 ltxcmds.sty    2016/05/16 v1.23 LaTeX kernel commands for general use (HO)
kvoptions.sty    2016/05/16 v3.12 Key value format for package options (HO)
kvsetkeys.sty    2016/05/16 v1.17 Key value parser (HO)
etexcmds.sty    2016/05/16 v1.6 Avoid name clashes with e-TeX commands (HO)
ifluatex.sty    2016/05/16 v1.4 Provides the ifluatex switch (HO)
pdftexcmds.sty    2018/09/10 v0.29 Utility functions of pdfTeX for LuaTeX (HO)
   ifpdf.sty    2018/09/07 v3.3 Provides the ifpdf switch
epstopdf-sys.cfg    2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live

更新

我尝试了下面评论中的建议,但无法让它工作。可能是我做得不对。以下是我尝试的

\documentclass[11pt]{article}%ext    
\usepackage{amsmath,mathtools,amssymb}    
\usepackage{longtable} %2/24/16  does not work in two columns
\usepackage{array}    
\usepackage{listings}
\lstset{
basicstyle=\small\ttfamily,
columns=flexible,
breaklines=true,
aboveskip= -0ex,
belowskip=-2ex
}

\listfiles
\begin{document} 

\begin{longtable}[c]{|p{3in}|}\hline 
\begin{minipage}{3in}
 \begin{lstlisting}
this is code
which can be long
and have more than one line
 \end{lstlisting}
\end{minipage}
 \\\hline
%%%%%%%%%
\begin{minipage}{3in}
 \begin{lstlisting}
this is also code
 \end{lstlisting}
\end{minipage}
 \\\hline
\end{longtable}

\end{document}

它给

Mathematica 图形

如果没有迷你页面,它就会出现这种情况

\documentclass[11pt]{article}%ext
\usepackage{amsmath,mathtools,amssymb}
\usepackage{longtable} %2/24/16  does not work in two columns
\usepackage{array}
\usepackage{listings}
\lstset{
basicstyle=\small\ttfamily,
columns=flexible,
breaklines=true,
aboveskip= -0ex,
belowskip=-2ex
}

\begin{document}     
\begin{longtable}[c]{|p{3in}|}\hline 
 \begin{lstlisting}
this is code
which can be long
and have more than one line
 \end{lstlisting}
 \\\hline
%%%%%%%%%
 \begin{lstlisting}
this is also code
 \end{lstlisting}
 \\\hline
\end{longtable}

\end{document}

Mathematica 图形

答案1

代码太长了,我没法将其放入评论中……

有趣的是,如果我复制您的测试并再次测试,我可以重现您的结果。但是,在我最初的测试中,我使用了以下代码:

\documentclass[11pt]{article}%ext
\usepackage{array, longtable}  % 2/24/16  does not work in two columns
\usepackage{listings}
\lstset{
basicstyle=\small\ttfamily,
columns=flexible,
breaklines=true,
keepspaces=true,
aboveskip= 0ex, 
belowskip=1ex  % <--- changed
        }

\begin{document}
\begin{longtable}{|p{3in}|}
\hline
\begin{minipage}{\linewidth}% <--- changed in comparison to your code
\begin{lstlisting}
this is code
which can be long
\end{lstlisting}
\end{minipage}
\\ \hline
\begin{minipage}{\linewidth}% <--- changed in comparison to your code
\begin{lstlisting}
this is also code
\end{lstlisting}
\end{minipage}
\\ \hline
\end{longtable}
\end{document}

我得到以下结果:

在此处输入图片描述

代码中现在的差异很小(由 %<--- 表示)。为什么我应该改变这个,我无法解释......也许是因为我同时升级了我的(两周前的)miktex安装。在测试之前我删除了.aux文件。

答案2

这是由于如何array在列的开始和结束处添加支柱p

我建议定义一个特定的环境,而不是手动堆积指令:

\documentclass[11pt]{article}
\usepackage{amsmath,mathtools,amssymb}    
\usepackage{longtable}
\usepackage{array}

\usepackage{listings}
\lstset{
  basicstyle=\small\ttfamily,
  columns=flexible,
  breaklines=true
}

\lstnewenvironment{tablelstlisting}
 {\minipage{\linewidth}}
 {\endminipage}

\begin{document}     
\begin{longtable}[c]{|p{3in}|}
\hline
\begin{tablelstlisting}
this is code
which can be long
\end{tablelstlisting}
\\
\hline
%%%%%%%%%
\begin{tablelstlisting}
this is also code
\end{tablelstlisting}
\\
\hline
\end{longtable}

\end{document}

在此处输入图片描述

如果您想要获得更窄的顶部和底部间距,可以采用不同的方法:

\documentclass[11pt]{article}
\usepackage{amsmath,mathtools,amssymb}    
\usepackage{longtable}
\usepackage{array}

\usepackage{listings}
\lstset{
  basicstyle=\small\ttfamily,
  columns=flexible,
  breaklines=true
}

\lstnewenvironment{tablelstlisting}
 {\mbox{}\vspace*{-1.2\baselineskip}}
 {\endgraf\mbox{}\vspace{-1.2\baselineskip}}

\begin{document}     
\begin{longtable}[c]{|p{3in}|}
\hline
\begin{tablelstlisting}
this is code
which can be long
\end{tablelstlisting}
\\
\hline
%%%%%%%%%
\begin{tablelstlisting}
this is also code
\end{tablelstlisting}
\\
\hline
\end{longtable}

\end{document}

在此处输入图片描述

相关内容