# NewDocumentCommand-defined command in bookmark

I have a scenario where xparse's \NewDocumentCommand has a regression compared to any alternative I know, including ones defining robust commands. So I'm avoiding \NewDocumentCommand in this case, but I would like to understand where the problem lies.

I am trying to understand the difference between robust commands and commands defined by \NewDocumentCommand, from the point of view of hyperref's processing of bookmarks. Consider the following MWE and the comments inside.

\documentclass{article}

\usepackage{xparse}

\usepackage[T1]{fontenc}
\usepackage[utf8x]{inputenc}
\usepackage[unicode=true]{hyperref}

\usepackage{bookmark}
% The problem appears also without bookmark, but you need to compile twice to
% see the correct result.

% Allow using \lambda in section headers, in two parts.
% Part one (no problem here):
\PrerenderUnicode{λ}

% Part two:
% Problem: The following command can be robust, but cannot be defined via
% \NewDocumentCommand.

% All these definitions work:

%\def\TitleLambda{\texorpdfstring{$\lambda$}{λ}}
%\newcommand{\TitleLambda}{\texorpdfstring{$\lambda$}{λ}}
%\DeclareRobustCommand{\TitleLambda}{\texorpdfstring{$\lambda$}{λ}}

% This one doesn't:

\NewDocumentCommand{\TitleLambda}{}{\texorpdfstring{$\lambda$}{λ}}

% Done!

\begin{document}
\section{The \TitleLambda{}-calculus}
\end{document}


The above MWE gives the warning:

Package hyperref Warning: Token not allowed in a PDF string (Unicode):
(hyperref)                removing \TitleLambda' on input line 34.


As a consequence, the bookmark in the resulting file will contain no lambda character, unlike intended.

Replacing \NewDocumentCommand by any alternative avoids the problem. So the problem does not happen simply because \NewDocumentCommand defines robust commands, though what is happening appears similar. I've even tested using \def + \MakeRobustCommand from the makerobust package:

\usepackage{makerobust}
\MakeRobustCommand\TitleLambda


and everything still worked.

Who's at fault? I see three possibilities:

• me;
• xparse;
• hyperref.

Edit: I should have phrased my question better. I'm mainly curious about what happens, given that I have a working solution.

• Motivation: I don't like to use \texorpdfstring directly in the section header, it's distracting for me and coauthors. – Blaisorblade Nov 7 '13 at 23:54
• It's simple: \NewDocumentCommand creates a robust command similarly to \DeclareRobustCommand, but hyperref doesn't know how to deal with it. You just need \newcommand. – egreg Nov 7 '13 at 23:54
• So hyperref would need to add explicit support for \NewDocumentCommand? How inelegant. – Blaisorblade Nov 7 '13 at 23:55
• @egreg: yours should really be an answer, however short. – Blaisorblade Nov 7 '13 at 23:56
• I think the recommended approach here would be to use \DeclareExpandableDocumentCommand. – Bruno Le Floch Nov 8 '13 at 2:04

## 1 Answer

While hyperref is able to cope with \DeclareRobustCommand, it isn't with \NewDocumentCommand (this might be a feature request).

But \newcommand is all you need.

There is another way, though. It's incompatible with utf8x, but you can judge about its elegance.

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{newunicodechar}
\usepackage[unicode=true]{hyperref}
\usepackage{bookmark}
\newunicodechar{λ}{\texorpdfstring{\ensuremath{\lambda}}{λ}}

\begin{document}
\section{The λ-calculus}
\end{document}


• This should not be a feature request for hyperref, but for the L3 team: Integrate hyperref into L3. – Martin Schröder Nov 8 '13 at 22:50
• @MartinSchröder That's of course in the (long term) plans. – egreg Nov 8 '13 at 22:53
• I agree the second solution is rather elegant for people like me. – Blaisorblade Nov 22 '13 at 17:16
• I also wonder whether that could be integrated in something like unicode-math, but probably people don't want this to interfere with Unicode in normal input (outside of \$). – Blaisorblade Nov 22 '13 at 17:29
• @Blaisorblade What makes you think it doesn't work with unicode-math? It does, by the way. ;-)` Of course not if the document is in Greek. – egreg Nov 22 '13 at 17:40