11

When I write

\documentclass{article}
\usepackage{mathtools}
\DeclarePairedDelimiter\p{\lparen}{\rparen}

\begin{document}
    \[
        w = \int f\p*{x} g\p*{\frac{y}{2}} h\p*{z}\ dx
    \]
\end{document}

I get

but the three functions are squeezed too close to each other.
In fact, it's so bad that it looks like g is attached to f(x) rather than to (y/2).

So I need to add some space after the ). Clearly it's easy to fix via \, as follows:

        w = \int f\p*{x}\,g\p*{\frac{y}{2}}\,h\p*{z}\ dx

but I obviously don't want to litter the code with \, everywhere!

Is there a way to make the space after a closing right parenthesis do this automatically?
(Note, this shouldn't affect behaviors in other situations -- so, for example, the spacing after a closing parenthesis should stay the same as it is originally if it is followed by a + sign instead of another function.)

14
  • Not automatically, as a space there is generally not desired.
    – egreg
    Commented Feb 24, 2014 at 0:43
  • @egreg: How do people normally solve this problem then?
    – user541686
    Commented Feb 24, 2014 at 0:48
  • 2
    The other way is to use the first form, it isn't clear to me the spacing is an improvement here. Commented Feb 24, 2014 at 0:57
  • 3
    @Mehrdad The problem is usually solved by not adding a thin space.
    – egreg
    Commented Feb 24, 2014 at 0:59
  • 1
    \DeclarePairedDelimiter\p{\lparen}{\rparen\,} will automatically add space, and you can replace your \ with another \, at the end if needed. Commented Feb 24, 2014 at 0:59

1 Answer 1

10

\mathinner adds a space before a following math ord atom, but the usual space is set before a binary or relational operator:

\documentclass{article}
\usepackage{mleftright}
\usepackage{mathtools}

\makeatletter
\DeclarePairedDelimiter\p{\lparen}{%
  \rparen
  % the math atom before is expected to be a `\mathclose`,
  % then we need to cancel the space between `\mathclsoe`
  % and `\mathinner` that is added in non-script styles.
  \@ifnextchar{,}{}{%
  \@ifnextchar{;}{}{%
  \@ifnextchar{\colon}{}{%
    \nonscript\!%
    \mathinner{}%
  }}}%
}
\makeatother

\begin{document}
    % automatic space setting:
    \[
        w\p*{y} = \int f\p*{x} g\p*{\frac{y}{2}} + h\p*{z}, i\p*{z} dx
    \]
    % manual space setting:
    \[
        w(y) = \int f(x)\,g\mleft(\frac y2\mright) + h(z), i(z)\,dx
    \]
\end{document}

Result

Remarks:

  • \mathinner does not add spaces in \scriptstyle or \scriptscriptstyle (except before a math operator/\mathop). This might be a limitation or a feature (more compact subscripts).

  • Support for punctuation chars ,, ;, \colon added, if they are directly following. If they are hidden inside macros, then they are not detected.

2
  • Hmm, it seems like this messes up the spacing when there's a comma after the closing parenthesis. Do you happen to know how to fix it?
    – user541686
    Commented Mar 2, 2014 at 2:50
  • 1
    @Mehrdad: Except for \mathclose there is always a space in non-script math styles. This can be compensated by \! manually or the definition must explicit check for punctuation characters. Commented Mar 2, 2014 at 3:21

You must log in to answer this question.

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