3

I was checking the solution here

for make a small fraction. I get the fraction but I noticed that there is a variation in the vertical alignment in display mode and inline mode:

enter image description here

\documentclass[12pt]{memoir}

\usepackage{amsmath} \usepackage{graphicx} \usepackage{stackengine}

\newcommand{\smath}[1]{\text{\scalebox{0.95}{$#1$}}}

\newcommand{\myfrac}[3][0pt]{\belowbaseline[-.65\baselineskip]{\smath{\genfrac{}{}{}{3}{\raisebox{#1}{$#2$}}{\raisebox{-#1}{$#3$}}}}}

\begin{document} $\tanh\Bigl(\myfrac{\alpha}{2}\Bigr)$

\begin{align} \tanh\Bigl(\myfrac{\alpha}{2}\Bigr) \end{align}

\end{document}

As you can see in display mode the fraction is moved up a little bit. This doesn't happen with equation environment nor \[ \]. What am I doing wrong?

juanuni
  • 1,987
  • \frac works fine. Nor is \scalebox the culprit. This looks llike a bug in \genfrac (stackengine). – John Kormylo Apr 03 '21 at 15:08
  • Oh well, I was looking for a small frac, because that I defined another "frac". I add information. – juanuni Apr 03 '21 at 18:38
  • 1
    @JohnKormylo -- genfrac is defined in amsmath; I don't think it's redefined in stackengine. – barbara beeton Apr 03 '21 at 19:17
  • @juanuni -- align should give exactly the same result as equation. But Id be inclined to use\dfrac` instead of trying to define something else. – barbara beeton Apr 03 '21 at 19:24
  • @barbarabeeton well I was looking for another small fraction, just that. Maybe I shouldn't try with small fraction in display mode. – juanuni Apr 03 '21 at 19:26
  • There's also \tfrac ("t" for "text-sized"). See the amsmath user guide for examples (texdoc amsldoc). – barbara beeton Apr 03 '21 at 19:30
  • @barbarabeeton yeah, but in my case \tfracalters the space between lines in which it appears. – juanuni Apr 03 '21 at 19:36
  • Huh? \dfrac can sometimes push adjacent lines of text apart but the result of \tfrac is almost always small enough to fit within any reasonable baselines. And it certainly is with the particular numerator and denominator you show in your example. (But those are definitely smaller with \tfrac than what you show.) – barbara beeton Apr 03 '21 at 19:46
  • @barbarabeeton well in my case is needed use \Bigl and \Bigr for the parenthesis with \tfrac. In such a situation, the space between lines is altered, in my case. if I use \bigl and \bigr the fraction is bigger than parenthesis. – juanuni Apr 03 '21 at 22:10
  • I've just tested this again. \dfrac definitely needs the \Big parentheses, but \tfrac does not; the parentheses come just to the bottom of the denominator, and that is preferable to parentheses that are too large. (Long experience and familiarity with editorial practices of a math publisher.) And \dfrac by itself could cause the baselines to spread in text, depending on what is in the previous and following lines. I still recommend \tfrac. – barbara beeton Apr 03 '21 at 23:53
  • @barbarabeeton indeed \tfrac is a good option, but in my case with stix2 package, \tfrac is bigger than \bigl( and \bigr). Then, I switch b to B, but ocurrs the situation with space between lines. I would like to show the packages that I am using to find myself in that situation but this comes out of the query made. – juanuni Apr 04 '21 at 04:15
  • Sadly, I can't help with stix2. (It's frustrating.) That information should have been made clear in your question. – barbara beeton Apr 04 '21 at 14:47
  • Well, the situation in my question (vertical alignment) ocurrs with or without stix2 package. The problem with space between lines is another question. In any case, I appreciate your time and interest. – juanuni Apr 04 '21 at 17:10
  • @barbarabeeton The equation and amsmath environments do not always typeset the same way. This question is a prime example. The amsmath environments redefine the value of \baselineskip. Since this OP's definition makes use of that value, the results vary. – Steven B. Segletes Apr 08 '21 at 01:22

2 Answers2

1

While \scalebox{0.95}{...} is not a major problem \scalebox{0.5}{...} would be. When you scale an image, only one point is not moved, which for \scalebox turns out to be the left baseline.

In the following I move the baseline to the center, scale it, then move the baseline back again.

\documentclass[12pt]{memoir}

\usepackage{amsmath} \usepackage{graphicx} %\usepackage{stackengine}

\fboxsep=0pt

\newcommand{\smath}[1]{\raisebox{\dimexpr 0.5\ht\strutbox-0.5\dp\strutbox}% {\scalebox{0.5}{\raisebox{\dimexpr 0.5\dp\strutbox-0.5\ht\strutbox}{$#1$}}}}

\newcommand{\myfrac}[3][0pt]{\smath{\tfrac{#2}{#3}}}

\begin{document} $\tanh\Bigl(\myfrac{\alpha}{2}\Bigr) {\displaystyle \tanh\Bigl(\myfrac{\alpha}{2}\Bigr)}$

\begin{align} \tanh\Bigl(\myfrac{\alpha}{2}\Bigr) \end{align}

\end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • I appreciate your time to write an answer. Your suggestion, elegant by the way, doesn't go in the direction of my question.In your suggestion I was trying with \scalebox{0.75}{...} and without \Big for parenthesis. I saw the fraction and the parenthesis was aligned at the bottom of the line. I was looking for move up the fraction a little bit so the fraction and parenthesis was centered vertically (I hope write this adequately). Because that I used \belowbaseline[-.65\baselineskip]{{...} and I noticed that inline mode and with align environment the vertical shift isn't the same, and – juanuni Apr 06 '21 at 16:37
  • ... that's what I was trying to solve. I hope I have written well because English is not my native language. – juanuni Apr 06 '21 at 16:37
1

The problem is that the definition of \myfrac makes use of \baselineskip, whose value is reset in amsmath environments. The solution? Use \normalbaselineskip in the definition of \myfrac instead.

In fact, the default baselineskip for long stacks in the stackengine package is \normalbaselineskip, for precisely this reason.

\documentclass[12pt]{memoir}

\usepackage{amsmath} \usepackage{graphicx} \usepackage{stackengine}

\newcommand{\smath}[1]{\text{\scalebox{0.95}{$#1$}}}

\newcommand{\myfrac}[3][0pt]{\belowbaseline[-.65\baselineskip]{\smath{\genfrac{}{}{}{3}{\raisebox{#1}{$#2$}}{\raisebox{-#1}{$#3$}}}}} \begin{document} OK with non-amsmath [ \tanh\Bigl(\myfrac{\alpha}{2}\Bigr)---\the\baselineskip ] In vertical alignment error with amsmath \begin{align} \tanh\Bigl(\myfrac{\alpha}{2}\Bigr)---\the\baselineskip \end{align} Why? amsmath redefines \textbackslash baselineskip.\ Solution? Use \textbackslash normalbaselineskip \renewcommand{\myfrac}[3][0pt]{\belowbaseline[-.65\normalbaselineskip]{\smath{\genfrac{}{}{}{3}{\raisebox{#1}{$#2$}}{\raisebox{-#1}{$#3$}}}}}% \begin{align} \tanh\Bigl(\myfrac{\alpha}{2}\Bigr)---\the\normalbaselineskip \end{align} \end{document}

enter image description here

  • Good diagnosis. But in the third instance (presumably using \normalbaselineskip), the fraction line appears to actually cut through the top of the 2. (In all instances, I think it's too close, but assume that's subjective. But intersecting can't be correct.) However, the fraction line is definitely in the same vertical position as the math axis, which is what is wanted. – barbara beeton Apr 08 '21 at 01:45
  • @barbarabeeton That is how the OP defined it to begin with, to get a close fraction. I didn't pick up on that, but my reading of the question was that it was all about why the vertical shift occurred. I'll see if it is just font hinting, or actual – Steven B. Segletes Apr 08 '21 at 01:55
  • @barbarabeeton I have confirmed that your perception arises from the resolution of the image. Under zoom, all denominators lie at the same relative position to the fraction bar. – Steven B. Segletes Apr 08 '21 at 01:59
  • @StevenB.Segletes thanks a lot.For the fraction line a put \mathstrut inside each \raisebox just before $2$ and $3$, respectively. – juanuni Apr 18 '21 at 17:47