8

Is it a bug of unicode-math or hyperref in LuaLaTeX?

    % !TeX program = lualatex                                   
    % !TeX encoding = utf8
    
    \documentclass[14pt]{extarticle}
    
    \usepackage{fontspec}
    \usepackage{amsmath}
    \setsansfont{CMU Sans Serif}
    \setmainfont{CMU Serif}
    \setmonofont{CMU Typewriter Text}
    \usepackage{unicode-math} % this one DOES NOT cause error without hyperref
    \usepackage[english]{babel}
    \usepackage{hyperref} % this one DOES NOT cause error without unicode-math
    
    
    \begin{document}
    
    \section{title $\alpha$}
    
    \end{document}

I get an error

Improper alphabetic constant. \section{title $\alpha$}

4
  • 3
    It is a known problem. Use \texorpdfstring to add an alternative for the bookmarks. Commented Nov 8, 2020 at 8:13
  • 3
    Try changing \section{title $\alpha$} to \section{title \texorpdfstring{$\alpha$}{alpha}}.
    – Mico
    Commented Nov 8, 2020 at 8:14
  • Pdflatex does not notice the problem, but even with \texorpdfstring{$\alpha$}{alpha}} does not add alpha in bookmarks. Commented Nov 8, 2020 at 8:27
  • If you are using unicode=math, compile with xelatex or lualatex. With xelatex, I get bookmarks with α, and alpha is the bookmark with \texorpdfstring. \newpage\section{title α1} x \newpage\section{title \textit{α}2} x \newpage\section{title \ensuremath{\mathit{α}}3} x \newpage\section{title \texorpdfstring{$\alpha$}{alpha}4}
    – Cicada
    Commented Nov 8, 2020 at 8:38

2 Answers 2

11

bookmarks can contain only rather simple text. This means that hyperref has to translate LaTeX input into something that makes sense there. For this it often drops or replace problematic content. In general hyperref is quite good to at least avoid errors in this process, but it can't handle commands which have been let to a char: https://github.com/latex3/hyperref/issues/63. And sadly unicode-math does this in a number of places.

You can use \texorpdfstring to offer hyperref an alternative. For example the direct unicode char U+1D6FC would work with most pdf readers. You can input it directly or with the ^^ notation:

\documentclass[14pt]{extarticle}

    \usepackage{fontspec}
    \usepackage{amsmath}
    \setsansfont{CMU Sans Serif}
    \setmainfont{CMU Serif}
    \setmonofont{CMU Typewriter Text}
    \usepackage{unicode-math} % 
    \usepackage[english]{babel}
    \usepackage{hyperref} % 


    \begin{document}

    \section{title \texorpdfstring{$\alpha$}{𝛼 or ^^^^^^01d6fc}}

    \end{document}

enter image description here

6

I believe you've encountered a basic shortcoming in the way pdf bookmarks may be specified, not a shortcoming in either unicode-math or hyperref per se.

The \texorpdfstring device may be used to work around this shortcoming. E.g., under LuaLaTeX,

\section{title \texorpdfstring{$\alpha$}{$\symit{alpha}$}}

will result in title alpha (note: not title α) showing up in the bookmarks.

Aside: Even though slot x03B1 in the Unicode table points to α and the CMU Serif font has the correct symbol in that slot,

\section{title \texorpdfstring{$\alpha$}{\symbol{"03B1}}}

doesn't work at all, as the bookmark now says title "03B1. This is definitely far less satisfactory than title alpha, isn't it? (This is also why I opined up front that there's a fundamental issue with the way bookmarks can be formed.)

What's the moral of this story? Maybe, it's that one should avoid using math-mode greek letters in the arguments of commands that show up as pdf bookmarks.

1
  • 1
    it doesn't make sense to use font commands or the math shift in the pdf part of texorpdfstring. You should think in "unicode" there. Commented Nov 8, 2020 at 10:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .