0

Why results of an optional argument don't appear at bookmarks?

I was writing a text using some commands to insert formatted text. At some moment I added an optional argument and then I realized that the optional command result doesn't appear at the bookmark of the PDF, although it still appears at the table of contents.

I filtered the scenario into three simple cases of analysis:

  • \foo is just a simple newcommand and works fine.

  • \bla has a mandatory argument and works fine.

  • \cmd has only an optional argument and doesn't appear at the bookmark list, nevertheless "[v]" used as optional argument is still presented at the bookmark.

The following MWE reproduces the issue.

\documentclass{article}
\usepackage{hyperref}
\newcommand{\foo}{A}
\newcommand{\bla}[1]{B{#1}}
\newcommand{\cmd}[1][]{C{#1}}
\begin{document}
\tableofcontents
\section{{\foo} -- {\bla{s}} -- {\cmd} -- {\cmd[v]}}
\end{document}

As seen at the image, the results of \cmd appears at title and \tableofcontents, but not at the bookmarks on the PDF.

enter image description here

4
  • 2
    Commands with optional arguments defined with \newcommand don't work in bookmarks. Try \NewExpandableDocumentCommand instead, or define a suitable replacement in \pdfstringdefDisableCommands. Oct 31 '20 at 1:04
  • Hi @PhelypeOleinik. This command is from the package xparse. I tried \NewExpandableDocumentCommand but could not understand how to use only one optional argument -- indeed, on of the six requirements say the last argument must be an mandatory argument. I only could use one single optional argument with the standard \NewDocumentCommand, but this doesn't show at bookmarks. I will still chck about ´\pdfstringdefDisableCommands`.
    – FHZ
    Nov 2 '20 at 2:43
  • 1
    Basically, you can't have a single optional argument + bookmarks (a single optional argument only works with looking ahead (\@ifnextchar), and looking ahead is not expandable, thus no bookmarks). \NewExpandableDocumentCommand allows optional arguments without looking ahead, but requires a final mandatory argument (if it didn't require, you could get mysterious errors if the optional argument were missing). With \pdfstringdefDisableCommands you can define your command to either require the optional argument, or ignore it, but not be actually optional. You're in a tough spot :) Nov 2 '20 at 2:54
  • Understood. I downgraded the command to a version without the optional argument, since the bookmark appearance was more important than the flexibility given from the option. Although this finding was unexpected, I must say it was interesting to learn it and to avoid this issue in the future. Thanks.
    – FHZ
    Nov 2 '20 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.