
\ifdefstring
표시된 대로 환경 변수에 의존하는 조건부 정의의 일부로 사용하고 있습니다 .이 질문에.
로 정의된 블록 내부에 백슬래시가 포함 \ifdefstring
된 경우 \lstinline
이를 이스케이프해야 한다는 점을 제외하면 대부분 잘 작동합니다. 그렇지 않으면 작동하지 않습니다.
특히 오류 메시지가 명확하지 않기 때문에 내부 내용을 이스케이프/언이스케이프하지 않고도 블록을 추가할 수 있기를 바랍니다. \ifdefstring
백슬래시가 누락되면 블록 끝에 비밀스러운 오류 메시지가 표시됩니다. 그렇게 할 수 있는 방법이 있나요?
\ifdefstring
다음은 각 경우( 내부 및 외부 모두 ) 단일 백슬래시를 인쇄해야 하는 MVE입니다 .
\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}
출력(여기서 VAR != yes
)은 다음과 같습니다.
하지만 구문 분석이 가능하도록 하려면 블록 내부 부분 \
에 추가 항목을 수동으로 추가해야 했는데 , 이는 피하고 싶습니다.\lstinline
\ifdefstring
답변1
\lstinline
다른 축어적 명령과는 약간 다르기 때문에~할 수 있다다른 명령에 대한 인수로 이동하지만 제한이 있습니다.
\else
– \fi
형식 을 정의하면 \ifdefstring
이스케이프되지 않은 버전을 사용할 수 있습니다.
\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}
\newcommand{\defstring}[2]{%
TT\fi
\ifdefstring{#1}{#2}{\iftrue}{\iffalse}%
\ignorespaces
}
\getenv[\VAR]{VAR}
\begin{document}
\if\defstring{\VAR}{yes}
\lstinline|\must_be_escaped_yes|
\else
\lstinline|\must_be_escaped_not_yes|
\fi
\lstinline|\must_not_be_escaped|
\end{document}