我想使用一个看起来像分数 \frac{A}{B} 的数学符号,其中有一条额外的垂直线,从水平线的右端向下延伸到分母的基线。有没有一种简单的方法可以实现这一点,使其在不同设置下正确缩放?
答案1
我通过 测量当前样式中的分数\mathpalette
。然后我添加一个 ,\vrule
其高度与数学轴(\fontdimen22
系列 2 中的当前字体)加上默认规则厚度的一半(\fontdime8
系列 3 中的当前字体)一样高,深度与分数一样深,宽度为默认规则厚度。
需要一些负空间,也就是-\nulldelimiterspace
,我在构造之后将其添加回来。
为了留出一些空间,我在分母的两边都加了一些细小的空间。
\documentclass{article}
\usepackage{amsmath}
\usepackage{unicode-math} % comment out for pdflatex
\makeatletter
\newcommand{\funnyfrac}[2]{{\mathpalette\funny@frac{{#1}{#2}}}}
\newcommand{\funny@frac}[2]{\funny@@frac#1#2}% trick for passing three args to \mathpalette
\newcommand{\funny@@frac}[3]{%
\begingroup
\sbox\z@{$\m@th#1\frac{#2}{\,#3\,}$}%
\usebox\z@
\kern-\nulldelimiterspace
\funny@fractionrule{#1}%
\kern\nulldelimiterspace
\endgroup
}
\newcommand{\math@param}[3]{%
\fontdimen#3
\ifx#1\displaystyle\textfont#2
\else\ifx#1\textstyle\textfont#2
\else\ifx#1\scriptstyle\scriptfont#2
\else\scriptscriptfont#2 \fi\fi\fi
}
\@ifpackageloaded{unicode-math}{%
\usepackage{ifluatex}
\ifluatex
\newcommand{\funny@fractionrule}[1]{%
\vrule height \dimexpr\Umathaxis#1+0.5\Umathfractionrule#1\relax
depth \dp\z@
width \Umathfractionrule#1\relax
}
\else
\usepackage{xfp}
\newcommand{\funny@fractionrule}[1]{%
\sbox\tw@{$\textstyle x$}\sbox\@tempboxa{$#1x$}%
\vrule height \fpeval{\ht\@tempboxa/\ht\tw@}%
\dimexpr\fontdimen22\textfont2+0.5\fontdimen8\textfont3\relax
depth \dp\z@
width \fpeval{\ht\@tempboxa/\ht\tw@}\fontdimen8\textfont3
}
\fi
}{% no unicode-math
\newcommand{\funny@fractionrule}[1]{%
\vrule height \dimexpr\math@param{#1}{2}{22}+0.5\math@param{#1}{3}{8}\relax
depth \dp\z@
width \math@param{#1}{3}{8} % default rule width
}
}
\makeatother
\begin{document}
$\displaystyle\funnyfrac{a}{b}$
$\textstyle\funnyfrac{a}{b}$
$\scriptstyle\funnyfrac{a}{b}$
$\scriptscriptstyle\funnyfrac{a}{b}$
$\displaystyle\funnyfrac{a}{\dfrac{1}{2}}$
$\funnyfrac{a+b}{c}+\funnyfrac{a}{b+c}$
\end{document}
答案2
下面的代码应该可以为您提供规则的正确深度。
在第一个版本中,我假设分数规则宽度等于 0.4pt(默认规则宽度)。感谢egreg 的回答,我用一个扩展为\fontdimen8
适当数学字体的宏(\textfont3
,\scriptfont3
或\scriptscriptfont3
,取决于当前的数学样式)替换了这个 0.4pt。
语法非常简单:只需使用\strange
或\strangeii
代替\frac
。
注意:我添加\Bigg
分隔符并不是因为它们看起来不错,而是为了显示垂直线与分数分母一样深,而不是与整个公式一样深——如果删除替换文本和中的外括号,就会出现这种情况\strange
(\strangeii
这些括号会创建一个子公式与分数一样高、一样深)。
\documentclass{article}
\usepackage{xparse}
\makeatletter
% Small amount of expl3 code for the DRY principle
\ExplSyntaxOn
\cs_new_protected:Npn \__michael_mathchoice:nnnn #1#2#3#4
{ \mathchoice {#1} {#2} {#3} {#4} }
\cs_generate_variant:Nn \__michael_mathchoice:nnnn { oooo }
% Expand to a \fontdimen parameter of a math font
%
% #1: 'text', 'script' or 'scriptscript' (for \textfont, \scriptfont or
% \scriptscriptfont, respectively)
% #2: font family (0-16)
% #3: number of the font parameter (cf. TeXbook p. 447)
\cs_new:Npn \__michael_math_param:nnn #1#2#3
{ \fontdimen #3 \use:c { #1font } #2 }
% Default rule width \__michael_math_param:nnn {#1} {3} {8} based on a hint
% from egreg's answer (<https://tex.stackexchange.com/a/499972/73317>).
\cs_new_protected:Npn \__michael_small_rule:n #1
{
\vrule
width \dim_eval:n { \__michael_math_param:nnn {#1} {3} {8} }
height \dim_eval:n { \__michael_math_param:nnn {#1} {2} {22} +
0.5\__michael_math_param:nnn {#1} {3} {8} }
\relax
}
% Generate the vertical rule to be used on the right of the fraction
% in the appropriate style.
\NewDocumentCommand \michaelsmallrule { }
{
\__michael_mathchoice:oooo
{ \__michael_small_rule:n { text } }
{ \__michael_small_rule:n { text } }
{ \__michael_small_rule:n { script } }
{ \__michael_small_rule:n { scriptscript } }
}
\ExplSyntaxOff
\newcommand*{\strangeii}[2]{% No need for the % here: we are in math mode
{ \frac{#1}{#2} \michaelsmallrule }
}
% See <https://tex.stackexchange.com/a/233033/73317> and TeXbook p. 150
\newcommand*{\strange}[2]{% with correction for horizontal space
{ \frac{#1}{#2} \kern-\nulldelimiterspace \michaelsmallrule }
}
\newcommand*{\mytest}[1]{%
\par
In text style: $\Biggl(2 + #1{x+1}{(2y-8)^2} \Biggr)$; in subscripts and
subsubscripts:
$\Gamma_{ #1{a+1}{1+#1{x}{y}^{#1{\alpha}{\beta}}} }$; finally, in a display formula:
\[ \Biggl( 2 + #1{x+1}{(2y-8)^2} \Biggr). \]
}
\makeatother
\begin{document}
\section*{With \texttt{\string\strange}}
\mytest{\strange}
\section*{With \texttt{\string\strangeii}}
\mytest{\strangeii}
\end{document}
放大\strange
在\scriptstyle
和中显示的第一个表达式\scriptscriptstyle
:
为了娱乐
可以推动DRY 原则代码中还有一点expl3
。我上面没有这样做,因为我认为它会让代码更难阅读;但如果你感兴趣的话,你可以看看:
\cs_new_protected:Npn \__michael_small_rule_aux:n #1
{ \__michael_mathchoice:oooo #1 }
\cs_generate_variant:Nn \__michael_small_rule_aux:n { V }
% Generate the little vertical rule from middle to top of the fraction rule
% in the appropriate style.
\NewDocumentCommand \michaelsmallrule { }
{
% Prepare the argument list for the four styles; store it in \l_tmpa_tl.
\tl_clear:N \l_tmpa_tl
\clist_map_inline:nn { text, text, script, scriptscript }
{
\tl_put_right:Nn \l_tmpa_tl { { \__michael_small_rule:n {##1} } }
}
% Expand \l_tmpa_tl to construct the four arguments (they correspond to
% the arguments of \mathchoice, except that one expansion step will be
% done in each argument before it is fed to \matchoice)
\__michael_small_rule_aux:V \l_tmpa_tl
}
\michaelsmallrule
这可以替换上面给出的完整代码示例中的定义。
答案3
答案4
只是为了好玩。
\documentclass{article}
\usepackage{tikz,amsmath}
\newcommand{\mytikzmark}[2]{%
\tikz[remember picture,inner sep=-1pt,outer sep=0pt,baseline,anchor=base]
\node (#1) {\ensuremath{#2}};}
\begin{document}
\[ q
= \mytikzmark{n}{\dfrac{{Q_l}}{2}}
\]
\begin{tikzpicture}[overlay,remember picture]
\draw (n.-10) --++(-90:0.35);
\end{tikzpicture}
\end{document}