3

Is it possible to mimic the output of

\section{\texorpdfstring{$x_1$}{x\_1}}

using \section{$x_1$} and \pdfstringdefDisableCommands{...} ? I've only seen this answer, which tries to do something similar, but it doesn't work (as they acknowledge). I'm not too concerned about warnings; I just want x_1 to appear in the bookmarks for the PDF, while not having \texorpdfstring{...}{...} for every instance of an underline.


MWE for the error generated by tikz when using @StevenB.Segletes's first solution:

\documentclass{article}
\usepackage{hyperref}

%\usepackage{tikz} % OK

\let\svus_ \catcode`_=\active \gdef_{\texorpdfstring{\svus}{\string_}}

%%%
% pgfmathparser.code.tex % ! Missing \endcsname inserted. % <to be read again> % \svus % l.251 ...mnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ} % \usepackage{tikz}
%%%

\begin{document} \tableofcontents

\section{$x_1$}

Document math, $a^3_1$ should be no problem

\section{$y_1$}

Does This_work?

\end{document}

llf
  • 175

3 Answers3

4

Recall that hyperref warns you about the subscript character with something like

Package hyperref Warning: Token not allowed in a PDF string (Unicode):
(hyperref)                removing `subscript' on input line 33.  

So clearly hyperref knows that it encounters a subscript character. All you need to do is to tell hyperref that, the next time you see _, instead of a warning, you put that character into the pdf toc.

The following code does that.

\documentclass{article}

\usepackage{tikz}

\usepackage{hyperref}

\makeatletter \def\subscripttext{subscript} \def\HyPsd@CatcodeWarning#1{% \def\argone{#1} \ifx\subscripttext\argone \expandafter\def\expandafter\HyPsd@String\expandafter{\HyPsd@String_}% \else \HyPsd@Warning{% Token not allowed in a PDF string (% \ifHy@unicode Unicode% \else PDFDocEncoding% \fi ):% \MessageBreak removing `\HyPsd@RemoveCmdPrefix#1'% }% \fi }

\begin{document}

\tableofcontents

\section{$x_1$} Document math, $a^3_1$ should be no problem \section{$y_1$} Does This_work?

\end{document}

This approach allows you to not setting the catcode of _. There are only this many special characters and a bunch of packages are competing with each other for a chance to define the activated characters. TikZ, as you noticed, is an expert in that sort.

Symbol 1
  • 36,855
  • I've changed the accepted answer to this one since it uses the tools provided by hyperref. Should there be a \makeatother after the last \def block? – llf Sep 17 '21 at 19:32
  • Yes there should be a \makeatother. – Symbol 1 Sep 17 '21 at 19:37
3

You can make the underscore math active.

You'll get warnings

Package hyperref Warning: Token not allowed in a PDF string (Unicode):
(hyperref)                removing `math shift' on input line 17.

but the output will be as expected.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz} % OK
\usepackage{hyperref}
\usepackage{bookmark}

\AtBeginDocument{% \begingroup\lccode~=_ \lowercase{\endgroup\let~\sb}% \mathcode_=&quot;8000 \catcode_=12 }

\begin{document} \tableofcontents

\section{$x_1$}

Document math, $a^3_1$ should be no problem

\section{$y_1$}

This_works and also_this.

\end{document}

enter image description here

egreg
  • 1,121,712
2

Here I make _ active to accomplish it. Not sure if that works for your need.

\documentclass{article}
\usepackage{hyperref}
\let\svus_
\catcode`\_=\active
\gdef_{\texorpdfstring{\svus}{\string_}}
\begin{document}
\tableofcontents
\section{$x_1$}

Document math, $a^3_1$ should be no problem

\section{$y_1$}

Does This_work?

\end{document}

enter image description here

Perhaps a better way is to use \mathcode, so that _ is not active, but catcode-12 inside the document:

\documentclass{article}
\usepackage{hyperref}
\let\svus_
\mathcode\number`\_="8000
\begingroup\lccode`\~=`\_ \lowercase{\endgroup\def~}%
  {\texorpdfstring{\svus}{\string_}}
\catcode`\_=12
\begin{document}
\tableofcontents
\section{$x_1$}

Document math, $a^3_1$ should be no problem

\section{$y_1$}

Does This_work?

\end{document}

  • Thanks, they both seem to work in my use case. When loading tikz after the first solution, it complains with Missing \endcsname inserted. on \svus. I've added a MWE for that error in the original post, feel free to copy it if you want to add an explanation. – llf Sep 14 '21 at 04:02