I'm using \ifdefstring as part of a conditional definition which depends on an environment variable, as indicated in this question.
It works mostly fine, except that inside the blocks defined by \ifdefstring, if I have a \lstinline containing backslashes, I need to escape them, otherwise it won't work.
I'd like to be able to add \ifdefstring blocks without having to escape/unescape what's inside, especially because the error message is not very clear: missing backslashes lead to cryptic error messages at the end of the block. Is there a way to do so?
Here's an MVE, where in each case (both inside and outside the \ifdefstring) a single backslash must be printed.
\documentclass{article}
\usepackage{listings}
\usepackage{etoolbox}
% catchfile block copied from other question
\usepackage{catchfile}
\newcommand{\getenv}[2][]{%
\CatchFileEdef{\temp}{"|kpsewhich --var-value #2"}{\endlinechar=-1\relax}%
\if\relax\detokenize{#1}\relax\temp\else\edef#1{\temp}\fi}
\getenv[\VAR]{VAR}%
\begin{document}
\ifdefstring{\VAR}{yes}{
\lstinline|\\must_be_escaped_yes|
}{
\lstinline|\\must_be_escaped_not_yes|
}
\lstinline|\must_not_be_escaped|
\end{document}
The output (here, with VAR != yes) is:
But to make it parsable, I had to manually add the extra \ inside the \lstinline parts inside the \ifdefstring blocks, which I'd like to avoid.



VARshould be somehow escaped and what you mean by that? – egreg Nov 15 '16 at 17:01VAR, I would like to have LaTeX code that is the same whether it is inside an\ifdefstringblock or not. – anol Nov 15 '16 at 18:13\lstinlineis a verbatim like command; it has features that allow putting it in the argument to a command, but you need some escapes, in this case. – egreg Nov 15 '16 at 18:15