I would like to add listings source as attachment to a PDF (created by pdflatex
). For \lstinputlisting
this is pretty easy:
\RequirePackage{listings}
\RequirePackage{embedfile}
\let\lstinputlistingO=\lstinputlisting
\renewcommand{\lstinputlisting}[2][]{\embedfile{\detokenize{#2}}\lstinputlistingO[#1]{#2}}
but how can I do the same with \begin{listing} ... \end{listing}
? (referenced filename should be the something like listing 12
)
Best Answer
As
embedfile
can only embed external files, you have to write the content of the environment to a file first. To do this, the capabilities of thelistings
package, namely the\lst@BeginAlsoWriteFile
macro, can be used:This code snippet creates a new environment called
lstembedlisting
, which can be used like the normallstlisting
, except that it expects an additional filename as a mandatory argument. The code inside the environment is typeset and written to that file at the same time. Afterwards,\embedfile
is called to attach it to the PDF.Full MWE: