# How to automatically put a space after the first parenthesis in f(x) g(y)?

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.)

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

\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}


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.

• 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? – Mehrdad Mar 2 '14 at 2:50
• @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. – Heiko Oberdiek Mar 2 '14 at 3:21