[Tex/LaTex] A special fraction macro with raised fraction bar

fontsfractionsmath-mode

I need to create a macro which allows me to get a fraction with a raised fraction bar. In a first approach, I mean to create two boxes (numerator and denominator), separated by a fixed horizontal rule that runs all the numerator and denominator width. However, actually I have not sufficient TeX notions to do this, thus I'm typesetting my documents with a simply \raisebox{.85pt}{$\displaystyle{\frac{#1}{#2}}$}.

different height of fracions

The picture above shows the problem: in this one, on the left there is the raised fraction, on the right the default fraction. As you can see, my trivial resolution does not solve the problem of the height of the whole fraction, which is too high.

I must tell you that this problem is due to the use of a non-standard TeX font: plus and minus symbols belong to mathematical pi fonts, and their mid-line does not coincide with the TeX primitive.

So, is there a way to fix the fraction height and raise only its bar?

Best Answer

In this revised solution, there is a 2pt gap above the bar and a 4 pt gap below it, as specified in the optional arguments to \stackon and \stackunder respectively. Obviously, those numbers can be tweaked (even to the point of being made negative) to deal with the specific characteristics of your font. If you want the division bar raised relative to the horizontal math axis, then the .5ex argument of \raisebox can be changed.

EDITED to load \ifthen package explicitly, since it is no longer done automatically by stackengine.

\documentclass{article}
\usepackage{stackengine}
\usepackage{ifthen}
\newlength\testwida
\newlength\testwidb
\newlength\mywidth
\newcommand\newfrac[2]{%
  \setlength\testwida{\widthof{$#1$}}%
  \setlength\testwidb{\widthof{$#2$}}%
  \ifthenelse{\lengthtest{\testwida>\testwidb}}%
    {\mywidth=\testwida}%
    {\mywidth=\testwidb}%
  \raisebox{.5ex}{%
    \stackunder[4pt]{\stackon[2pt]{\rule{\mywidth}{.4pt}}{$#1$}}{$#2$}}%
}

\parskip 1ex
\begin{document}

\(x^3 \, \newfrac{x + y^2}{\sqrt{xy}}\)

\end{document}

enter image description here

ORIGINAL SOLUTION:

If I am understanding your problem, your font is causing "unnatural" vertical spacing. If that is so, you can use the stackengine package to stack the items with any particular gap (above and below the crossline). In this example, I show the gap as the default (3pt), then as 1pt, and finally as 4pt. EDITED the answer to get the math axis correct.

\documentclass{article}
\usepackage[oldsyntax]{stackengine}
\usepackage{ifthen}
\newlength\testwida
\newlength\testwidb
\newlength\mywidth
\newcommand\newfrac[2]{%
  \setlength\testwida{\widthof{$#1$}}%
  \setlength\testwidb{\widthof{$#2$}}%
  \ifthenelse{\lengthtest{\testwida>\testwidb}}%
    {\mywidth=\testwida}%
    {\mywidth=\testwidb}%
  \raisebox{.5ex}{%
    \stackunder{\stackon{\rule{\mywidth}{.4pt}}{$#1$}}{$#2$}}%
}

\parskip 1ex
\begin{document}

\(x^3 \, \newfrac{x + y^2}{\sqrt{xy}}\)

\Sstackgap=1pt
\(x^3 \, \newfrac{x + y^2}{\sqrt{xy}}\)

\Sstackgap=4pt
\(x^3 \, \newfrac{x + y^2}{\sqrt{xy}}\)
\end{document}

enter image description here

[EDIT: The second MWE uses obsolete stackengine syntax for setting stackgap lengths (e.g., \Sstackgap=1ex), which prevented scalable lengths from scaling under a fontsize change. Version 2 of the package (submitted 7/11/13) remedies the problem with a small syntax change.]