%20section%20%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%E3%81%A7%E3%81%AF%E4%BD%BF%E7%94%A8%E3%81%A7%E3%81%8D%E3%81%BE%E3%81%9B%E3%82%93%E3%80%82.png)
babel
のオプションは、自動的に を前に付けることで、frenchb
eg 文字をアクティブにします。このようなアクティブ文字は、のブックマークによってセクションで適切にサポートされます。ただし、仕様を使用して独自のセクション コマンドを作成しようとすると、次の MWE で指摘されているように、よく知られている警告が表示されます。!
\thinspace
hyperref
\addcontentsline
Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding)
\documentclass[french]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{hyperref}
\newcommand{\mynaivesection}[1]{%
\textbf{#1}%
\addcontentsline{toc}{section}{#1}%
}
\begin{document}
\tableofcontents
\section{Foo !}
\mynaivesection{Bar !}
\end{document}
この警告を取り除くために、(意図的に素朴な)カスタマイズされたセクション コマンドに何が欠けているのでしょうか?
ところで:
\textbf{#1}%
このカスタム セクション コマンドで削除すると警告が表示されません。かなり奇妙です...bookmark
パッケージはここでは役に立ちません。
答え1
引数をサニタイズするのは hyperref ではありません。\section
と コマンドの主な違いは、 が\addcontentsline
垂直モードで最初に発行されることです。 ! の定義はモードによって異なります。
\french@sh@!@ ->\ifhmode \ifdim \lastskip >\z@ \unskip \penalty \@M \FBthinspace
\else \FDP@thinspace \fi \fi \string !
を挿入すると警告は消えます (また、 vmode であるため\par
の前にテキストがない場合も警告は消えます)。\addcontentsline
\documentclass[french]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage[]{hyperref}
\usepackage{etoolbox}
\pretocmd\addcontentsline{\ifvmode \message{V-Mode!#3}\else\message{H-Mode!#3}\fi}{}{}
\newcommand{\mynaivesection}[1]{%
#1\addcontentsline{toc}{section}{{#1}}}%
\begin{document}
\tableofcontents
\section{Foo !}
\mynaivesection{Bar!}
\renewcommand{\mynaivesection}[1]{%
#1\par\addcontentsline{toc}{section}{{#1}}}%
\mynaivesection{FooBar!}
\end{document}
答え2
何もしないようにローカルで再定義することもできます\FBthinspace
。
\newcommand{\mynaivesection}[1]{%
\textbf{#1}%
\begingroup%
\renewcommand{\FBthinspace}{}%
\addcontentsline{toc}{section}{#1}%
\endgroup%
}
MWE:
\documentclass[french]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{hyperref}
\newcommand{\mynaivesection}[1]{%
\textbf{#1}%
\begingroup%
\renewcommand{\FBthinspace}{}%
\addcontentsline{toc}{section}{#1}%
\endgroup%
}
\begin{document}
\tableofcontents
\section{Foo !}
\mynaivesection{Bar !}
\end{document}
出力: