TransWikia.com

Improper alphabetic constant error in title with greek: bug with unicode-math and hyperref in LuaLaTeX

TeX - LaTeX Asked by sergiokapone on March 24, 2021

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

2 Answers

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

Correct answer by Ulrike Fischer on March 24, 2021

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.

Answered by Mico on March 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP