5

I am trying to put in some lines of code using the lstlistings-package but the code differs on conditionals. How can I only change some lines depending on the conditional?

Assume I have the If: \newif\ifshell. If it is true there should be the following code in a lst-environment:

pdprint -l

and a few other lines (which are the same for both, true and false).

If \shell is false, there should be pdfprintsvr -l and the other lines instead.

How can I include the conditional inside the lst-environment? Thanks in advance

5

You can define an escape character.

In the following example I've choosen % (in this case it has to be escaped because it has a special meaning). You can choose the one you prefer, namely one you don't use inside the lstlisting environment.

This is the code:

\documentclass{article}
\usepackage{listings}

\begin{document}

\newif\ifshell

\section{Shell=true}

\shelltrue
\begin{lstlisting}[escapeinside={\%}{\%}]
%\ifshell%
pdprint -l
%\else%
pdfprintsvr -l
%\fi%
\end{lstlisting}

\section{Shell=false}

\shellfalse
\begin{lstlisting}[escapeinside={\%}{\%}]
%\ifshell%
pdprint -l
%\else%
pdfprintsvr -l
%\fi%
\end{lstlisting}

\end{document}

and this is the output:

enter image description here

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.