4

I am using listings with mathescape=true to include some typeset output. However, I would like a little white space around that output. Currently I have

\lstset{language=,
  keepspaces=true,
  basicstyle=\ttfamily,
  columns=flexible,
  mathescape=true,
  }  

However, the result is that a typeset equation is put on the line immediately following the preceding code, with no vertical white space.

For example:

\begin{lstlisting}
code: Gimme the square root of two:
$\sqrt{2}$
code: Gimme a rational approximation to pi:
$\displaystyle{\frac{355}{133}}$
\end{lstlisting}

comes out as

enter image description here

I know I could put the equations outside the listing, but it works better for me to have equations escaped within a listing.

How can I force escaped equations to be indented, and have a little white space above and below?

3

You can insert tokens with escapebegin and escapeend. If you don't use mathescape but escapechar you can e.g. switch to displaystyle:

\documentclass{article}


\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{listings}
% #################################################################### COLORS #
\lstset{language=,
  keepspaces=true,
  basicstyle=\ttfamily,
  columns=flexible,
  %mathescape=true,
  escapechar=$,
  escapebegin=\[,
  escapeend=\]
  }  

\begin{document}
\begin{lstlisting}
code: Gimme the square root of two:
$\sqrt{2}$
code: Gimme a rational approximation to pi:
$\displaystyle{\frac{355}{133}}$
\end{lstlisting}

\end{document}

enter image description here

You can also insert some strut:

\lstset{language=,
  keepspaces=true,
  basicstyle=\ttfamily,
  columns=flexible,
  mathescape=true,
  escapebegin=\quad\mbox{\rule[-1em]{0pt}{2.5em}},
  }  
  • This makes sense to me - I was confused about the usage of escapechar, escapebegin and escapend, but this example has sorted it out. – Alasdair Jan 30 '16 at 0:09
2

Interesting question!

\documentclass{article}
\usepackage{listings}

\lstset{language=,
  keepspaces=true,
  basicstyle=\ttfamily,
  columns=fullflexible,
  mathescape=true,
}

\newcommand{\PRESPACE}{\vadjust pre{\vskip\abovedisplayskip}}
\newcommand{\POSTSPACE}{\vadjust{\vskip\belowdisplayskip}}

\newcommand{\DS}{%
  \aftergroup\aftergroup\aftergroup\PRESPACE
  \aftergroup\aftergroup\aftergroup\POSTSPACE
  \displaystyle
}

\begin{document}

\begin{lstlisting}
code: Gimme the square root of two:
$\DS\sqrt{2}$
code: Gimme a rational approximation to pi:
$\DS\frac{355}{133}$
code: OK
\end{lstlisting}

\end{document}

We have to jump off two groups in order \vadjust can act.

enter image description here

  • This looks like a solution which could be generalized; however I've never used \aftergroup before! (I had to look it up to see what it meant.) – Alasdair Jan 30 '16 at 0:08
  • @Alasdair We have to escape the group due to the math formula and also the box made by listings around the line. – egreg Jan 30 '16 at 0:11

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.