# A special fraction macro with raised fraction bar

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}}$}.

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?

• Hi Lorenzo, could you post the contents of a small compilable .tex file, starting with \documentclass and ending with \end{document}? Then I can paste it into my editor, reproduce your situation on my machine, and start thinking about a solution. – John Wickerson Jun 25 '13 at 10:26
• I'd raise or lower the plus and minus signs using \mathchoice, or set a different \fontdimen22. – egreg Jun 25 '13 at 10:40
• @JohnWickerson Here there is the file: \documentclass[11pt]{article} \usepackage{amsmath} \def\frazione#1#2{\raisebox{.85pt}{$\displaystyle{\frac{#1}{#2}}$}} \begin{document} $$\bigg(\frazione{a+b}{c-d}+\frac{a+b}{c-d}\bigg)$$ Here, raising the whole fraction on the left seems have no--sense, but using symbols from {\sl mathematical pi font\/} (which I can't load here because they belong to my local tree), I need to do this. \end{document} – Lorenzo Jun 25 '13 at 11:40
• @Lorenzo I'd say that you should change the parameter \fontdimen22 for the font you're using for the math symbols; however, without an example of your setup it's difficult to say how. – egreg Jun 25 '13 at 13:02
• @Lorenzo -- if i remember correctly, the math symbols in the mathematical pi font (at least the one i'm familiar with) are designed to sit on the baseline, not be vertically centered on the math axis. so it is unlikely that they can be aligned properly with the fraction bar, which is placed along the math axis. it would be better if you could find another suitable symbol font that is designed with math rather than text in mind. – barbara beeton Jun 25 '13 at 13:25

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}


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}


[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.]

• My problem does not consist in the gap between numerator (or denominator) and the bar, rather in the position of the bar. In your post, it seems be the default frac bar – Lorenzo Jun 25 '13 at 11:40
• @Lorenzo Changing the .5ex as the first argument to the raisebox will raise or lower the whole fraction. Increase the number to make it higher, decrease it to make it lower. Although, I would ask, if the gap is not the problem, then why didn't the \raisebox in your posted question accomplish what you needed? – Steven B. Segletes Jun 25 '13 at 11:43
• It solve partially the problem, because right now the whole fraction is aligned with the plus sign, but not with parenthesis. So, I'm trying to fix the positions of num. and den., and raise only the fraction bar – Lorenzo Jun 25 '13 at 11:51
• @Lorenzo Does my revised solution match your issue better? – Steven B. Segletes Jun 25 '13 at 11:53
• @Lorenzo Forget the train. "All aboard" the stackengine! – Steven B. Segletes Jun 25 '13 at 12:04

I think I've answer my question by modifying \fontdimen22, as suggested by egreg. This is what I done:

\DeclareFontShape{OMS}{cmsy}{m}{n}{%
<5><6><7><8><9><10>gen*cmsy%
<10.95><12><14.4><17.28><20.74><24.88>cmsy10%
}{\fontdimen22\font=1.25\fontdimen22\font}


so I adjust the position of fraction bar (and big delimiters). The picture below shows the result.

• I may also adjust the gap between numerator and fraction bar... – Lorenzo Jun 27 '13 at 11:10