[Tex/LaTex] How to customize the caption format for \lstinputlisting

captionslistings

I know how to customize the caption format of a lstlisting environment.

But how to do this if I used the \lstinputlisting command to import code?

My document contains the snippet below.

\begin{framed}
\lstinputlisting[label=samplecode,caption=sample code,language=python]{sample.py}
\end{framed}

Best Answer

You can use the package caption for customization your output.

The modification can be done for lstlisting with the command captionsetup.

\captionsetup[lstlisting]{font={small,tt}}

Here the font is set to small and typewriter family is used.

Please have a closer look at the manual. Here a complete example:

\documentclass{report}
\usepackage{filecontents}
\begin{filecontents*}{sample.py}
int main(int argc, char ** argv)
{
    printf("Hello!\n");
    return 0;
}
\end{filecontents*}
\usepackage{listings}
\usepackage{framed}

\usepackage{caption}
\captionsetup[lstlisting]{font={small,tt}}
\begin{document}

\begin{framed}
\lstinputlisting[label=samplecode,caption=sample code,language=python]{sample.py}
\end{framed}
\end{document}

enter image description here

Related Question