4

I don't know whether this is really a good idea or not, but I'm wondering if it's possible to implement a version of \frac where the numerator and denominator can be aligned at an alignment tab (&).

There are many questions about fraction alignment on this site, with the most obvious solution in many cases being to use \hfill. But consider a fraction like this one:

enter image description here

The top one looks, to my eyes, a little messy, but using \hfill moves the numerator too far to the left. What I really want (I think) is to align them exactly at the open bracket. So I'm imagining an \alignedfrac command that would allow me to write

\[
\alignedfrac{q&(x\mid y)}{p_\mathrm{foo}&(w\mid x,y,z)}
\]

and have that align the numerator and denominator at the & characters.

I'm aware that this may or may not actually be a good choice, and that I'm probably solving a non-problem, but still, I'm curious whether it's possible to implement something like this.

Here's the MWE that produces the image above:

\documentclass{article}
\begin{document}

\noindent without any alignment:
\[
\frac{q(x\mid y)}{p_\mathrm{foo}(w\mid x,y,z)}
\]
using hfill:
\[
\frac{q(x\mid y)\hfill}{p_\mathrm{foo}(w\mid x,y,z)}
\]
\end{document}
3
  • I say that what these expressions really need is to have both instances of | replaced with \mid .
    – Mico
    Commented Apr 1, 2022 at 0:40
  • @Mico I've changed it. I go back and forth sometimes on whether \mid or | looks better in any given context.
    – N. Virgo
    Commented Apr 1, 2022 at 1:57
  • looking at the answers it might be better to align them at the solidus instead of the open parenthesis (though it's probably better still to leave them centred as per the default) - I'll have to play around with it in my document.
    – N. Virgo
    Commented Apr 1, 2022 at 12:48

2 Answers 2

5

Here I construct the left portion of the frac (\leftfrac) and the right portion (\rightfrac) independently, using \hfills to achieve the portional justifications. Then, I typeset both adjacently, with a negative \mkern between them.

Per Mico's suggestion, occurrences of | have been replaced with \mid, for better appearance.

EDITED to add \vphantom matching, which I believe will help things if the left and right portions are of different vertical extents.

\documentclass{article}
\begin{document}
\newcommand\alignedfrac[2]{%
  \leftfrac#1\\#2\relax\mkern-4.5mu\rightfrac#1\\#2\relax
}
\def\leftfrac#1&#2\\#3&#4\relax{%
  \frac{\hfill#1\vphantom{#2}}{\hfill#3\vphantom{#4}}}
\def\rightfrac#1&#2\\#3&#4\relax{%
  \frac{\vphantom{#1}#2\hfill}{\vphantom{#3}#4\hfill}}
\noindent without any alignment:
\[
\frac{q(x\mid y)}{p_\mathrm{foo}(w\mid x,y,z)}
\]
using hfill:
\[
\frac{q(x\mid y)\hfill}{p_\mathrm{foo}(w\mid x,y,z)}
\]
using alignedfrac
\[
\alignedfrac{q&(x\mid y)}{p_\mathrm{foo}&(w\mid x,y,z)}
\]

\end{document}

enter image description here

1
  • 1
    +1. Do also consider replacing both instances of | with \mid .
    – Mico
    Commented Apr 1, 2022 at 0:43
2

You can do it:

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\alignedfrac}[2]{%
  \begingroup
  \setbox0=\vbox{%
    \m@th
    \ialign{&$\displaystyle##$\cr#1\cr#2\cr}%
    \setbox2=\lastbox
    \hbox{%
      \unhbox2
      \unskip
      \setbox4=\lastbox
      \unskip
      \setbox6=\lastbox
      \global\dimen1=\wd6
      \global\dimen3=\wd4
    }
  }
  \frac{\af@make#1\@nil}{\af@make#2\@nil}
  \endgroup
}
\def\af@make#1&#2\@nil{%
  \makebox[\dimen1][r]{$\displaystyle#1$}%
  \makebox[\dimen3][l]{$\displaystyle#2$}%
}
\makeatother

\begin{document}

\begin{gather*}
\alignedfrac{q&(x\mid y)}{p_\mathrm{foo}&(w\mid x,y,z)} \\
\frac{q(x\mid y)}{p_\mathrm{foo}(w\mid x,y,z)}
\end{gather*}

\end{document}

What's the deal? I make an alignment with the primitive \halign and then examine its last row by dismantling the built box. I store the dimension of the two parts and use them to make boxes of the right width.

However, I see no improvement with respect to the standard fraction, as the image clearly shows.

enter image description here

You must log in to answer this question.

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