0

This is related to a previous question of mine, how to use listings in conditionals.

I'm using the answer kindly provided there to include different code snippets when their library of origin is included in different documents. Now I would like to do the same for other libraries/documents without writing a new \if every time. In other words, I would like to make the conditional itself reusable.

Up until now, I've worked with \IfSubStr, but I'm having trouble figuring out how to pack the new \if into a reusable macro.

To illustrate what I'm talking about, this has been my way of using conditionals for regular text (which works fine):

\NewDocumentCommand{\ifjobnamecontains}{ m +m +m }{%     
   \IfSubStr{\jobname}{\detokenize{#1}}%       
    {#2}%  
    {#3}%
    }

>>>>

\ifjobnamecontains{<DOCUMENTA>}{Here is text that goes in Document A.} {Here is text that goes in all documents that are not Document A.}

However, I can't use this type of conditional for code snippets. Is there any way to make the \if conditional from the following MWE similarly reusable? (Note: The document name in this MWE is MWE.tex).

\documentclass[a4paper]{article}

\newif\ifmwe \usepackage{listings} \usepackage{xstring}

\lstset{basicstyle=\ttfamily,breaklines=true}

\begin{document}

Here is lstlisting within a conditional:

\IfSubStr{\jobname}{\detokenize{MWE}}% {\mwetrue}%true {\mwefalse}%false

\ifmwe \begin{lstlisting} This is sample code. \end{lstlisting}
\else fasle \fi

serval
  • 107
  • 2
    Have a look at https://tex.stackexchange.com/a/355085/233251 (the answer explaining that "You can't easily pass verbatim content as part of macro arguments" and suggests an alternative option) – Iacobus1983 Jun 30 '23 at 13:26
  • Thank you very much. I'll look into it. :-) – serval Jul 03 '23 at 05:38

0 Answers0