Adding a vertical line at the right end of the horizontal line in \frac
I measure the fraction in the current style, via \mathpalette
. Then I add a \vrule
that's as high as the math axis (\fontdimen22
of the current font in family 2) plus half of the default rule thickness (\fontdime8
of the current font in family 3), as deep as the fraction and with width the default rule thickness.
Some negative space is needed, which is -\nulldelimiterspace
, that I add back after the construction.
In order to leave some room, I added thin spaces on either side in the denominator.
\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}
The code below should get you the correct depth for the rule.
In the first version, I assumed a fraction rule width equal to 0.4pt (default rule width). Thanks to egreg's answer, I replaced this 0.4pt with a macro that expands to the \fontdimen8
of the appropriate math font (\textfont3
, \scriptfont3
or \scriptscriptfont3
, depending on the current math style).
The syntax is very easy: just use \strange
or \strangeii
instead of \frac
.
Note: I added the \Bigg
delimiters not because they look nice, but in order to show that the vertical rule is as deep as the fraction denominator, not as deep as the whole formula—which would be the case if one were to remove the outer set of braces in the replacement texts of \strange
and \strangeii
(these braces create a subformula that is as high and as deep as the fraction).
\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}
Zoom on the first expression that shows \strange
in \scriptstyle
and in \scriptscriptstyle
:
For fun
It is possible to push the DRY principle a bit further in the expl3
code. I didn't do it above, because I think it makes the code significantly harder to read; but in case you are interested, here you are:
\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
}
This can replace the definition of \michaelsmallrule
in the complete code sample given above.
Like this?
\documentclass{article}
\begin{document}
\[ \frac{A }{B\smash{\rule{0.4pt}{2.2ex}}} \quad\frac{A}{B}\]%
\end{document}