The package fancyvrb provides \begin{SaveVerbatim} and \UseVerbatim, which does almost exactly what I need. I want to use this with a custom lstlisting environment. The environment looks exactly how I want it, with the backgroundcolor and everything (it's quite involved).

So what I need is some sort of \begin{SaveLstlisting} and \UseLstlisting, so I can store the contents of a lstlisting environment in a macro at one point and use it at a later point. How can I achieve this?

  • Maybe something like tex.stackexchange.com/a/49418? – Marijn Aug 3 '18 at 14:49
  • Apparently it suppresses page breaks. Any idea how to allow page breaks in listings using this method? – Volker Aug 29 '18 at 5:55
  • What is your use case exactly? Maybe there are easier ways to do what you want. If you don't want to repeat long code fragments then you could save the code to a file and use \lstinputlisting. If you want to store the settings of the colors, fonts etc. then you can use \lstdefinestyle and use the defined style for multiple code fragments. – Marijn Aug 29 '18 at 7:57
  • I want to use text snippets, which display a code listing (or several listings) that is always different. It should work something like this: \usesnippet{snippetA}{listingOne}{listingTwo} where listingOne and listingTwo refer to a different listing each. I guess it could work with an extra file, but it would be nice if I could do it without that. Maybe the filecontents will do what I need? I just had that idea. – Volker Aug 29 '18 at 8:27

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.