我在 lualatex 中使用 unicode-math 和 Cambria Math 字体。与 Computer Modern math 相比,分数之间的垂直空间太大。有没有办法可以实现调整而无需修补\frac
?例如 fontspec 或 unicode-math 选项?
即我正在寻找一个选项来改变分数线周围的垂直空间。
编辑:添加了 xfrac 案例和 +frac、+numr/+dnom。
+frac 只是 +sups 所有数字。自定义 xfrac 仅在将参数括在内时在数学模式下有效,\text{19}
而不在\text{#1}
?! 中有效。
LuaTeX-0.70.2,Cambria 5.96
\documentclass{article}
\usepackage{fontspec}
\usepackage{xfrac}
\usepackage{unicode-math}
\setmathfont{Cambria Math}
\setmainfont{Cambria}
\newcommand{\otfrac}[2]{%
\frac%
{\raisebox{-.1em}{\scriptsize $#1$}}%
{\raisebox{.15em}{\scriptsize $#2$}}%
}
\newcommand{\ofrac}[2]{%
\text{%
{\addfontfeature{VerticalPosition=Numerator}#1}%
\divslash{}%
{\addfontfeature{VerticalPosition=Denominator}#2}
}%
}
\begin{document}
% tfracs look weird:
\noindent here is a first ygygyg line of text\\
bit too large: $\frac{19}{30}$. $\otfrac{19}{30}$ better?\\
here is anothÁÁÁÁÁÁ line of text\\[.5em]
%
% default sfrac looks weirder:
default sfrac text \sfrac{19}{30} math $\sfrac{19}{30}$\\
%
% customising helps some:
\DeclareInstance{xfrac}{default}{text}{
numerator-top-sep = -.1ex,
denominator-bot-sep = -.1ex,
slash-symbol=\divslash,
}
\DeclareCollectionInstance{plainmath}{xfrac}{mathdefault}{math}{
numerator-top-sep = -.2ex,
denominator-bot-sep = -.1ex,
scale-factor = 0.8333,
scale-relative = true,
slash-right-mkern = -0mu,
slash-left-mkern = -0mu,
slash-symbol=\text{\divslash}
}
\UseCollection{xfrac}{plainmath}
custom sfrac text \sfrac{19}{30} math $\sfrac{19}{30}$\\
%
% +frac feature just superscripts all digits
{\addfontfeature{Fractions=On} +frac 19/30, bad: 24. }
+numr/+dnom: \ofrac{19}{30} \\
%
% customize sfrac this way:
\DeclareInstance{xfrac}{default}{text}{
numerator-bot-sep = 0pt, denominator-bot-sep = 0pt,
scaling = false, slash-symbol=\divslash,
numerator-format={\addfontfeature{VerticalPosition=Numerator}#1},
denominator-format={\addfontfeature{VerticalPosition=Denominator}#1},
}
\ExplSyntaxOn
\DeclareCollectionInstance{plainmath}{xfrac}{mathdefault}{math}{
numerator-top-sep = \c_max_dim, numerator-bot-sep = 0pt,
denominator-bot-sep = 0pt, scaling = false,
slash-right-mkern = 0mu, slash-left-mkern = 0mu,
slash-symbol=\text{\divslash},
%------------------vvvv-- or no \text{}, doesn't matter.
numerator-format={\text{\addfontfeature{VerticalPosition=Numerator}#1}},
denominator-format={\text{\addfontfeature{VerticalPosition=Denominator}#1}},
}
\ExplSyntaxOff
\UseCollection{xfrac}{plainmath}
% nice
otf custom sfrac text \sfrac{19}{30}
% what?!
math $\sfrac{19}{30}, \sfrac{\text{19}}{\text{30}}$\\
\end{document}
无论如何,我不会使用\sfrac
数字以外的任何东西作为参数,因此为了保持一致性,我将使用它,但修改的方法\tfrac
仍然很有趣,或者是\tfrac = \otfrac
最好的方法?
答案1
分子和分母的位置由字体参数控制,该参数可以在 LuaLaTeX 中轻松修改:
\documentclass[pagesize=auto, version=last]{scrartcl}
\usepackage{fontspec}
\usepackage{unicode-math}
\usepackage{luacode}
\usepackage{luatexbase-mcb}
\setmathfont{Cambria Math}
\setmainfont{Cambria}
\begin{luacode*}
local function patch_cambria_frac(fontdata)
if fontdata.psname == "CambriaMath" then
local mc = fontdata.MathConstants
mc.FractionNumeratorShiftUp = 0.4 * mc.FractionNumeratorShiftUp
mc.FractionDenominatorShiftDown = 0.4 * mc.FractionDenominatorShiftDown
end
end
luatexbase.add_to_callback("luaotfload.patch_font", patch_cambria_frac, "cambria_frac")
\end{luacode*}
\begin{document}
$\frac{19}{30}$
\end{document}
答案2
也许这对你有帮助
\usepackage{amsmath} % load before unicode-math
\def\superfrac#1#2{\raisebox{.3ex}{\ensuremath{\genfrac{}{}{}2{#1}{#2}}}}
在文档中您可以将其用作\superfrac{19}{30}
。
垂直对齐 ( .3ex
) 可能需要调整。2
表示scriptstyle
。
托德莱曼创建了一些有趣的宏:改进了分数的字距调整?