5

babel's frenchb option makes active e.g. ! character by making it automatically preceded by a \thinspace. Such active characters are properly supported in sections by hyperref's bookmarks. But, when I try to create my own section command with an \addcontentsline specification, this leads to the well known Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding) warning, as pointed out by the following MWE:

\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}

What is missing in my (deliberately naive) customized section command in order to get rid of this warning?

BTW:

  1. there isn't any warning if I remove \textbf{#1}% in this custom section command: rather strange...
  2. bookmark package doesn't help here.
7

It is not hyperref that sanitizes the argument. The main difference between \section and your command is that the \addcontentsline is issued for the first in vertical mode. And the definition of the ! depends on the mode:

\french@sh@!@ ->\ifhmode \ifdim \lastskip >\z@ \unskip \penalty \@M \FBthinspace
\else \FDP@thinspace \fi \fi \string !

The warning disappears if you insert a \par (and also if there is no text before the \addcontentsline as you are in vmode then):

\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}
6

You can locally redefine \FBthinspace to do nothing.

\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} 

Output:

enter image description here

  • Indeed, but (1) aren't the \FBthinspace features lost also for the table of contents? (2) is it the way the "official" \section supports active characters? – Denis Bitouzé Feb 20 '15 at 9:32
  • 1
    (1) No, at the time of printing the ToC \FBthinspace feature is in effect, so you don't lose it. (2) I have no idea. – karlkoeller Feb 20 '15 at 9:35
  • 2
    @DenisBitouzé: You can easily test the scope of such a redefinition. Simply do \renewcommand{\FBthinspace}{HALLO} and then look where the HALLO is. – Ulrike Fischer Feb 20 '15 at 12:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.