
および を有効にしてセクション タイトルでどのように使用できますか\newrobustcmd
?以下の MWE は、レンダリングされた PDF は正しく表示されますが、ブックマークにマクロの内容が表示されないことを示しています。NewDocumentCommand
hyperref
\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}
ファイルは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{world}% }