1

The following 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}

compiles like a charm, except if the \section{\foo{bar}} is uncommented, the error message being in such a case:

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

Is there a way to use \pdfstringdefDisableCommands with both starred and non starred commands?

1 Answer 1

2

You have to redefine the command using the same signature as the original command. Since s m is a valid signature for an expandable command, you can just redefine using \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}

Note that in your example, the command \foo contains only expandable macros, so it can work in an expansion-only context, so if you define it using \NewExpandableDocumentCommand directly, you don't need to redefine for 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}
2
  • AFAICS, your second method which makes use of \NewExpandableDocumentCommand leads to ”Token not allowed in a PDF string (Unicode):“ warning if the \foo command involves a \cs_new_protected:Npn function. May 9, 2022 at 20:03
  • 1
    @DenisBitouzé Yes, because in that case, \foo contains a non-expandable token, thus, it cannot be expandable. I meant that in the specific case of the example you posted, the whole function can be made expandable. I clarified the text in the answer May 9, 2022 at 20:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .