5

I need to put listings inside one of the columns in a long table. The actual longtable I have is more complicated, but I made a MWE below to show.

I am using the solution given in how-to-remove-extra-line-break-inside-longtable-after-listing which works well in order to remove extra space that shows at bottom of listing inside table.

But once I include the array package then the above solution now no long works.

Will show a MWE, without using array first, which is what I want

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

now lualatex foo.tex gives

Mathematica graphics

But when adding array package (which I need somewhere else in the document), the output becomes

Mathematica graphics

I've looked at answers to why-does-longtable-not-process-medskip-inside-a-row-but-tabular-does since it seems related. it is about interaction between array and longtable. But I am not able after much trial and error figure how to use any of the solutions above in my code to make it not generate the extra space.

What I would like is to be able to include array package in the above MWE, but not have the extra space below the list pop up again.

Using TL 2018

File listings:

 *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

Update

I tried the suggestion in the comment below, but I can't get it to work. May be I am not doing it correctly. Here is what I tried

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

It gives

Mathematica graphics

Without the mini-page being there, it gives this

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

  • 1
    meanwhile (from time when i wrote linked answer) the package array is changed (improved vertical spaces). consequently this time proposed solution doesn't work anymore. a possible solution: (i) to \lstset add aboveskip= 0ex, belowskip=-2ex and (ii) lstlisting enclose in minipage. with this the result be independent from presence of the array package. – Zarko Sep 20 '18 at 2:53
  • @Zarko thanks. I am not able to get it to work. Once I put the listing inside a minipage, it does not expand vertically to fill the space as needed. I can't post the code here, as too large. May be I did not do it as it should be, so will add it to my question as additional part. – Nasser Sep 20 '18 at 3:43
4

code is to long that i can put in comment ...

interestingly, if i copy your test and test it again, i can reproduce your result. however, my original test, where i use the following code:

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

i obtain the following result:

enter image description here

in code is now small difference (indicated by % <---). why i should change this, i can't explain ... maybe because i meanwhile upgrade my (two week old) miktex installation. before my test i delete .aux file.

2

This is due to how array adds the struts at the beginning and end of a p column.

Rather than manually piling up instructions, I suggest to define a specific environment:

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

enter image description here

A different approach, if you want to get narrower top and bottom spacing:

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

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.