如何计算并使用两个长度的比率?

如何计算并使用两个长度的比率?

我想在 Latex 中计算两个长度的比率,以便我可以按照该比率拉伸短划线。例如,在下面的代码中,我想用 newdashw/dashw 的比率替换 FakeStretch 的“0.7”值。

我怎样才能做到这一点?

\documentclass{article}
\usepackage{fontspec}
\setmainfont{TeX Gyre Schola}  % the font might change from doc to doc

\def\hairspace{\kern .08333em} % make a very thin space

\newlength\dashw
\settowidth\dashw{\normalsize--} % width of en dash in this font

\newlength\newdashw
\setlength\newdashw\dashw
\addtolength\newdashw{-0.16667em} % subtract 2*hairspace

% make a figure dash: want to replace "0.7" with "newdashw / dashw"
\newcommand\figdash{%
    \hairspace{\addfontfeature{FakeStretch=0.7}--}\hairspace}

\begin{document}
Jenny's number: 867\figdash 5309
\end{document}

答案1

您可以使用包提供的可扩展宏信特压裂

长度比

\documentclass{article}
\usepackage{fontspec}
%\setmainfont{TeX Gyre Schola}  % the font might change from doc to doc

% on my installation, the fonts in TL tree must be made known via file name to
% xetex 
\setmainfont[ExternalLocation]{texgyreschola-regular}

\def\hairspace{\kern .08333em } % make a very thin space

\newlength\dashw
\settowidth\dashw{\normalsize--} % width of en dash in this font

\newlength\newdashw
\setlength\newdashw\dashw
\addtolength\newdashw{-0.16667em} % subtract 2*hairspace

\usepackage{xintfrac}% macros for expandable computations 

% make a figure dash: 
\newcommand\figdash{%
    \hairspace
    {\addfontfeature{FakeStretch=\xintRound {4}{\newdashw/\dashw}}--}%
    \hairspace
}

% make a big figure dash: 
\newcommand\figbigdash{%
    \hairspace
    {\addfontfeature{FakeStretch=\xintRound {4}{3*\newdashw/\dashw}}--}%
    \hairspace
}

\begin{document}

FakeStretch=\xintRound {4}{\newdashw/\dashw}

Jenny's number: 867\figdash 5309

FakeStretch=\xintRound {4}{3*\newdashw/\dashw}

Jenny's number: 867\figbigdash 5309

\end{document}

答案2

另一个解决方案是使用我的包calculator:命令\LENGTHDIVIDE将两个长度相除并返回一个数字。例如,

\LENGTHDIVIDE{\newdashw}{\dashw}{\theRatio}

\documentclass{article}

   \usepackage{calculator} % Load calculator

\usepackage{fontspec}
\setmainfont{TeX Gyre Schola}  
\def\hairspace{\kern .08333em} 
\newlength\dashw
\settowidth\dashw{\normalsize--} 
\newlength\newdashw
\setlength\newdashw\dashw
\addtolength\newdashw{-0.16667em} 

    \LENGTHDIVIDE{\newdashw}{\dashw}{\theRatio} % \theRatio=\newdashw/\dashw

\newcommand\figdash{%
    \hairspace{\addfontfeature{FakeStretch=\theRatio}--}\hairspace} % Using \theRatio
\begin{document}
Jenny's number: 867\figdash 5309
\end{document}

相关内容