Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is there any way I can get rid of the empty new row produced by lstlisting inside a tabular?

enter image description here

Here's a minimum working example:

\documentclass[11pt]{article}

\usepackage{listings}
\lstset{
language=python,
showspaces=false,
showtabs=false,
tabsize=4, 
frame=trbl,
frameround=tttt,
basicstyle=\ttfamily,
showstringspaces=false,
numbers=left,
breaklines=true,
}

\begin{document}

\begin{tabular}{l | p{0.8\textwidth}}
test &
\begin{lstlisting}
somecode
\end{lstlisting}
\end{tabular}

rest of text


\end{document}

I'm using TexShop with xelatex.

Follow-up question: It also seems to break when I'm using longtable: The tables don't continue into other pages, instead it creates huge gaps and I'm suspecting it has to do with lstlisting.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Try adding the option

belowskip=-\baselineskip

So

\begin{tabular}{l | p{0.8\textwidth}}
test &
\begin{lstlisting}[belowskip=-\baselineskip]
somecode
\end{lstlisting}
\end{tabular}

There is also aboveskip

share|improve this answer
    
I get missing number, treated as zero. –  pg-robban Oct 14 '11 at 17:22
    
The option must be added when calling the environment. It's an option for \lstset, but I don't think it's good to put it in the general options. I put it in the wrong place, sorry. –  egreg Oct 14 '11 at 17:24
    
It seems to work for tables, but seems to break if I have text immediately after when it's not inside a table: i.imgur.com/C98iu.png –  pg-robban Oct 14 '11 at 17:26
2  
@pg-robban That's why it's better to set belowskip to zero only when really needed: don't put in \lstset, but only as optional argument in tables. –  egreg Oct 14 '11 at 17:29
    
Nevermind, just saw your update. It works now. Thanks :) –  pg-robban Oct 14 '11 at 17:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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