6

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}
\newcommand{\grad}[1]{\operatorname{grad} \parentheses*{#1}}

\begin{document}

\begin{align}
\grad{\vec{x}} = \grad{2y}
\end{align}

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

\end{document}

Currently, I always get parentheses in the ouput: gradient

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:

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

  • 1
    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. – egreg Mar 31 '16 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.) – Thomas Mar 31 '16 at 12:17
  • At the minimum, a list of the possible “single” arguments not to be braced is needed. – egreg Mar 31 '16 at 12:20
  • 2
    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 '16 at 12:56
  • 2
    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 '16 at 14:02
4

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}
\newcommand{\npgrad}[1]{\operatorname{grad} {#1}}
\newcommand{\pgrad}[1]{\operatorname{grad} \parentheses*{#1}}
\newcommand\grad[1]{\gradx#1\relax\relax\relax\relax}
\def\gradx#1#2#3\relax{%
  \ifx\relax#2\relax%
    \npgrad{#1}%
  \else
    \ifx\relax#3\relax%
      \ifx\vec#1\npgrad{#1{#2}}\else\pgrad{#1{#2}}\fi
    \else
      \pgrad{#1{#2}{#3}}%
    \fi
  \fi
}
\begin{document}

\begin{align}
\grad{\vec{x}}          &= \grad{2y}\\
\grad{\frac{2w}{y}}     &= \grad{\vec{z}}\\
\grad{p}                &= \grad{\vec{a}\times\vec{b}}\\
\grad{\vec{\mathcal{P}}} &= \grad{\alpha}
\end{align}
\end{document}

enter image description here

  • 1
    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. – Paul Gessler Mar 31 '16 at 13:15
  • @PaulGessler That would be a misunderstanding on my part. I will delete my answer unless I can revise accordingly. – Steven B. Segletes Mar 31 '16 at 13:44
  • 1
    @PaulGessler Perhaps this is an improvement. – Steven B. Segletes Mar 31 '16 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 '16 at 16:21
  • 1
    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 '16 at 17:13
7

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}
\NewDocumentCommand\grad{d()}{\operatorname{grad}\IfValueT{#1}{\parens*{#1}}}

\begin{document}
\[ \grad \vec x = \grad(\frac{y}{z}) \]
\end{document}
  • 1
    +1 for providing a detailed description. I've taken the liberty of fixing a couple of typos – Mico Mar 31 '16 at 13:02
  • I see I made a lot of errors (the one with usepackage was big!!). Thank you. – Manuel Mar 31 '16 at 15:51
6

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.

enter image description here

\documentclass{memoir}
\usepackage{mathtools}
\DeclareMathOperator{\grad}{grad}
\DeclarePairedDelimiter\parens{\lparen}{\rparen}
\newcommand{\gradp}[1]{\grad\parens*{#1}}

\begin{document}
\[
\grad\vec{x} = \gradp{\frac{y}{z}}
\]
\end{document}
  • +1 but the image is strangely rasterized... – user4686 Mar 31 '16 at 14:03
  • 1
    @jfbu - Yeah, I'm at a site today that doesn't make it easy to upload high-resolution image files. :-( – Mico Mar 31 '16 at 14:07
  • 3
    ...but it feel so much dvi2hercules from 1990.... ;'-) – Rmano Mar 31 '16 at 14:50
  • @jfbu - Better now? :-) – Mico Mar 31 '16 at 16:11
  • 1
    @Rmano - Wow, dvi2hercules?! You must be as old as I am!! :-) – Mico Mar 31 '16 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.