10

I have a listings environment in my document with backgroundcolor=\color{grey} to get a nice grey background and mathescape=true for using math symbols. However when using the \vdots command inside a code listing I get a break in my listing, as follows:

\documentclass{book}
\usepackage{color}
\definecolor{grey}{rgb}{0.9,0.9,0.9}
\usepackage{listings}
\lstset{%
language=Lisp,
basicstyle=\small,
backgroundcolor=\color{grey},
mathescape=true}

\begin{document}
\begin{lstlisting}
(let ((<$var_1$> <$exp_1$>)
      (<$var_2$> <$exp_2$>)
      $\vdots$
      (<$var_n$> <$exp_n$>))
   <$body$>))
\end{lstlisting}
\end{document}

Resulting output:

enter image description here

So far it's only \vdots which is doing this (and it happens multiple times so I know it's not a fluke). Is there a fix for this, or maybe would minted work better at this sort of thing? (Although I'd rather not switch if I don't have to.)

  • Nice question! You could improve the question by providing compilable code with \documentclass and everything. – Hendrik Vogt Feb 13 '11 at 6:59
  • @Hendrik - Thanks! Ask and ye shall receive. – Andrew Arnold Feb 13 '11 at 7:09
  • 1
    And suddenly I don't get Undefined color `grey' anymore :-) – Hendrik Vogt Feb 13 '11 at 7:34
5

The problem seems to be that listings expects a constant line height, and that \vdots just takes up too much vertical space. One fix is to redefine \vdots so that it takes up less vertical space:

\renewcommand\vdots{%
  \vbox{\baselineskip3pt\lineskiplimit0pt\kern1pt\hbox{.}\hbox{.}\hbox{.}\kern-1pt}}

3
\documentclass{book}
\usepackage{color}
\definecolor{grey}{gray}{0.9}
\usepackage{listings}

\lstset{%
language=Lisp,
basicstyle=\small,
backgroundcolor=\color{grey},
mathescape=true}

\begin{document}
\begin{lstlisting}
(let ((<$var_1$> <$exp_1$>)
      (<$var_2$> <$exp_2$>)
      $\makebox(0,7.5){\vdots}$
      (<$var_n$> <$exp_n$>))
   <$body$>))
\end{lstlisting}
\end{document}
  • 1
    This solution eats a bit of the lowest of the three dots. My opinion: Here, a bit of explanation what \makebox does would be much more valuable than the full code. – Hendrik Vogt Feb 13 '11 at 9:35
  • This doesn't quite work in my document since it shoves the dots to the far left of the page, eating up any indentation. On the other hand, I learned something new about \definecolor! – Andrew Arnold Feb 13 '11 at 13:47
  • Replacing $\makebox(0,7.5){\vdots}$ with $\makebox(30,8){\vdots}$ (and perhaps an empty line below and above it so everything doesn't look so crammed) makes the lowest dot appear fully and fixes the indentation issue. Note: Adding the empty lines around it also makes the problem that the \vdots aren't vertically centered between the code above and below less apparent. – Casimir Jan 22 '16 at 8:42
0

The listings package documentation says that you have to use some box tools for this (tcolotbox, mdframed, etc.)

\documentclass{book}
\usepackage{color}
\definecolor{grey}{rgb}{0.9,0.9,0.9}
\usepackage{listings}
\usepackage{mdframed}
\usepackage{moreverb}
\begin{verbatimwrite}{mycode.lisp}
(let ((<$var_1$> <$exp_1$>)
      (<$var_2$> <$exp_2$>)
      $\vdots$
      (<$var_n$> <$exp_n$>))
   <$body$>))
\end{verbatimwrite}
\lstset{%
language=Lisp,
basicstyle=\small,
mathescape=true}

\begin{document}
\begin{mdframed}[
    backgroundcolor=grey,
    hidealllines=true,
    innertopmargin=0pt,
    innerbottommargin=0pt,
    innerleftmargin=0pt,
    innerrightmargin=0pt
]
\lstinputlisting{mycode.lisp}
\end{mdframed}
\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.