7

In order to use Unicode maths in pdflatex as well as in LuaTeX, in preamble of my document I have:

\usepackage{ifxetex,ifluatex}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
  \usepackage{newunicodechar}
            \newunicodechar{ρ}{\rho}
            \newunicodechar{∈}{\in}
            \newunicodechar{μ}{\mu}
            \newunicodechar{≠}{\neq}
% ... and more of these \newunicodechar
%
\else % if luatex or xetex
  \usepackage{unicode-math}
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi

This allows one to have an arxiv.org-submittable document (arxiv.org does not do LuaTeX).

However, I wonder whether there is actually a package I can use instead of manually writing out all these \newunicodechar{...}{...}, as it looks pretty standard to me.

12
  • you are not doing unicode math with these definitions, only a bit of utf8 math input. And typically it only makes the input slower, I would have no idea how to type ∈, using \in is imho much faster. Side remark: in current latex neither textcomp nor inputenc is needed. Jun 16, 2021 at 21:09
  • I can think of several reasons. (A) It takes time to include those definitions. (B) Unicode characters are not 1-1 to latex commands, especially \mathscr and \mathcal mean the same font in Unicode, and emojic things like red heart are just begging for troubles. (C) I sometimes want some flexibilities; for instance \def†#1†{\text{#1}} and \def…{,\allowbreak\dotsc,\allowbreak}. So a good interface is all I need.
    – Symbol 1
    Jun 16, 2021 at 21:11
  • note also ifxetex and ifluatex are legacy packages that both just input iftex package you can use \iftutex which is true in xetex and luatex rather than teh nested \ifnum game. Jun 16, 2021 at 21:24
  • I really wouldn't do this (I'd use the command forms when using unicode-math package with luatex as well) but if you do want this you can input unicode-math package's unicode-math-table.tex with a suitable definition of \UnicodeMathSymbol which defines the definitions in the "reverse" definition. Jun 16, 2021 at 21:28
  • 2
    @UlrikeFischer - editors such as VSCode would automatically translate \in to ∈, along with a whole host of other chars (if a pretty standard plugin is used). Jun 16, 2021 at 21:36

1 Answer 1

9

I would not do this. The source is far clearer if you use the command forms (even if using unicode-math with LuaTeX).

However, you can input unicode-math's unicode-math-table.tex, inverting its normal definition so that it defines the character to expand to the command rather than the other way around.

It is unrelated to the main issue, but most of the autogenerated preamble shown was unnecessarily complicated or redundant. pdfTeX defaults to UTF-8, so inputenc isn't needed. TeX ligatures are the default, so it doesn't need to be specified. ifxetex and ifluatex are legacy packages that now do the same thing and both just load iftex.

Enter image description here

\documentclass{article}

\usepackage{iftex,amsmath}

\iftutex
\usepackage{unicode-math}
\else
\usepackage{amssymb}
\def\z"{}
\def\UnicodeMathSymbol#1#2#3#4{%
 \ifnum#1>"A0
   \DeclareUnicodeCharacter{\z#1}{#2}%
  \fi}
\input{unicode-math-table}
\let\muprho\rho
\def\BbbR{\mathbb{R}}
\fi
\begin{document}


$ρ ∈ ℝ$

\end{document}

Note that even this small example highlights some issues with using the characters. I took rho from your question, but you used U+03C1 which is the standard ρ which unicode-math sets up as \muprho (math upright rho), the math italic rho being U+1D70C 𝜌. I assumed you didn't want the distinction in pdfTeX and added \let\muprho\rho and left it to the pdflatex math font setup to decide if that is italic.

So you may find that you still need to add several definitions, but at least inputting unicode-math-table defines the bulk of them and any remaining definitions, as here, can be written just using TeX command forms, not needing to refer to code points.

7
  • It would also be possible to load an upright math font in OML encoding as \mathrm and define \muprho as \mathrm{\rho}. Alternatively, you could load a LGR font for Greek with mathastext. This is more-compatible with what h ow \muprho works in math mode. (You might or might not need \muprho to also work the same way in text mode.) You could also define \symup as \mathrm. Finally, you could use \uprho, which works in both unicode-math and many legacy packages.
    – Davislor
    Jun 16, 2021 at 23:11
  • 1
    @Davislor you are too used to luatex \mathrm has no effect on \rho :-) Yes I could have forced upright but I doubt that was what was wanted, upright lowercase Greek is essentially never used in math, Jun 17, 2021 at 6:49
  • @Davislor oh sorry you were suggesting changing the normal definition of \rho so it was \mathalpha and so \mathrm worked yes I suppose so:-) Jun 17, 2021 at 7:47
  • @DavidCarlisle Links to github.com/josephwright/siunitx/issues/435 :)
    – Joseph Wright
    Jun 17, 2021 at 9:54
  • 2
    @DimaPasechnik yes if you look at unicode-math-table.tex you will see that it just consists of thousands of lines each with a single call to \UnicodeMathSymbol unicode-math uses this to define the commands so that the line \UnicodeMathSymbol{"02208}{\in }{\mathrel}{set membership, variant}% defines \in for that package as the unicode character U+2208, but here you want to define the input U+2208 to the already defined \in command. So just define \UnicodeMathSymbol to do that and ignore its other arguments. Jun 17, 2021 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.