8

I want to use lstinputlisting to include a file with code within an lstlisting block, which is defined through a new environment. However, the lstinputlisting command itself, not the code, is reproduced verbatim in the code block. MWE:

\documentclass{minimal}
\usepackage{color}
\usepackage{listings}
\usepackage{verbatim}

\definecolor{customblue}{RGB}{235,241,245}

\lstnewenvironment{terminalblock}{%
  \lstset{backgroundcolor=\color{customblue},
  frame=single,
  framerule=0pt,
  basicstyle=\ttfamily,
  breaklines=true,
  columns=fullflexible}}{}

\begin{document}
 \begin{terminalblock}
  \lstinputlisting{code.cpp}
 \end{terminalblock}

\begin{verbatim}
 \verbatiminput{code.cpp}
\end{verbatim}

\begin{lstlisting}
 \lstinputlisting{code.cpp}
\end{lstlisting}

\end{document}

The same problem occurs in the standard lstlisting and verbatim environments as well. The PDF document just contains \lstinputlisting{code.cpp} three times, as shown.

mwe.png

Since the background of the block generated by the custom environment is the correct colour, so I think the configuration settings are working in the custom environment, but not lstinputlistings. I don't know if it's relevant, but these are the contents of code.cpp:

#include <iostream>
#define abusing using

abusing namespace std;

int main()
{
   cout<<"Hello, world!"<<endl;
   return 0;
}
10

The terminalblock environmnet is for code directly input; it doesn't make much sense to use \lstinputlistings inside this environment since it will be taken verbatim (although it could be done, for example, by escaping to LaTeX). A better approach is to define a style and use this style both for your environment (to be used to input direct code) and for \lstinputlistings (to be used to include code from external files); also notice that you are using frame=single, but setting the rule width to 0pt so no lines will be drawn.

Here's your code with the defined style and \lstinputlisting:

\documentclass{article}
\usepackage{color}
\usepackage{listings}

\definecolor{customblue}{RGB}{235,241,245}

\lstdefinestyle{mystyle}{%
  backgroundcolor=\color{customblue},
  frame=single,
  framerule=0pt,
  basicstyle=\ttfamily,
  breaklines=true,
  columns=fullflexible
}

\lstnewenvironment{terminalblock}{%
  \lstset{style=mystyle}}{}

\begin{document}
\lstinputlisting[style=mystyle]{code.cpp}
\end{document}

and with the terminalblock environment:

\documentclass{minimal}
\usepackage{color}
\usepackage{listings}

\definecolor{customblue}{RGB}{235,241,245}

\lstdefinestyle{mystyle}{%
  backgroundcolor=\color{customblue},
  frame=single,
  framerule=0pt,
  basicstyle=\ttfamily,
  breaklines=true,
  columns=fullflexible
}

\lstnewenvironment{terminalblock}{%
  \lstset{style=mystyle}}{}

\begin{document}

\begin{terminalblock}
#include <iostream>
#define abusing using

abusing namespace std;

int main()
{
   cout<<"Hello, world!"<<endl;
   return 0;
}
\end{terminalblock}

\end{document}

enter image description here

  • That's exactly what I was looking for. Thank you for the help. I always learn a great deal every time I ask a question here. – Ricardo Altamirano May 28 '12 at 2:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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