"특수" catcode(특히 `_`)가 포함된 문자열을 사용자 정의 NewDocumentCommand 명령에 전달

"특수" catcode(특히 `_`)가 포함된 문자열을 사용자 정의 NewDocumentCommand 명령에 전달

\newcommand에 밑줄을 올바르게 전달하려면 어떻게 해야 합니까?_해결책은 실제 명령으로 확장하기 전에 의 catcode를 변경하는 인수 없는 명령을 사용하는 것이라고 제안합니다 .

\NewDocumentCommand그것은 다소 어색하고 난독화되는 인수 유형이기 때문에 내가 사용하고 싶은 이유와는 정반대입니다 .

expl3/xparse/ 버블의 편안함을 벗어나지 않고 밑줄(파일 이름)이 포함된 문자열을 전달할 수 있는 방법이 있습니까 \NewDocumentCommand?

나는 인수를 사용하고 싶지 않습니다 v. 실제로는 일반 매크로처럼 작동해야 \includechapter{foo_bar}하며 \includechapter!foo_bar!.


현재 내 코드:

%%% Chapter Inclusion Macros
\RequirePackage{expl3}
\ExplSyntaxOn
% Command to include chapter files, if
% either the exclusive chapter list is empty,
% or said chapter is in there
\cs_set:Npn \cel_includechapter:n #1 {
  % Check whether list is empty
  \clist_if_empty:NTF
  \g_cel_enabled_clist % which list
  {\include{#1/#1}} % if empty, just include
  { % else
    % check whether argument in list of enabled chapters
    \clist_if_in:NnTF 
    \g_cel_enabled_clist % in which list
    {#1} % which element to look for
    {\include{#1/#1}} % if in there
    {\chapter{#1~(currently~disabled)}} %if not in there
  }
}
% user-facing command \includechapter
% includes chaptername/chaptername
% if enabled
\NewDocumentCommand{\includechapter}{m}{
  \cel_includechapter:n{#1}
}
\NewDocumentCommand{\enableChapter}{m}{
  \clist_put_right:Nn \g_cel_enabled_clist {#1}
}
\ExplSyntaxOff

도달하면 빌드가 중단됩니다.

\includechapter{foo_bar}

~와 함께

! Missing $ inserted.
<inserted text>
                $
l.147 \includechapter{foo_bar}

답변1

문제는 당신이 말하는 곳입니다.

{\chapter{#1~(currently~disabled)}} %if not in there

파일 이름에 밑줄이 있으면 수학 모드가 실행되기 때문입니다.

해결책: 다음으로 변경

{\chapter{\tl_to_str:n {#1}~(currently~disabled)}} %if not in there

답변2

이 경우 해결책은 v-type 인수를 사용하는 것입니다.

\NewDocumentCommand{\includechapter}{v}{
  \cel_includechapter:n{#1}
}
\NewDocumentCommand{\enableChapter}{v}{
  \clist_put_right:Nn \g_cel_enabled_clist {#1}
}

밑줄이 포함된 경우 전달된 인수를 조판하기 전에 처리해야 하기 때문에 여전히 더 많은 수정이 필요합니다.

다행히도 이 매크로를 다른 함수(usrguide3.pdf)에 대한 인수로 사용하고 싶지 않습니다.

v: LATEX 2ε 명령의 인수와 유사한 방식으로 다음 문자와 다음 문자 사이의 '축어적' 인수를 읽습니다 \verb. 따라서 -type 인수는 , , , 또는 중 하나 일 v수 없는 두 개의 동일한 문자 사이에서 읽혀집니다 . 축어적 인수는 중괄호 및 로 묶을 수도 있습니다 .%\#{}{}축어적 인수가 있는 명령은 다른 함수의 인수 내에 나타날 때 오류를 생성합니다.

관련 정보