32

I'm using hyperref, which causes trouble when having math in my section title.

MWE:

\documentclass{report}
\usepackage{hyperref}
\usepackage{fixltx2e}
\begin{document}
\section{Basis for CO\texorpdfstring{\textsubscript{2}{ h}} heat}
\section{Basis for CO\texorpdfstring{\textsubscript{2}} heat}
\section{Basis for CO\texorpdfstring{$_2$} heat}
\end{document}

The first text line will be displayed correctly, but why do I have to add { h} (I know this is not the correct solution, but it works)? If I don't, the space and the h will disappear. I found out that I had to add it by trial and error. How should this be done?

Heiko Oberdiek
  • 271,626
ROLF
  • 1,296
  • 5
  • 15
  • 22
  • 4
    \texorpdfstring takes two arguments: the first is typeset into the document, the second one is put in the bookmark. So \textorpdfstring{\textsubscript{2}}{2} for example. – Qrrbrbirlbel Jun 07 '15 at 20:14
  • @Qrrbrbirlbel: That's what I tried at first, but it doesn't work. – ROLF Jun 07 '15 at 20:17
  • 1
    @ROLF It works for me, given that \textsubscript is defined (which neither report nor hyperref nor fixltx2e (deprecated anyway) do). – Qrrbrbirlbel Jun 07 '15 at 20:19
  • @Qrrbrbirlbel: Aha, I thought \textsubscript was a standard thing. How should it be defined? – ROLF Jun 07 '15 at 20:27
  • \textsubscript is now in the LaTeX kernel (01/2015). You should redo your formats. – Bernard Jun 07 '15 at 21:33

1 Answers1

51

\texorpdfstring has two arguments, the first is the normal TeX code, the second is a string, which can be used as replacement for the arbitrary TeX in the bookmarks.

If option unicode (or pdfencoding=auto) is enabled, then the subscript 2 is available as \texttwoinferior:

\documentclass{report}
\usepackage[pdfencoding=auto]{hyperref}
\usepackage{bookmark}% faster updated bookmarks
\begin{document}
\section{Basis for
  CO\texorpdfstring{\textsubscript{2}}{\texttwoinferior} heat}
\end{document}
Heiko Oberdiek
  • 271,626
  • I'm confused why it looks just fine in my PDF bookmarks without \texorpdfstring despite the warning in the compile log? I mean, sure, it's not latex-ed, but in my case the chemical formula CO2 was cleanly replaced with just CO2. But i still get the warning. – K.-Michael Aye Mar 31 '19 at 02:03
  • 1
    @K.-MichaelAye The formula is CO₂ rather than CO2. There are lots of ways to write chemical formulas in LaTeX, also there are lots of warnings. Without knowing neither of them, helping is difficult. Maybe, make a new question with a minimal example and the full warning. – Heiko Oberdiek Apr 19 '19 at 21:36