14

Please see below:

enter image description here

There is too little space between the horizontal line from \frac and the bar from \bar{h_1}. So, it is a little hard to read. Any alternative way to represent the upper bar? And, it is short too.

ADDED: This is inline mode. In the display math mode, it is quite better. But in the inline math mode, it looks pretty bad, even with \bar{h}_1.

3
  • 1
    Can you add a minimum working example? My version of this in frac has naturally wider spaces, as you want. i.e. \frac{\bar{p}_1}{\bar{p}_1 + \bar{h}_1}.
    – qubyte
    Nov 9, 2011 at 16:10
  • 1
    @MarkS.Everitt It was inline mode. Sorry I missed that explanation. See the added explanation.
    – Chang
    Nov 9, 2011 at 16:22
  • 5
    May be you could re-write your fraction as $\bar{p}_1/(\bar{p}_1 + \bar{h}_1)$ which would fit better with inline math. Nov 9, 2011 at 16:46

6 Answers 6

18

I think the problem here is that you are using inline math mode $..$ as opposed to display mode \[...\]. Here is the comparison of the output between the two:

enter image description here

with the second one producing better spacing. To obtain that you need to either use one of the following

\[\frac{\bar{p}_1}{\bar{p}_1 + \bar{h}_1}\]

$\displaystyle \frac{\bar{p}_1}{\bar{p}_1 + \bar{h}_1}$

$\dfrac{\bar{p}_1}{\bar{p}_1 + \bar{h}_1}$

Note that \dfrac requires that the amsmath package be loaded, either by including it explictly \usepackage{amsmath}, or as part some other package that already includes amsmath, such \usepackage{mathtools}.

Here is the MWE. Note that the center environment was only used to simplify the image capture.

\documentclass{article}
\usepackage{amsmath}

\begin{document}
\begin{center}
$\frac{\bar{p}_1}{\bar{p}_1 + \bar{h}_1}$
\end{center}

\[\frac{\bar{p}_1}{\bar{p}_1 + \bar{h}_1}\]

\begin{center}
$\displaystyle \frac{\bar{p}_1}{\bar{p}_1 + \bar{h}_1}$
\end{center}

\begin{center}
$\dfrac{\bar{p}_1}{\bar{p}_1 + \bar{h}_1}$
\end{center}
\end{document}

To make the bar wider you can use \overline as in:

$\dfrac{\overline{p}_1}{\overline{p}_1 + \overline{h}_1}$

or adapt the xoverline from The \bar and \overline commands with which you can adjust the spacing:

$\dfrac{\xoverline{p}_1}{\xoverline{p}_1 + \xoverline{h}_1}$

or specify you want it wider with:

$\dfrac{\xoverline[1.25]{p}_1}{\xoverline[1.25]{p}_1 + \xoverline[1.25]{h}_1}$

