8

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.

7
  • 1
    Motivation: I don't like to use \texorpdfstring directly in the section header, it's distracting for me and coauthors. Commented Nov 7, 2013 at 23:54
  • 1
    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
    Commented Nov 7, 2013 at 23:54
  • So hyperref would need to add explicit support for \NewDocumentCommand? How inelegant. Commented Nov 7, 2013 at 23:55
  • @egreg: yours should really be an answer, however short. Commented Nov 7, 2013 at 23:56
  • I think the recommended approach here would be to use \DeclareExpandableDocumentCommand. Commented Nov 8, 2013 at 2:04

1 Answer 1

8

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}

enter image description here

7
  • This should not be a feature request for hyperref, but for the L3 team: Integrate hyperref into L3. Commented Nov 8, 2013 at 22:50
  • @MartinSchröder That's of course in the (long term) plans.
    – egreg
    Commented Nov 8, 2013 at 22:53
  • I agree the second solution is rather elegant for people like me. Commented Nov 22, 2013 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 $). Commented Nov 22, 2013 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
    Commented Nov 22, 2013 at 17:40

You must log in to answer this question.

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