latex3 / unicode-math

XeLaTeX/LuaLaTeX package for using unicode/OpenType maths fonts
http://ctan.org/pkg/unicode-math
LaTeX Project Public License v1.3c
246 stars 29 forks source link

Conditionals inside \setmathfont fail*, why? (*T&C apply) #639

Closed niruvt closed 2 months ago

niruvt commented 2 months ago

Description

Conditionals inside font-options work only when empty.

Add info or delete as appropriate:

Minimal example demonstrating the issue

\documentclass{article}
\usepackage{unicode-math}
\newif\ifmyexpext
\myexpexttrue
\def\myext{otf}
\setmathfont[%
  % \ifmyexpext
    Extension         = {.\myext}%
  % \fi
]{XITSMath-Regular}
\setmainfont[%
  \ifmyexpext
    Extension           = {.\myext}%
  \fi
]{KpRoman-Regular}

\begin{document}
$1$
\end{document}

Further details

The MWE fails if the conditionals are uncommented. It works in fontspec as demonstrated. Strangely they don't produce any errors if there is no content inside.

davidcarlisle commented 2 months ago

I'm not sure there is anything specific to unicode-math, it is a general feature of most key-value parsers that they do not expand while parsing the keyval list. you would see the same with \includegraphics for example.

You could use

\expanded{
\noexpand\setmainfont[%
  \ifmyexpext
    Extension           = {.\myext}%
  \fi
]{KpRoman-Regular}
}
niruvt commented 2 months ago

Oh, okay. I didn't know this. I started with fontspec and it worked there, so it set an unfair expectation :P Thanks for the solution. I will close this, but a side question: would it be safer to use \expanded for just \ifmyexpext and \fi? Can there be parts in this list which should not be expanded as a rule?

davidcarlisle commented 2 months ago

Can there be parts in this list which should not be expanded as a rule?

well of course that's the tricky part to answer. In general "it depends" which is why the generic parsers try to avoid expanding anything. But actually here since all (almost all?) the keys and their values end up being strings passed to the font machinery it's probably safe enough to expand everything.

niruvt commented 2 months ago

Okay thanks for the clarification, there are more keys too in the actual use-case, I showed a minimal example in this issue. I will better go with the safer method of expanding only the desired macros since this is going to be a public release.