# Math operators with and without parentheses

I have made several math operators in my preamble, but now I want to make a minor change (in the output). I will use the gradient operator as an example in the following. The MWE below shows my gradient definition including a small example of usage.

\documentclass{memoir}

\usepackage{amsmath}
\usepackage{mathtools}

\DeclarePairedDelimiter\parentheses{\lparen}{\rparen}

\begin{document}

\begin{align}
\end{align}

\begin{align}
\sin x = \sin (2y)
\end{align}

\end{document}


Currently, I always get parentheses in the ouput:

The problem is that I would like the left-hand side without parentheses, but still have parentheses on the right-hand side---depending on whether there is a single or multiple inputs/parameters, as exemplified for the sine:

I would like to change this globally based on the above example without having to correct any operator notation throughout my document. Is that possible? Or optimal? And how is it best done in general (from scratch)?

(I would prefer automatic scaling of the parentheses.)

• It's not a minor change, unfortunately; the concept of “single symbol” is hard to define as the \vec{x} and x cases show. If you expect that a “single” argument to \grad is always of the form \vec{...}, then something can be devised. By the way, it's a very bad idea to automatically use the * form of the delimiters; I know it's handy, but it's wrong nonetheless. Mar 31, 2016 at 12:11
• @egreg I expected so. Unfortunately, \grad arguments can be of any kind, not just \vec{...}. Are you saying that it is not possible to make such a command? (At least not without making use of exotic stuff.) Mar 31, 2016 at 12:17
• At the minimum, a list of the possible “single” arguments not to be braced is needed. Mar 31, 2016 at 12:20
• it is not clear why you use parentheses at all. sin 2y doesn't need them and neither does \grad \vec x (naturally not using the \grad definition as in OP). The spacing is indicative enough.
– user4686
Mar 31, 2016 at 12:56
• It is always nice to have something automatic, but Mico's approach with grad/gradp or sin/sinp and you, the human, who decides which one to use has much for it in this case. Naturally egreg, Manuel, Mico and others interested can explain how to do it automatically if the criterion is completely explicit.
– user4686
Mar 31, 2016 at 14:02

Based on a comment from Paul, my job actually became, perhaps, easier. Here, I do tests on the argument to \grad. If the argument is a single token (or embraced quantity) OR if it is \vec{<single token or embraced quantity>}, the parens are not employed. Otherwise, they are.

\documentclass{memoir}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{ifthen}
\DeclarePairedDelimiter\parentheses{\lparen}{\rparen}
\ifx\relax#2\relax%
\else
\ifx\relax#3\relax%
\else
\fi
\fi
}
\begin{document}

\begin{align}
\end{align}
\end{document}


• What if \grad{2y} is on the left-hand side? I think the left-right description was just to explain for that particular example. In general, he wants "single" operands (whatever that precisely means; see the comments on the Q) to not have parentheses, while operands containing multiple symbols have the parentheses. Mar 31, 2016 at 13:15
• @PaulGessler That would be a misunderstanding on my part. I will delete my answer unless I can revise accordingly. Mar 31, 2016 at 13:44
• @PaulGessler Perhaps this is an improvement. Mar 31, 2016 at 14:32
• \ifx#2\relax is a bit dangerous if #2 is braced and starts with two identical tokens, perhaps \ifx\relax #2 ?
– user4686
Mar 31, 2016 at 16:21
• the big thing is between delimited and non delimited parameters; I recall learning my trade with parameters delimited with (multiple) space tokens ...
– user4686
Mar 31, 2016 at 17:13

Using the power of the xparse package, here's a \grad macro that checks if the next char is (; if that's the case, it treats the contents of the parentheses as an argument and puts \parens*{#1} around the argument automatically. So you can use \grad A and \grad(A) and the latter would become \grad\parens*{A} automatically. Plus, thanks to xparse, the macro does grab balanced parentheses, so \grad(\foo(x)) would treat \foo(x) correctly.

\documentclass{scrartcl}
\usepackage{xparse,mathtools}
\DeclarePairedDelimiter\parens{\lparen}{\rparen}

\begin{document}
$\grad \vec x = \grad(\frac{y}{z})$
\end{document}

• +1 for providing a detailed description. I've taken the liberty of fixing a couple of typos
– Mico
Mar 31, 2016 at 13:02
• I see I made a lot of errors (the one with usepackage was big!!). Thank you. Mar 31, 2016 at 15:51

For maximum flexibility, I suggest you set up two macros: \grad as a basic math operator, and \gradp, which takes an argument surrounded by autosized parentheses.

\documentclass{memoir}
\usepackage{mathtools}
\DeclarePairedDelimiter\parens{\lparen}{\rparen}

\begin{document}
$\grad\vec{x} = \gradp{\frac{y}{z}}$
\end{document}

• +1 but the image is strangely rasterized...
– user4686
Mar 31, 2016 at 14:03
• @jfbu - Yeah, I'm at a site today that doesn't make it easy to upload high-resolution image files. :-(
– Mico
Mar 31, 2016 at 14:07
• ...but it feel so much dvi2hercules from 1990.... ;'-) Mar 31, 2016 at 14:50
• @jfbu - Better now? :-)
– Mico
Mar 31, 2016 at 16:11
• @Rmano - Wow, dvi2hercules?! You must be as old as I am!! :-)
– Mico
Mar 31, 2016 at 16:42