[Tex/LaTex] Adding a vertical line at the right end of the horizontal line in \frac

fractions

I'd like to use a mathematical notation that looks like a fraction \frac{A}{B}, with an additional vertical line that extends from the right end of the horizontal line down towards the base line of the denominator. Is there an easy way to achieve this in such a way that it scales correctly in different settings?

Best Answer

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}

zzz