\jobname을 구문 분석하려고 할 때 이러한 폭주 인수가 발생하는 원인은 무엇입니까?

\jobname을 구문 분석하려고 할 때 이러한 폭주 인수가 발생하는 원인은 무엇입니까?

나중에 사용할 수 있도록 true/false 플래그를 설정하기 위해 (Xe)LaTeX 문서의 작업 이름을 구문 분석하려고 합니다. 나는 우연히 만났다이 솔루션이는 유망해 보였고 제가 구현한 것입니다(다음 MWE 참조).

\documentclass{report}

\newif\ifFraktur
\makeatletter%
\newcommand{\filenameparse}[1]{\expandafter\filename@parse@#1\@nil}%
\def\filename@parse@#1_#2\@nil{%
   \gdef\filenameflag{#2}%
}%
\makeatother
\filenameparse{\jobname}%

\if{f}\filenameflag{%
   \Frakturtrue}%
\else{%
   \Frakturfalse}%
\fi%

\begin{document}

\ifFraktur{We have an f document}%
\else{We have an a document.}%
\fi
\end{document}

이 파일의 파일 이름은 실제 파일( , 여기서 mwe-1-0_a.tex사용할 파일 이름)에 해당합니다.version-m-n_f.texm그리고n버전 번호에 대한 자리 표시자이며fa은 또는 일 수 있는 플래그의 자리 표시자입니다 f.

이것을 실행하면 다음과 같은 오류가 발생합니다.

Runaway argument?
mwe-1-0_a\@nil
! Paragraph ended before \filename@parse@ was complete.
<to be read again>
                   \par
l.11

오류를 무시하면(누름 enter) 문서가 더 이상 오류 없이 컴파일될 수 있습니다.

문제의 원인을 파악하려고 노력하면서 다음 내용을 복사하기로 결정했습니다.이미 연결된 답변, 그러나 다음과 같이 약간 수정했습니다.

\documentclass{article}

\makeatletter
\newcommand{\filenameparse}[1]{\expandafter\filename@parse@#1\@nil}
\def\filename@parse@#1_#2_#3\@nil{%
  \gdef\fileA{#1}% first part
  \gdef\fileB{#2}% middle part
  \gdef\fileC{#3}% final part
}
\makeatother

\filenameparse{\jobname}

\begin{document}

\fileA \par
\fileB \par
\fileC
\end{document}

놀랍게도 이는 폭주 주장을 생성할 뿐만 아니라 세 가지 매크로 중 어느 것도 \fileA정의 되지 \fileB않습니다 \fileC. 그 결과 (위와 같이)를 검토 mwe-1-0_f.tex한 결과 컴파일되었지만 마지막 \ifFraktur절은 false.

여기에는 여러 가지 문제가 있을 수 있지만, 당분간은 폭주 논쟁의 원인에 가장 관심이 있을 것입니다.

답변1

의 문자에는 \jobnamecatcode 12(예 \meaning: 또는 \string)가 있으므로 catcode 12를 원합니다._

\documentclass{report}

\newif\ifFraktur
\makeatletter%
\catcode`\_=12
\newcommand{\filenameparse}[1]{\expandafter\filename@parse@#1\@nil}%
\def\filename@parse@#1_#2\@nil{%
   \gdef\filenameflag{#2}%
}%
\catcode`\_=8
\makeatother
\filenameparse{\jobname}%

\edef\testf{\string f}
\ifx\testf\filenameflag
   \Frakturtrue
\else
   \Frakturfalse
\fi%

\begin{document}

\ifFraktur
We have an f document
\else
We have an a document.
\fi
\end{document}

답변2

다음은 내 코드를 기반으로 버전 번호를 추출할 수 있는 구현입니다.파일 이름을 문서로 구문 분석

\documentclass{article}
\usepackage{xparse,l3regex}

\ExplSyntaxOn
\cs_generate_variant:Nn \regex_split:nnN { nV }
\seq_new:N \l_liz_jobname_seq

\NewDocumentCommand{\splitjobname}{m}
 {
  \regex_split:nVN { #1 } \c_sys_jobname_str \l_liz_jobname_seq
 }

\DeclareExpandableDocumentCommand{\jobnamepart}{m}
 {
  \seq_item:Nn \l_liz_jobname_seq { #1 }
 }

\DeclareExpandableDocumentCommand{\isfrakturTF}{mm}
 {
  \str_if_eq_x:nnTF { \jobnamepart{-1} } { f } { #1 } { #2 }
 }
\ExplSyntaxOff

\splitjobname{ [ _ \- ] } % at _ or -

\begin{document}

This document's name \isfrakturTF{has}{has not} a trailing `f'.

First part: \jobnamepart{1}\par
Second part: \jobnamepart{2}\par
Third part: \jobnamepart{3}

\end{document}

파일 이름이 이면 mwe-1-0_a.tex출력은 다음과 같습니다.

여기에 이미지 설명을 입력하세요

이름이 이면 mwe-1-0_f.tex출력은 다음과 같습니다.

여기에 이미지 설명을 입력하세요

관련 정보