3

I am using math symbols in \section or \subsection. Therefore I need to use \texorpdfstring inside of \section or \subsection to show the math symbols properly. For example, my code \section{Proof of \texorpdfstring{$\mu=0$} is correct}. Here are my questions:

  1. Missing characters. Characters are missing by using my code \section{Proof of \texorpdfstring{$\mu=0$} is correct}. It shows something as follows. The space and character i is missing following \mu=0. I find a similar question How to use \texorpdfstring. Characters disappear. However, I still don't understand how to use \texorpdfstring properly not just for this case but for general situations. According to the answer of the similar question mentioned above, "\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." I still don't understand what the second argument is. What is to be put into the second argument?
  2. Are there any official documents introducing \texorpdfstring? I can't find one.

enter image description here

2 Answers2

5

The command \texorpdfstring takes two arguments:

  1. what should be typeset in the PDF, and
  2. what should appear in the bookmark.

In your code \texorpdfstring{$\mu=0$} is the first argument is $\mu=0$ and the second one is i. Thus i disappears in the title and will appear in the bookmark, that will have “Proof of is correct”.

Minimal example:

\documentclass{article}
\usepackage{hyperref}

\begin{document}

\section{Title \texorpdfstring{xyz}{abc} here}

\end{document}

enter image description here

As you see, “xyz” is printed in the section title, but it's replaced by “abc” in the bookmark.

When and why would you use it? For instance, there's no typesetting going on in bookmarks, where $\mu=0$ is invalid. You can use \texorpdfstring to provide some “emulation”.

\documentclass{article}
\usepackage{hyperref}

\begin{document}

\section{Proof of \texorpdfstring{$\mu=0$}{μ=0} is correct}

\end{document}

Your mileage may vary.

enter image description here

egreg
  • 1,121,712
  • Thank you so much for your help!!! – liaoming999 Oct 22 '23 at 09:52
  • Thank you so much for your help!!! I have one more question. I know the first argument for '\texorpdfstring' is the normal latex language. My question is, how do I find the proper second argument? For example, what should be the second argument if I want to use the newline command $\$ for the first argument? What about other math symbols? I saw many weird second arguments. Is there any hand book to check it out? – liaoming999 Oct 22 '23 at 09:58
  • @liaoming999 There's no rule. It depends on what and can emulate. – egreg Oct 22 '23 at 10:00
  • 1
    @liaoming999 you should almost never have \\ in the argument anyway as that would mess up the table of contents and page headings, if you need a \\ in the main heading, use \section[short version]{long version\\ with newline} and the short version without \\ will be used for table of contents and the pdf bookmarks – David Carlisle Oct 22 '23 at 10:05
3

An alternative is to write the expression in a way that you do not need to use \texorpdfstring

\documentclass{article}
\usepackage{hyperref}
\DeclareUnicodeCharacter{03BC}{\mu}
\begin{document}

\section{Proof of $μ=0$ is correct}

\end{document}

enter image description here

David Carlisle
  • 757,742