Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This question already has an answer here:

Whenever I have a page break splitting my lstlisting on different pages, I would like to have a symbol or generally spoken some command inserted automatically. I can accomplish that manually by using the lstlisting's escape character:

\begin{lstlisting}[language=cs] 
class MyClass
{
  // assume this is on one page

  ´
  \begin{flushright}
  $\blacktriangledown$
  \end{flushright}
  ´

  // assume this is on the other page
}
\end{lstlisting}

However, when I add enough text before the listing, the symbol won't be placed at the right spot anymore. It will get moved to the next page where it doesn't make sense anymore. To tackle that I need some dynamic placement of some user-defined latex to be inserted right at the spot where a page break splits my lstlisting apart.

Update: There is a formfeed Parameter that can be set within lstset. It defaults to \bigbreak and even though inserting \bigbreak in escaped mode within the listing results in the same visual appearance of a split of the listing like it happens at page breaks, changing formfeed doesn't make a change in the output, really unfortunately.

Is there a solution to it?

share|improve this question

marked as duplicate by cgnieder, Werner, mafp, barbara beeton, egreg Jun 5 '13 at 20:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Can you transform your code snippet into a minimal working example (MWE)? –  hpesoj626 Feb 28 '13 at 22:11
    
Possible duplicate: "How to show a hint when lstlisting is breaking page?" –  Heiko Oberdiek Jun 5 '13 at 17:38
add comment

1 Answer

To signify a code listing continued from the prior page, I get my MWE to stick a \nabla (inverted triangle) at the right top of a page where the lstlisting breaks across a page boundary. I had to use a David Carlisle trick to make afterpage and listings play nice together (empty page afterpage listings). I also temporarily modify the definition of \thepage to put the same symbol at the end of the page where the listing breaks.

However, in both cases, the user must choose to insert them, only if he notices the code breaking across a page. So, in that sense, they are not automated. However, it is an improvement over what the questioner started with, in that he had to literally place his symbol at the exact line of the page break to get the desired look. At least with this solution, adding one line of text will not, generally, screw up the result, as it would with his original approach.

\documentclass{article}
\usepackage{afterpage}
\usepackage{listings}
\let\svthepage\thepage
\begin{document}

Normal text

% http://tex.stackexchange.com/questions/78794/empty-page-afterpage-listings
\def\thepage{\hfill\svthepage\hfill$\nabla$}
\setbox0\vbox{
\afterpage{\hfill$\nabla$}
\begin{lstlisting}%[language=cs] 
class MyClass
{
  // assume this is on one page


  // assume this is on one page



  // assume this is on one page



  // assume this is on one page



  // assume this is on one page



  // assume this is on one page



  // assume this is on one page



  // assume this is on one page



  // assume this is on one page



  // assume this is on one page



  // assume this is on one page



  // assume this is on the other page


  // assume this is on the other page


  // assume this is on the other page
}
\end{lstlisting}
}
\unvbox0

\let\thepage\svthepage

Back to normal text

\end{document}

enter image description here

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.