![セクションタイトルでハイパーリファレンスを使用して \newrobustcmd と \NewDocumentCommand を使用する方法](https://rvso.com/image/353010/%E3%82%BB%E3%82%AF%E3%82%B7%E3%83%A7%E3%83%B3%E3%82%BF%E3%82%A4%E3%83%88%E3%83%AB%E3%81%A7%E3%83%8F%E3%82%A4%E3%83%91%E3%83%BC%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%A6%20%5Cnewrobustcmd%20%E3%81%A8%20%5CNewDocumentCommand%20%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95.png)
および を有効にしてセクション タイトルでどのように使用できますか\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}% }