Modifying fraction macro for in-line text and exponents

There's an amazing fraction macro here* for FontSpec with LuaLaTeX (*: that would be the second macro in that answer, called \unifrac, which I like better than the first "macro"/ using the frac feature), but it only works if the font you're using has OpenType features dnom and numr. The font that I use, MinionMath (used with unicode-math, which requires FontSpec), does not have these features for anything beyond numbers (many fonts don't), and even fonts that have these features for letters don't have it for Greek letters.

To be honest, that macro looks absolutely beautiful for many fractions that I use for in-line text and exponents, but I wish I could use it for things other than numbers. I'm aware of the \sfrac macro which can use all letters (as well as \nicefrac and \tfrac) but I like the look of the \unifrac macro much more.

Is there any way that I can modify \unifrac to behave like \sfrac in the sense that I can use it with non-number characters, but retain the look of \unifrac?

I'll provide a MWE here along with pictures:

\documentclass{article}
\usepackage{xfrac}
\usepackage{fontspec} % Using unicode-math instead doesn't seem to make much difference
\setmainfont{EB Garamond 12 Regular} % this font has dnom and numr features;
% XITS Math, for example, doesn't

\newcommand{\unifrac}[2]{\mbox{% making sure we don't get a line break
⁄% That slash is U+2044 FRACTION SLASH, which has special spacing
}}

\begin{document}

\unifrac{12}{14} \unifrac{31415}{27182} \unifrac{abc}{def} \unifrac{Foo!}{Bar?}
\unifrac{\#\$\%+/<>=}{?\@[]\textbackslash\_|\{\}§†} $\left(\frac{3x}{2y}\right)^{\unifrac{3}{2}}=\unifrac{\lambda}{2x}$\\ And this is \texttt{\sfrac:}\qquad \sfrac{12}{14} \sfrac{31415}{27182} \sfrac{abc}{def} \sfrac{Foo!}{Bar?} \sfrac{\#\$\%+/<>=}{?\@[]\textbackslash\_|\{\}§†}
$\left(\frac{3x}{2y}\right)^{\sfrac{3}{2}}=\sfrac{\lambda}{2x}$\\

\end{document}


For a font that has the dnom and numr features (for numbers), such as EB Garamond, this results in:

For a font that doesn't have these features, such as XITS Math, this results in:

• What do you want: for fonts without dnom and numr features to switch to the sfrac style, or to make fonts that don't have those features "get" those features? – jon Jan 29 '16 at 19:20
• @Jon basically I want \unifrac to work in the same way that \sfrac does; i.e. if \sfrac can handle fonts that don't have dnom and numr features, why can't \unifrac? – Alborz Jan 29 '16 at 19:22
• @Jon if I had to choose between the two options, I'd say the "make fonts that don't have those features "get" them" is closest to what I'm asking. I know fonts that don't have those features simply won't work properly, I'm just trying to see how \sfrac works around that to handle things like Greek letters etc, and see if we can apply that "work-around" to \unifrac but still make sure that it retains its look. – Alborz Jan 29 '16 at 19:42
• \unifrac fundamentally depends on a font having the features you name. What that means is the font actually has a set of designed glyphs for the numbers you are seeing with EB Garamond. If those glyphs don't exist in the font you choose to use, then the \unifrac command is "too dumb" to fix the problem. The xfrac package, in contrast, is very sophisticated and scales things as needed or as you set in the options. What you might want to define is a command \myunifrac that uses \unifrac when the font has the features you need, and \sfrac when it doesn't. – jon Jan 29 '16 at 20:08
• You can't make normal-style glyphs (such as the letters in what I'm typing right now) look like the "special glyphs" that a font may include (such as special letters designed to be written superscript or subscript). Scaling a "g" down will usually not look like the specially designed small-subscript "g". However, you can help your case by ensuring that the font uses "lining" numbers rather than "oldstyle" ones (if the font has both types! EB Garamond does not, AFAIK, which is why \sfrac looks really different from \unifrac in your example). – jon Jan 29 '16 at 20:55

OpenType support in TeX as of when I first wrote xfrac in early 2004 was basically non-existent - XeTeX hadn't even been released back then. Therefore, the package was never designed with that in mind. Besides, the fundamental purpose was to provide a way to write nice fractions for fonts that didn't include them.

The package could very likely be made OpenType aware by making a few changes as to how fonts are selected. However, you should also be able to define an instance for the font in question by using the numerator-format key to select the font. The following instance declaration will basically make \sfrac behave as you wish for your chosen variant of Garamond. This is very similar to the Janson example from the manual.

\DeclareInstance{xfrac}{EBGaramond12Regular(0)}{text}{
numerator-format = {\fontspec{EBGaramond12Regular}[RawFeature=+numr]#1},
scaling = false,
numerator-bot-sep = 0pt,
denominator-bot-sep = 0 pt,
}


Hope this helps, Morten

• This is good, but it doesn't work for math mode for some reason :/ if you retry the MWE everything works just as it should except for the in-line equation... – Alborz Feb 7 '16 at 1:04
• Recall that xfrac was an early experiment. In hindsight, I think I should have made different templates for math and text mode and let the document command make the choice. I'll have a look at it. – Morten Høgholm Feb 7 '16 at 16:43

Here I create \altfrac{}{}. It does not require fontspec, it obeys math mode, its font is larger than that of \sfrac, the denominator lies on the baseline, while the top of the numerator I tries to lay at the top of the normal text font. The numbers are presented in \footnotesize. The slash is a horizontally stretched / to give it more of the fractional slash appearance.

It's downside is that is does not work properly if the current fontsize is anything other than \normalsize.

\documentclass{article}
\usepackage{xfrac,graphicx}

\newcommand{\altfrac}[2]{\ifmmode\def\tmp{$}\else\def\tmp{}\fi\mbox{% {\footnotesize \raisebox{.24\ht\strutbox}{\tmp#1\tmp}}% \kern-2.2pt\scalebox{1.6}[1]{/}\kern-1.8pt%⁄% That slash is U+2044 FRACTION SLASH, which has special spacing {\footnotesize\tmp#2\tmp}% }} \begin{document} \parindent 0pt This is \texttt{altfrac:}\qquad \altfrac{12}{14} \altfrac{31415}{27182} \altfrac{abc}{def} \altfrac{Foo!}{Bar?} \altfrac{\#\$\%+/<>=}{?\@[]\textbackslash\_|\{\}§†}
$\left(\frac{3x}{2y}\right)^{\altfrac{3}{2}}=\altfrac{\lambda}{2x}$
\qquad And here is \altfrac{x}{y} versus $\altfrac{x}{y}$\\

$\left(\frac{3x}{2y}\right)^{\sfrac{3}{2}}=\sfrac{\lambda}{2x}$\\

• Nice use of \footnotesize, I think we could call these "only-partially-vulgar" fractions :) – Will Robertson Feb 5 '16 at 8:15