5

I have heavy fractions in my LaTeX equations.

When there are 2 or more consecutive fractions in a row (i.e. I evaluate a product), they are too close to each other.

Is there any way to add an extra space between fractions in LaTeX mathmode?

To be more precise, I have the following function containing a big fraction:

\newcommand{\myfrac}[1]{
    \dfrac{\int\limits_0^{#1} x^2 \, \mathrm{d}x}{\zeta(#1)}
}

I am tired of adding an extra space between them: $ \myfrac{1} \, \myfrac{2} $ instead of $ \myfrac{1} \myfrac{2} $.

I thought of adding an invisible symbol \i (in the beginning and end of my custom function) such that \i is ignored and \i\i works as \,.

Another approach is to tune spacing around any fractions, but I failed to find anything related to user-defined spaces between concrete objects.

Here is my code:

\newcommand{\myfrac}[1]{
    \dfrac{\int\limits_0^{#1} x^2 \, \mathrm{d}x}{\zeta(#1)}
}

\begin{document}

Here everything works good:
\begin{equation*}
    a = 4 + \myfrac{1} < 5.
\end{equation*}

However, here the space between the fractions is too small:
\begin{equation*}
    b = 7 + \myfrac{1} \myfrac{2} < 5,
\end{equation*}
so I have to add an extra space:
\begin{equation*}
    b = 7 + \myfrac{1} \; \myfrac{2} < 5.
\end{equation*}

\end{document}

which produces the following output

Just adding \, to the end of custom function seems not working, as then the space between \myfrac{1} and other operators (i.e., =) is larger than expected.

3
  • You might look at the code used by tabular to process || or \hline\hline. It treats two consecutive \hlines in a totally different way from normal. The code is also very complicated. Jun 5, 2022 at 14:55
  • Thanks! I'll take a look. Jun 5, 2022 at 16:46
  • Not that it helps you, but it could be comforting (and interesting to know): In the newer engine luametatex fraction is its own atom class (like ordinary, binary, relation, ...), so to solve your problem, one could (locally) increase the fraction-fraction space.
    – mickep
    Jun 5, 2022 at 17:04

1 Answer 1

7

You can check whether another \myfrac follows and, in this case, add \;.

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\myfrac}[1]{%
  \dfrac{\int\limits_0^{#1} x^2 \, \mathrm{d}x}{\zeta(#1)}%
  \@ifnextchar\myfrac{\;}{}%
}
\makeatother

\begin{document}

\begin{gather*}
    a = 4 + \myfrac{1} < 5.
\\
    b = 7 + \myfrac{1} \myfrac{2} < 5.
\end{gather*}

\end{document}

enter image description here

The same with expl3:

\documentclass{article}
\usepackage{amsmath}

\ExplSyntaxOn

\NewDocumentCommand{\myfrac}{m}
 {
  \dfrac{\int\limits\sb{0}\sp{#1} x^2 \, \mathrm{d}x}{\zeta(#1)}
  \peek_remove_spaces:n { \peek_meaning:NT \myfrac {\;} }
 }
\ExplSyntaxOff

\begin{document}

\begin{gather*}
    a = 4 + \myfrac{1} < 5.
\\
    b = 7 + \myfrac{1} \myfrac{2} < 5.
\end{gather*}

\end{document}

You might consider a different syntax:

\documentclass{article}
\usepackage{amsmath}

\ExplSyntaxOn

\cs_new_protected:Nn \oleg_zetafrac:n
 {
  \dfrac{\int\limits\sb{0}\sp{#1} x^2 \, \mathrm{d}x}{\zeta(#1)}
 }
\cs_generate_variant:Nn \oleg_zetafrac:n { V }
\NewDocumentCommand{\zetafrac}{m}
 {
  \seq_set_split:Nnn \l_tmpa_seq { ; } { #1 }
  \seq_pop_left:NN \l_tmpa_seq \l_tmpa_tl
  \oleg_zetafrac:V \l_tmpa_tl
  \seq_map_inline:Nn \l_tmpa_seq { \; \oleg_zetafrac:n { ##1 } }
 }
\ExplSyntaxOff

\begin{document}

\begin{gather*}
    a = 4 + \zetafrac{1} < 5.
\\
    b = 7 + \zetafrac{1;2} < 5.
\\
    c = 11 + \zetafrac{1;2;3} < 5.
\end{gather*}

\end{document}

enter image description here

2
  • Ah, not that complicated after all. Jun 5, 2022 at 22:46
  • Wow, that's exactly what I need! Thanks!! Jun 7, 2022 at 9:18

You must log in to answer this question.

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