I have a document with many large listings. To keep things organised I'd prefer to keep these listings in separate files and using \input in the appropriate location in the text.

However, I'd also like to be able to modify the label and caption from the enclosing document file. Currently listings are of the form:

\begin{lstlisting}[language=C, caption={main function}, label={lst:main}]
int main()

Is it possible to define a macro like \includelisting{listing.tex}{my-caption}{my-label} which would import the listing contents and also set the caption and label?

One solution is to input the listing inside a figure environment and use figure captions/labels instead. However, I'd prefer keep my listings and figures separate.

Alternatively, I could keep only the code inside a separate file, however it gets tiresome switching between files when I need to define extra keywords.

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.