별표가 있는 명령과 별표가 없는 명령이 있는 \pdfstringdefDisableCommands

별표가 있는 명령과 별표가 없는 명령이 있는 \pdfstringdefDisableCommands

다음 MCE:

\documentclass{article}
\usepackage{hyperref}
\ExplSyntaxOn
\NewDocumentCommand { \foo } { s m } {
  \IfBooleanTF {#1}{
    Foo~ #2
  }{
    #2
  }
}
\pdfstringdefDisableCommands{
  \def\foo*#1{Foo~ #1}
  % \def\foo#1{#1}
}
\ExplSyntaxOff
\begin{document}
\section{\foo*{bar}}
% \section{\foo{bar}}
\end{document}

주석 처리가 없는 경우를 제외하고는 매력처럼 컴파일됩니다. 다음과 \section{\foo{bar}}같은 경우 오류 메시지가 표시됩니다.

! Use of \foo doesn't match its definition.
<argument> ...rline {\csname thesection\endcsname }\fi \foo {
                                                  bar}

\pdfstringdefDisableCommands별표가 있는 명령과 별표가 없는 명령을 모두 사용할 수 있는 방법이 있습니까 ?

답변1

원래 명령과 동일한 서명을 사용하여 명령을 다시 정의해야 합니다. s m확장 가능한 명령에 대한 유효한 서명이므로 다음을 사용하여 재정의할 수 있습니다 \RenewExpandableDocumentCommand.

\documentclass{article}
\usepackage{hyperref}
\ExplSyntaxOn
\NewDocumentCommand { \foo } { s m } {
  \IfBooleanTF {#1}{
    Foo~ #2
  }{
    #2
  }
}
\pdfstringdefDisableCommands{
  \RenewExpandableDocumentCommand \foo { s m }
    { Foo~ #2 }
}
\ExplSyntaxOff
\begin{document}
\section{\foo*{bar}}
\section{\foo{bar}}
\end{document}

예제에서 명령에는 \foo확장 가능한 매크로만 포함되어 있으므로 확장 전용 컨텍스트에서 작동할 수 있으므로 \NewExpandableDocumentCommand직접 사용하여 정의하는 경우 다음을 다시 정의할 필요가 없습니다 hyperref.

\documentclass{article}
\usepackage{hyperref}
\ExplSyntaxOn
\NewExpandableDocumentCommand { \foo } { s m } {
  \IfBooleanTF {#1}{
    Foo~ #2
  }{
    #2
  }
}
\ExplSyntaxOff
\begin{document}
\section{\foo*{bar}}
\section{\foo{bar}}
\end{document}

관련 정보