2

Consider the following code which does an alignment experiment with listings. We are interested in the behaviour aboveskip and belowskip with and without a frame.

% ---------------------------------------------------------------------------- %
% File
\begin{filecontents*}{\jobname.cpp}
int main(int argc, char* argv[])
{
    return 0;
}
\end{filecontents*}
% ---------------------------------------------------------------------------- %
% Preamble
\documentclass[letterpaper, 12pt, onecolumn]{article}
% ---------------------------------------------------------------------------- %
% Packages
\usepackage{listings}
\usepackage{xcolor}
% ---------------------------------------------------------------------------- %
% Listings options
\lstset{
    language = C++,
    framesep = 0pt,
    rulesep = 0pt,
    aboveskip = 0pt,
    belowskip = 0pt,
    backgroundcolor = \color[RGB]{200, 200, 200},
}
% ---------------------------------------------------------------------------- %
% Commands
\newcommand{\alphabet}{ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz}
\newcommand{\makelisting}[1]{
  \newpage\noindent\alphabet
  \lstinputlisting[frame = lines, basicstyle = \fontfamily{cmtt}\selectfont#1]{\jobname.cpp}
  \alphabet
  \lstinputlisting[basicstyle = \fontfamily{cmtt}\selectfont#1]{\jobname.cpp}
  \alphabet
}
% ---------------------------------------------------------------------------- %
% Document
\begin{document}
\makelisting{\tiny}
\makelisting{\scriptsize}
\makelisting{\footnotesize}
\makelisting{\small}
\makelisting{\normalsize}
\makelisting{\large}
\makelisting{\Large}
\makelisting{\LARGE}
\makelisting{\huge}
\makelisting{\Huge}
\end{document}
% ---------------------------------------------------------------------------- %

Some of the results it produces:


Tiny size tiny


Huge size enter image description here


With a frame, the top of the listing is always anchored at the same top position with the text that is above (same thing with bottom text). However, when no frame is set, the distance varies with the size of the text in the listing, which is annoying.

How to anchor the listing always in the same way regarding to the text above and below when no frame is set, regarless of the font size in the listing?

3

The place the listings package defines the frame typesetting of horizontal rules is in the definition of the \lst@frameH macro. I tried to understand that code but it's a wild mix of nested boxing and dimension adjusting. My best guess is that with the rules being added there's a fixed amount of \vskip between the listings and the surrounding text, wheras without any rules TeX's standard interline spacing rules are active which add space depending on the currently active font size/baseline distance.

A possible fix is to always add the frame but additionally set framerule = 0pt, which effectively adds no visible frame but keeps the spacing consistent:

\newcommand{\makelisting}[1]{
  \newpage\noindent\alphabet
  \lstinputlisting[frame = lines, basicstyle = \fontfamily{cmtt}\selectfont#1]{\jobname.cpp}
  \alphabet
  \lstinputlisting[frame = lines, framerule = 0pt, basicstyle = \fontfamily{cmtt}\selectfont#1]{\jobname.cpp}
  \alphabet
}

Tiny output: enter image description here

Huge output: 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.