セクションタイトルでハイパーリファレンスを使用して \newrobustcmd と \NewDocumentCommand を使用する方法

セクションタイトルでハイパーリファレンスを使用して \newrobustcmd と \NewDocumentCommand を使用する方法

および を有効にしてセクション タイトルでどのように使用できますか\newrobustcmd?以下の MWE は、レンダリングされた PDF は正しく表示されますが、ブックマークにマクロの内容が表示されないことを示しています。NewDocumentCommandhyperref

\documentclass[11pt]{article}

\usepackage{hyperref}
\usepackage{xparse}
\usepackage{etoolbox}

\newcommand{\testA}[0]{world}
\newrobustcmd{\testB}[0]{world}
\NewDocumentCommand{\testC}{}{world}

\begin{document}
    \section{Hello \testA}
    \section{Hello \testB}
    \section{Hello \testC}
\end{document}

結果のPDF

ファイルはMWE.out次のようになります。

\BOOKMARK [1][-]{section.1}{Hello world}{}% 1
\BOOKMARK [1][-]{section.2}{Hello }{}% 2
\BOOKMARK [1][-]{section.3}{Hello }{}% 3

答え1

では\newcommand、展開可能なマクロ ( \def) を定義しますが、 と では、\newrobustcmdエンジン\NewDocumentCommand保護されたマクロ ( \protected\def) を定義します。ブックマークのコンテンツは、ブックマーク文字列の構築中に展開されますが、保護されたマクロは展開できないため、MWE 内のトークン\testBとが\testCブックマーク内のそのままの状態になります。hyperrefでは、それらをどう処理すればよいかわからないため、破棄します。

Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref)                removing `\testB' on input line 13.


Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref)                removing `\testC' on input line 14.

したがって、保護されたマクロが本当に必要な場合は、次の 2 つの選択肢があります。

  • \section{Hello \texorpdfstring{\testB}{world}}文書内で使用するか
  • 読み込み後のプリアンブルに、hyperrefブックマークで使用されるより単純な拡張可能な定義を追加します。

    \pdfstringdefDisableCommands{%
      \def\testB{world}%
      \def\testC{wor‌​ld}%
    }
    

関連情報