I want to define a command with an optional star argument and be able to use it inside pdf strings, e.g. via \section. With the MWE below, I get a Token not allowed in a PDF string warning and \foo is just removed.

I can't think of a safe expandable way to replace \@ifstar in order to do this. For my application, it would be okay to default to \s@foo inside a pdf string, but the * should be removed if present.

Is there any way to achieve this?



  \def\@foo#1{one #1 one}
  \def\s@foo#1{two #1 two}



  • Depending on what you're actually trying to do, you can use \texorpdfstring{regular tex}{inside pdf string} from hyperref.
    – Teepeemm
    Commented Aug 27, 2019 at 11:51

1 Answer 1


With xparse, of course ;-)

\NewExpandableDocumentCommand \foo { s m }
      {one #2 one}% Starred
      {two #2 two}% Non-starred

The command that goes into the PDF bookmark has to be expandable, so you need an expandable mechanism to grab the optional *. Same goes for optional [...] arguments.

In the code above, the PDF bookmarks are one bar one and two baz two.

You must log in to answer this question.

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