For my class, I write exercises; I also include the solutions (for me and the TAs). I like to process a single document, but set a "flag" to say whether to produce solutions or not. This answer helped me make that work. But when I include a "lstlisting" within the optional material, things no longer work properly:
\documentclass{article}
\RequirePackage{listings}
\RequirePackage{etoolbox}
\newcommand{\solflag}{}
\newcommand{\version}[2]
{\ifcsdef{#1flag}{#2}{}}
\begin{document}
Before.
\version{sol}{
\begin{lstlisting}[language=C]
// An example of a C comment.
\end{lstlisting}
}
After.
\end{document}
Processing this with pdflatex on my Linux machine produces a warning (Package Listings Warning: Text dropped after begin of listing on input line 18.) followed by an "asterisk-waiting-for-input" state.
From this answer, I see that stuff after \begin{lstlisting} (on the same line) gets swallowed up and ignored and provokes that warning. Does the processing of arguments to macros swallow up all the end-of-line markers or something? And if so, is there something I can do to make this work?
(One answer: I can use \lstincludelisting, which seems to work fine...but it's nice to see small (10 line?) listings right in place rather than by reference. And switching over involves editing dozens of documents, too.)


verbatim-like content as part of a macro argument, and this is not allowed. Consider using thecommentpackage which does what you'll after - allowing versions to comment in/out certain environments. – Werner Sep 29 '16 at 20:24listingscontent "verbatim-like", and how could I have known this in advance? – John Sep 29 '16 at 20:26