Here is a comparison between \bar{p}_1, \overline{p}_1, \xoverline[1.25]{p}_1}, and {\xoverline[1.50]{p}_1

enter image description here

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newsavebox\myboxA
\newsavebox\myboxB
\newlength\mylenA

\newcommand*\xoverline[2][1.00]{%
    \sbox{\myboxA}{$\m@th#2$}%
    \setbox\myboxB\null% Phantom box
    \ht\myboxB=\ht\myboxA%
    \dp\myboxB=\dp\myboxA%
    \wd\myboxB=#1\wd\myboxA% Scale phantom
    \sbox\myboxB{$\m@th\overline{\copy\myboxB}$}%  Overlined phantom
    \setlength\mylenA{\the\wd\myboxA}%   calc width diff
    \addtolength\mylenA{-\the\wd\myboxB}%
    \ifdim\wd\myboxB<\wd\myboxA%
       \rlap{\hskip 0.5\mylenA\usebox\myboxB}{\usebox\myboxA}%
    \else
        \hskip -0.5\mylenA\rlap{\usebox\myboxA}{\hskip 0.5\mylenA\usebox\myboxB}%
    \fi}
\makeatother


\begin{document}
$\dfrac{\bar{p}_1}{\bar{p}_1 + \bar{h}_1}$\quad
$\dfrac{\overline{p}_1}{\overline{p}_1 + \overline{h}_1}$\quad
$\dfrac{\xoverline[1.25]{p}_1}{\xoverline[1.25]{p}_1 + \xoverline[1.25]{h}_1}$
$\dfrac{\xoverline[1.50]{p}_1}{\xoverline[1.50]{p}_1 + \xoverline[1.50]{h}_1}$
\end{document}
5
  • Would there be any method or alternative to make the space wider, without using the displaystyle? I probably need to use another accent, like hat.
    – Chang
    Nov 9, 2011 at 16:25
  • 1
    See updated solution with \overline and \xoverline. Nov 9, 2011 at 16:40
  • 4
    I don't think that using \displaystyle of \dfrac in in-line math mode is a good option; it breaks the normal separation between lines. Nov 9, 2011 at 16:54
  • @PeterGrill: I don't think the problem is so much with the OP using inline math mode as with the fact that he/she should not be using a fraction style that's not suitable for inline math.
    – Mico
    Nov 9, 2011 at 17:01
  • This is a good answer. Very thorough. Nice.
    – Seamus
    Nov 9, 2011 at 17:05
16

It is possible to define your own fraction command. The amsmath package provides

\genfrac{<ldelim>}{<rdelim>}{<thickness>}{<mathmode>}{<num>}{<denom>}

This typesets the fraction using numerator <num> and denominator <denom> with a line thickness <thickness>. Left and right delimiters are possible by specifying <ldelim> and/or <rdelim>. The mathmode override is also possible via <mathmode> by specifying an integer value from 0-3, which select respectively \displaystyle, \textstyle, \scriptstyle, and \scriptscriptstyle.

In the minimal example below I've defined \myfrac[<gap>]{<num>}{<denom>} which raises/lowers <num>/<denom> by <gap> using \raisebox:

enter image description here

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\newcommand{\myfrac}[3][0pt]{\genfrac{}{}{}{}{\raisebox{#1}{$#2$}}{\raisebox{-#1}{$#3$}}}
\begin{document}
\centering
\verb!Text style! \par
$\frac{a}{b}\ \myfrac{a}{b}\ \myfrac[1pt]{a}{b}\ \myfrac[2pt]{a}{b}\ \myfrac[3pt]{a}{b}$

\bigskip

\verb!Display style! \par
\[\frac{a}{b}\ \myfrac{a}{b}\ \myfrac[1pt]{a}{b}\ \myfrac[2pt]{a}{b}\ \myfrac[3pt]{a}{b}\]
\end{document}​

Perhaps a modification of this might suit your needs here, or elsewhere. It would also be possible to use a better choice mechanism to scale the font appropriately. However, this answer just provides a proof-of-concept.

8
  • Nice addition! I always forget about \genfrac. Nov 9, 2011 at 16:52
  • @Werner Ooold post, but still useful. Is it possible to use \raisebox{} inside a math mode? It gives me an error, but I'm not sure that's the problem.
    – Andyc
    Sep 24, 2020 at 8:06
  • 1
    @Andyc: Yes, but you'll have to force math mode again inside, and possibly reduce the size of the style. That is, \raisebox{<len>}{$<style><math>$}, where <style> is \displaystyle, \textstyle, \scriptstyle or \scriptscriptstyle.
    – Werner
    Sep 24, 2020 at 20:20
  • @Werner I have a question. Why the distance from the midline to numerator and denominator doesn't look like the same?
    – juanuni
    Apr 3, 2021 at 1:43
  • @juanuni: It's to accommodate for ascenders in the denominator.
    – Werner
    Apr 3, 2021 at 2:46
8

When in inline math mode, and especially when diacritics (such as \bar{.}) are involved, it's almost always preferable to replace an expression such as

$\frac{\bar{p}_1}{\bar{p}_1+\bar{h}_1}$

with its "inline fraction" form,

$\bar{p}_1 / (\bar{p}_1+\bar{h}_1)$

See, e.g., the TeXbook, pp. 139f., for Knuth's "endorsement" of this form for inline math usage. The two expressions look like this (deliberately enlarged to make the problem with the inline-fraction method more easily visible):

enter image description here

Of course, if a math expression containing fractions is in "display" format (i.e., on a line by itself), there's usually no reason or need to use the "inline fraction" form.

2

Use $\bar{h}_1$. That should at least centre the bar above h, although probably not lower it.

2

Use \bar{h}_1 instead. The bar doesn't belong to the subscript, just to the h.

0

This is TikZ's way.

enter image description here

\documentclass{article}
\usepackage{tikz,amsmath}
\begin{document}

\begin{center}
\begin{tikzpicture}
\def\d{1.2} % length of fraction line
\draw 
(0,0)--+(0:\d)++(0:\d/2)
+(90:.2)  node{$\overline{p_1}$}
+(-90:.25) node{$\overline{p_1}+\overline{h_1}$};
\end{tikzpicture} 
\end{center}

\end{document}

You must log in to answer this question.

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