\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}

関連情報