8

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?

0

5 Answers 5

11

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

8
  • 1
    Thanks, this is also very nice! When using unicode-math and LuaLaTeX it gives unwanted results in subscripts (as does frougon's solution). But that's not such a big problem, since I doubt I'll use it in subscripts.
    – Michael
    Jul 14, 2019 at 21:19
  • 2
    @Michael I added the code for LuaLaTeX.
    – egreg
    Jul 14, 2019 at 22:13
  • 1
    @Michael Now the code is for all three engines; xfp is needed for xelatex.
    – egreg
    Jul 14, 2019 at 22:40
  • @egreg Pfiouuuuu, impressive. I see you drastically changed the horizontal spacing in recent edits. I need to wrap the \usepackage{unicode-math} inside \ifluatex ... \fi in order to compile your code with pdfTeX (maybe this is incorrect for XeTeX, though). The error message is: unicode-math Error: Cannot be run with pdfLaTeX! (unicode-math) Use XeLaTeX or LuaLaTeX instead.
    – frougon
    Jul 14, 2019 at 22:46
  • 1
    @frougon Of course: you can't have \usepackage{unicode-math} if you run pdflatex. I added a comment for that.
    – egreg
    Jul 14, 2019 at 22:56
6

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}

Screenshot

Zoom on the first expression that shows \strange in \scriptstyle and in \scriptscriptstyle:

Zoomed in

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.

6
  • Thanks a lot. This looks good. I haven't been able to get your last edit working, but with the previous edit using LuaLaTeX and unicode-math, the vertical line extended above the horizontal line in subscript style. Any idea why, or how to fix it? Unfortunately I would need a lot more background to understand what your code does.
    – Michael
    Jul 14, 2019 at 20:25
  • The last edit so far is only a minor text change, what do you mean? Can you post minimal code (e.g., on pastebin.com, etc.)? The code I posted works fine with LuaTeX here (lualuatex executable). Do you mean the “For fun” section, maybe?
    – frougon
    Jul 14, 2019 at 20:31
  • Here is a MWE to illustrate the problem with LuaLaTeX and unicode-math. It does not yet use the for fun edit. pastebin.com/UEj2J3Mh
    – Michael
    Jul 14, 2019 at 20:48
  • But it's not such a big problem, since I doubt that I'll use the notation in subscripts, so thanks again for the effort!
    – Michael
    Jul 14, 2019 at 20:52
  • 1
    Thanks for the example. So, with unicode-math, \fontdimen22 of the appropriate math font doesn't seem to give the axis height contrary to what the TeXbook p. 447 seems to imply (also Heiko Oberdiek's answer). I wonder if it is an unicode-math bug... In case you didn't notice, I found that the 1.2pt is simply \nulldelimiterspace (and always the same unless you change it yourself). The “For fun” section is just a different programming style (smarter but less readable!), the behavior will be exactly the same.
    – frougon
    Jul 14, 2019 at 21:12
5

Like this?

\documentclass{article}

\begin{document}

\[ \frac{A }{B\smash{\rule{0.4pt}{2.2ex}}} \quad\frac{A}{B}\]%

\end{document} 

enter image description here

2
  • Yes, thanks! Unfortunately, in inline math mode the vertical bar extends above the horizontal one. Similarly in subscript style. (I'm using LuaLaTeX with unicode-math)
    – Michael
    Jul 14, 2019 at 20:20
  • The vertical line height a problem of trial and error. I guess the ex unit is the global ex of the document, not of the fraction. You could use \mathpalette to ensure it works also in indices, subindices, &c.
    – Bernard
    Jul 14, 2019 at 20:26
3

My proposal is to change the holtpolt.sty and adapt for the fraction without left vertical bar, as the image in attachment:

The image is:

enter image description here

and the MWE:

\documentclass{article}
\usepackage{amsmath}
\newcommand{\holter}[2]{\begin{array}{c}\multicolumn{1}{c}{#1}\\\hline
\multicolumn{1}{c|}{#2}\end{array}}
\begin{document}
$\holter{A}{B}$
\end{document}
2
2

Only for fun.

\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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .