beamer icon indicating copy to clipboard operation
beamer copied to clipboard

Extra vertical space before lstlisting code with latest listings package

Open zohooo opened this issue 6 years ago • 5 comments

After updating listings package from version 1.6 to 1.7, there was extra vertical space before lstlisting code.

\documentclass{beamer}
\usepackage{listings}
\begin{document}
\begin{frame}[fragile=singleslide]
\frametitle{Some Title}
\begin{lstlisting}
Some Code
Some Code
Some Code
Some Code
Some Code
Some Code
Some Code
Some Code
Some Code
Some Code
Some Code
Some Code
Some Code
Some Code
Some Code
Some Code
\end{lstlisting}
\end{frame}
\end{document}

Please compare the following two results. It is OK with article class. A bug of beamer or listings?

listings0

listings1

zohooo avatar Nov 07 '18 02:11 zohooo

@zohooo The recent change on auto-breaking of frames is in the commit fd369b7. Can you help test it?

louisstuart96 avatar Nov 07 '18 15:11 louisstuart96

I have tried latest code in beamer repository, but didn't fixed this problem.

zohooo avatar Nov 08 '18 00:11 zohooo

I comared listings version 1.6 and 1.7, and found that a \leavevmode was added to the beginning of lstlisting environment in version 1.7, causing the problem in beamer frames.

\lstnewenvironment{lstlisting}[2][]
    {%
     \leavevmode%
     \lst@TestEOLChar{#2}%
     \lstset{#1}%
     \csname\@lst @SetFirstNumber\endcsname}
    {\csname\@lst @SaveFirstNumber\endcsname}

zohooo avatar Nov 08 '18 01:11 zohooo

I can confirm this.

The \leavevmode in listings v1.7 was apparently added to fix the problem discussed in https://tex.stackexchange.com/questions/136256/strange-interaction-between-listings-and-sections (curiously enough, \lstinputlisting was not changed, so the problem still exists there).

Unfortunately, \leavevmode in the opening of lstlisting does add extra vertical space in beamer slides, just as @zohooo observed, breaking a lot of my beamer presentations.

As a workaround, I can simply use a patched local copy of listings.sty with \leavevmode removed. However, it would be nice if beamer could somehow restore the previous behaviour.

tertium avatar Dec 03 '18 23:12 tertium

@tertium You could also use \lstnewenvironment to define your own listing environments. These environments work as before, without extra vertical space.

zohooo avatar Dec 04 '18 03:12 zohooo