11

When I use subscripts and superscript with \frac the numerator and denominator looks very tight,

\documentclass[a4paper,12pt]{article}
\usepackage{mathtools,amssymb}

\begin{document} [e^{-\frac{(x-\mu_2)^2}{2\sigma^2_2}}] [e^{-\dfrac{(x-\mu_2)^2}{2\sigma^2_2}}] \end{document}

enter image description here

So I tried using \dfrac but not the minus sign and fraction line are not aligned.

enter image description here

Anybody has a smart fix to this?

Sandy G
  • 42,558
zmkm
  • 379
  • 5
    please always provide a complete test file so people can see the issue and test answers. \dfrac in a superscript is probably excessive, you could set the whole superscript, including the - in \displaystyle rather than the default \scriptstyle or use \exp notion rather than e^{..} so the expression is not in a superscript – David Carlisle Mar 20 '22 at 19:51
  • 3
    My preference would be to move the minus sign to the numerator: \frac{-(...)}{...} – James K Mar 20 '22 at 20:02

3 Answers3

24

The cause of the misalignment is that the minus sign is in scriptstyle, but the fraction is being forced into displaystyle. Here are a few options:

\[e^{\displaystyle -\frac{(x-\mu_2)^2}{2\sigma^2_2}}\]

enter image description here

\[e^{\dfrac{-(x-\mu_2)^2}{2\sigma^2_2}}\]

enter image description here

\[e^{-(x-\mu_2)^2/2\sigma^2_2}\]

enter image description here

\[\exp\bigl(-(x-\mu_2)^2/2\sigma^2_2\bigr)\]

enter image description here

Sandy G
  • 42,558
  • Note that it's unclear whether the vertical alignment is correct in the last example, because the minus sign is altogether missing. – Ben Voigt Mar 23 '22 at 20:10
  • 1
    @BenVoigt, there is no issue with the alignment in the last expression since there is a common baseline and only one style. But I added the missing minus sign. – Sandy G Mar 23 '22 at 20:19
19

Let's consider five separate typographic approaches to displaying the expression at hand:

  • e^{-\dfrac{(...)}{...}} and e^{\dfrac{-(...)}{...}}

  • e^{-\tfrac{(...)}{...}} and e^{\tfrac{-(...)}{...}}

  • e^{-(x-\mu_2)^2/(2\sigma_2^2)} (inline-style fraction in exponent)

  • \exp(-\dfrac{...}{...}) and \exp(-\tfrac{...}{...})

  • \exp\bigl(-(x-\mu_2)^2/(2\sigma_2^2)\bigr) (inline-style fraction)

enter image description here

I think we may posit that the expressions in the first two rows look awful. (Ok, row 1 is truly awful, whereas row 2 is just plain awful.) If you must use e^{...} notation, then do please consider using the inline-fraction variant in row 3. That said, I'd really, really recommend that you consider adopting \exp(...) notation; cf. rows 4 and 5.

\documentclass{article} % or some other suitable document class
\usepackage{amsmath} % for 'gather*' env.
\begin{document}
\begin{gather*}
e^{-\dfrac{(x-\mu_2)^2}{2\sigma_2^2}} \qquad
e^{\dfrac{-(x-\mu_2)^2}{2\sigma_2^2}} \\[\jot]
e^{-\tfrac{(x-\mu_2)^2}{2\sigma_2^2}} \qquad
e^{\tfrac{-(x-\mu_2)^2}{2\sigma_2^2}} \\[\jot]
e^{-(x-\mu_2)^2/(2\sigma_2^2)}        \\[\jot]
\exp\biggl(-\frac{(x-\mu_2)^2}{2\sigma_2^2}\biggr) \qquad
\exp\Bigl(-\tfrac{(x-\mu_2)^2}{2\sigma_2^2}\Bigr) \\[\jot]
\exp\bigl(-(x-\mu_2)^2/(2\sigma_2^2)\bigr)
\end{gather*}
\end{document}
Mico
  • 506,678
16

If you want necessarily to use \dfrac with the minus sign you add \ooalign.

enter image description here

\documentclass[a4paper,12pt]{article}
\usepackage{mathtools,amssymb}
\begin{document}
\[e^{-\frac{(x-\mu_2)^2}{2\sigma^2_2}}\]
\[e^{\ooalign{$-$}\dfrac{(x-\mu_2)^2}{2\sigma^2_2}}\]
\end{document}

Addendum: Using the comment of @campa with \hbox you obtain the same result.

\documentclass[a4paper,12pt]{article}
\usepackage{mathtools,amssymb}
\begin{document}
\[e^{-\frac{(x-\mu_2)^2}{2\sigma^2_2}}\]
\[e^{\hbox{$-$}\dfrac{(x-\mu_2)^2}{2\sigma^2_2}}\]
\end{document}

enter image description here

Update 2022-04-14

Based on the discussions from this question, I propose to use \mbox instead of \hbox.

The main difference is that using \hbox in LaTeX can lead to unexpected results. So always use the latter, unless you know what you're doing with \hbox. -- egreg

\documentclass[a4paper,12pt]{article}
\usepackage{mathtools,amssymb}
\begin{document}
default: \[e^{-\frac{(x-\mu_2)^2}{2\sigma^2_2}}\]
hbox: \[e^{\hbox{$-$}\dfrac{(x-\mu_2)^2}{2\sigma^2_2}}\]
mbox: \[e^{\mbox{$-$}\dfrac{(x-\mu_2)^2}{2\sigma^2_2}}\]
\end{document}

enter image description here

Sebastiano
  • 54,118