4

I have created a macro \piece to structure longer sections into smaller, numbered pieces. (I am using the macro in a set of lecture notes where I try to introduce one concept per piece.) The code, which I have basically stolen from \@startsection, has been working fine so far, but now I am experiencing problems with it in connection with the listings package. When I start a \piece immediately after an lstlisting, there is too much vertical space between the listing and the new piece.

Similar issues with vertical space after an lstlisting have been discussed before, but I was not able to derive the answer to my question from the answers:

How can I make the code for \piece more robust so that this problem disappears?


Here is a minimal working example that illustrates the problem:

\documentclass{minimal}

\usepackage{listings}

\makeatletter
\newcounter{piece}
\def\thepiece{\arabic{piece}}
\newcommand*{\piece}{%
  \if@noskipsec \leavevmode \fi
  \par
  \@afterindentfalse
  \if@nobreak
    \everypar{}%
  \else
    \addvspace\belowdisplayskip
  \fi
  \refstepcounter{piece}%
  \@afterheading
  \makebox[0pt][r]{\thepiece\quad}%
}
\makeatother

\begin{document}

\piece Piece 1

\piece Piece 2

\begin{lstlisting}
Listing
\end{lstlisting}

\piece Piece 3

\end{document}

Output of the minimal example:

Output of the MWE

  • 1
    Are you sure that somethinglike \section or \subsection isn't better suited for this 'splitting'? And please have a look on Why should the minimal class be avoided – user31729 Jan 16 '16 at 10:01
  • You are right that \piece and the sectioning commands are similar; that's why I stole the code for \piece from there :). A crucial difference is that there is no title. But perhaps there already is a similar command in some package? – Marco Kuhlmann Jan 16 '16 at 10:29
  • Ok, I'll delete my answer -- it's of no use to you – user31729 Jan 16 '16 at 11:00
  • But you spotted the \leavevmode typo; that was helpful! – Marco Kuhlmann Jan 16 '16 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.