2

There are horizontal gaps in my listings code block when I change the background color. This minimal working example code and screenshot demonstrate the issue.

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{
    basicstyle=\linespread{0.67}\ttfamily,
    backgroundcolor=\color{gray}
}
\begin{document}
\begin{lstlisting}
n := 42;
for i in [1,2,3,4,5] do
    n := n+i;
od;
\end{lstlisting}
\end{document}

screenshot of issue

I know a linespread of ⅔ is ridiculous, I'm just accentuating the problem with this example. The real issue is that these gaps appear, albeit very faintly, even for a linespread of 1!

linespread of 1 faint lines screenshot

What's going on here and how can I fix this?

  • yes it can happen if the font is a bit too large. Use tcolorbox if you want listings with background, its code for this is better. – Ulrike Fischer May 24 '21 at 18:17
  • @UlrikeFischer this seems like such a drive a nail with a sledgehammer solution – Mike Pierce May 24 '21 at 19:37
  • 2
    well listings colors lines and this can always give this gaps. If you want to avoid it, you need another painting system, and listings doesn't have it. – Ulrike Fischer May 24 '21 at 19:57

3 Answers3

2

You can wrap lstlistings in a tcolorbox.

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage[many]{tcolorbox}

\tcolorboxenvironment{lstlisting}{ spartan, frame empty, boxsep=0mm, left=1mm,right=1mm,top=-1mm,bottom=-1mm, colback=gray!45, }

\lstset{ basicstyle=\linespread{0.67}\ttfamily, }

\begin{document}

\begin{lstlisting} n := 42; for i in [1,2,3,4,5] do n := n+i; od; \end{lstlisting}

\end{document}

enter image description here

egreg
  • 1,121,712
0

This isn't my favorite solution since it involves including another package and wrapping each lstlisting environment in another environment, but Ulrike Fischer's suggestion in the comments of using tcolorbox works, and it add pleasing margins to the code block.

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{
    basicstyle=\linespread{0.67}\ttfamily,
    %backgroundcolor=\color{gray},
}
\usepackage{tcolorbox}
\newtcolorbox{tcolorbox-code}{%
    colback=gray, %
    colframe=gray, %
    sharp corners %
}

\begin{document} \begin{tcolorbox-code} \begin{lstlisting} n := 42; for i in [1,2,3,4,5] do n := n+i; od; \end{lstlisting} \end{tcolorbox-code} \end{document}

screenshot of what the code outputs

  • well tcolorbox has dedicated environments for code that you can use. – Ulrike Fischer May 24 '21 at 20:29
  • @UlrikeFischer I saw that briefly, but it looked like it was specific to TeX code, and then it complained about a comment line starting with a # character, then it wasn't syntax-highlighting my code right, and then it turned into more of a hassle to learn to use the sledgehammer properly to drive the nail than to just clumsily drop the sledgehammer around the nail until the job was done – Mike Pierce May 24 '21 at 20:31
  • I echo the use of tcolorbox for code listings. Here's how I did it: https://tex.stackexchange.com/a/583367/218142. – LaTeXereXeTaL May 24 '21 at 20:47
0

I found this workaround. We can add frames to the lines, and set the frame's border to 0 width. It removed the vertical gaps between lines for me.

\usepackage{listings}
\lstset{
  basicstyle=\small\ttfamily,
  columns=flexible,
  breaklines=true,
  backgroundcolor=\color{lightgray},
  frame=ltb,
  framerule=0pt,
}
Jay Wang
  • 252