各种字体都有一些内置分数和/或使用 +frac 字体功能构造分数的能力。它们往往看起来很棒,但太小而难以阅读。
因此,我尝试旋转自己的分数宏,发现我需要围绕斜线进行字距调整。我可以测试整个分子或分母的值,但我真的想测试分子的最后一个字符和分母的第一个字符。
我该怎么做?我的方法如下,但仅适用于个位数的分子和分母。
\documentclass{article}
\usepackage{fontspec}
%\setmainfont{STIXTwoText-Regular}
%\newfontfamily\fracfigs{STIXTwoText-Medium}[% compensate weight for scaling
% Numbers = {Proportional,Lining},
% Scale = 0.75]
% Actual kerns wrong for this font, but everyone will have it:
\setmainfont{Latin Modern Roman}
\newfontfamily\fracfigs{Latin Modern Roman}[Scale = 0.75]
\newcommand*\myFii{2}
\newcommand*\myFiv{4}
\newcommand*\myFvi{6}
\newcommand*\myFvii{7}
\newcommand*\myFix{9}
\newcommand*\myfrac[2]{%
\def\myfractmpn{#1}% Really want just last char
\def\myfractmpd{#2}% Really want just first char
\raisebox{0.5ex}{{\fracfigs#1}}%
\ifx\myfractmpn\myFii \kern 0.1em%
\else\ifx\myfractmpn\myFvii \kern -0.05em%
\else\ifx\myfractmpn\myFix \kern 0pt%
\else \kern 0.05em%
\fi\fi\fi%
\textfractionsolidus%
\ifx\myfractmpd\myFiv \kern -0.05em%
\else\ifx\myfractmpd\myFvi \kern 0pt%
\else \kern 0.05em%
\fi\fi%
{\fracfigs#2}}
\begin{document}
\myfrac{1}{1}\quad \myfrac{11}{11}\par
\myfrac{2}{2}\quad \myfrac{22}{22}\par
\myfrac{3}{3}\quad \myfrac{33}{33}\par
\myfrac{4}{4}\quad \myfrac{44}{44}\par
\myfrac{5}{5}\quad \myfrac{55}{55}\par
\myfrac{6}{6}\quad \myfrac{66}{66}\par
\myfrac{7}{7}\quad \myfrac{77}{77}\par
\myfrac{8}{8}\quad \myfrac{88}{88}\par
\myfrac{9}{9}\quad \myfrac{99}{99}\par
\end{document}
答案1
只需引入可扩展的宏来获取字符串的第一位或最后一位数字。
\documentclass{article}
\usepackage{fontspec}
%\setmainfont{STIXTwoText-Regular}
%\newfontfamily\fracfigs{STIXTwoText-Medium}[% compensate weight for scaling
% Numbers = {Proportional,Lining},
% Scale = 0.75]
% Actual kerns wrong for this font, but everyone will have it:
\setmainfont{Latin Modern Roman}
\newfontfamily\fracfigs{Latin Modern Roman}[Scale = 0.75]
\def\getfirst#1#2\relax{#1}
\def\getlast#1#2\relax{\if\relax#2\relax#1\else\getlast#2\relax\fi}
\newcommand*\myFii{2}
\newcommand*\myFiv{4}
\newcommand*\myFvi{6}
\newcommand*\myFvii{7}
\newcommand*\myFix{9}
\newcommand*\myfrac[2]{%
\edef\myfractmpn{\getlast#1\relax}% Really want just last char
\edef\myfractmpd{\getfirst#2\relax}% Really want just first char
\raisebox{0.5ex}{{\fracfigs#1}}%
\ifx\myfractmpn\myFii \kern 0.1em%
\else\ifx\myfractmpn\myFvii \kern -0.05em%
\else\ifx\myfractmpn\myFix \kern 0pt%
\else \kern 0.05em%
\fi\fi\fi%
\textfractionsolidus%
\ifx\myfractmpd\myFiv \kern -0.05em%
\else\ifx\myfractmpd\myFvi \kern 0pt%
\else \kern 0.05em%
\fi\fi%
{\fracfigs#2}}
\begin{document}
\myfrac{1}{1}\quad \myfrac{11}{11}\par
\myfrac{2}{2}\quad \myfrac{22}{22}\par
\myfrac{3}{3}\quad \myfrac{33}{33}\par
\myfrac{4}{4}\quad \myfrac{44}{44}\par
\myfrac{5}{5}\quad \myfrac{55}{55}\par
\myfrac{6}{6}\quad \myfrac{66}{66}\par
\myfrac{7}{7}\quad \myfrac{77}{77}\par
\myfrac{8}{8}\quad \myfrac{88}{88}\par
\myfrac{9}{9}\quad \myfrac{99}{99}\par
\end{document}
答案2
使用expl3
;\tl_item:nn { #1 } { -1 }
我们提取参数中的最后一项和\tl_item:nn { #2 } { 1 }
第一项。
如何填写其他数字的字距应该很清楚。请注意,在第二次调用中,F
“no match”参数用于插入默认字距 0.05em。
\documentclass{article}
\usepackage{fontspec}
\setmainfont{STIX Two Text}
\newfontface{\fracfigs}{STIXTwoText-Medium}[% compensate weight for scaling
Extension=.otf,
Numbers = Proportional,
Scale = 0.75,
]
\ExplSyntaxOn
\NewDocumentCommand{\myfrac}{mm}
{
\dedded_myfrac:nn { #1 } { #2 }
}
\cs_new_protected:Nn \dedded_myfrac:nn
{
\raisebox{0.5ex}{\fracfigs #1}
\str_case_e:nn { \tl_item:nn { #1 } { -1 } }
{
{2}{\kern 0.1em}
{7}{\kern -0.05em}
{9}{\kern 0.05em}
}
\textfractionsolidus
\str_case_e:nnF { \tl_item:nn { #2 } { 1 } }
{
{4}{\kern 0.05em}
{6}{}
}
{\kern 0.05em}
{\fracfigs#2}
}
\ExplSyntaxOff
\begin{document}
\myfrac{1}{1}\quad \myfrac{11}{11}\par
\myfrac{2}{2}\quad \myfrac{22}{22}\par
\myfrac{3}{3}\quad \myfrac{33}{33}\par
\myfrac{4}{4}\quad \myfrac{44}{44}\par
\myfrac{5}{5}\quad \myfrac{55}{55}\par
\myfrac{6}{6}\quad \myfrac{66}{66}\par
\myfrac{7}{7}\quad \myfrac{77}{77}\par
\myfrac{8}{8}\quad \myfrac{88}{88}\par
\myfrac{9}{9}\quad \myfrac{99}{99}\par
\end{document}
不管怎样,这样的分数都是丑陋的。
是什么\str_case_e:nnTF
?_e
部分表示参数在被“字符串化”之前要进行完全扩展,因此\tl_item:nn
在检查字符串之前会进行评估。 的第一个参数\str_case_e:nnTF
是要测试的字符串;第二个参数应采用以下形式
{<str-1>}{<code for case-1>}
{<str-2>}{<code for case-2>}
...
{<str-n>}{<code for case-n>}
如果匹配,则插入相应的代码,然后插入第三个参数( )中的代码;如果不匹配,则插入T
第四个参数( )的代码。F
然而,一个非常方便的expl3
惯例是T
和F
参数是“特殊的”,所以我们实际上有四个完整的函数,即
\str_case_e:nn \str_case_e:nnF \str_case_e:nnT \str_case_e:nnTF
最有用的是前两个