0

I'd like to have a shorthand command for math that accepts an optional argument which gets put into parentheses if it exists and where the operator also optionally has a subscript. How can I position the subscript and parentheses how I want it?

I would expect it to look something like this MWE:

\documentclass{article}
\usepackage{amsmath}
\newcommand{\op}[1]
{
    \operatorname{\mathrm{J}}(#1)
}
\begin{document}
\begin{tabular}{lllll}
    $ \op $ &
    $ \op{a} $ & 
    $ \op_f $ & 
    $ \op_f{a} $ & 
    $ \op{a}_f $ % probably doesn't make sense to use it like this
    \\
    $ \mathop{\mathrm J} $ &
    $ \mathop{\mathrm J}(a) $ & 
    $ \mathop{\mathrm J}_f $ & 
    $ \mathop{\mathrm J}_f(a) $ & 
    $ \mathop{\mathrm J}_f(a) $
\end{tabular}
\end{document}

The bottom row is how I would want the first code-block to look: mwe

  • I know the \op takes the next character as its argument without curly brackets, hence why it looks so weird, but I don't know why it grabs the brace in the definition – josh May 10 at 8:10
  • Is there a real improvement over typing \op_f(a)? – egreg May 10 at 8:42
  • 1
    Indeed, I would just use \DeclareMathOperator{\op}{J} and normal syntax like $\begin{array}{*{4}{l}} \op & \op(a) & \op_f & \op_f(a) \end{array}$. – frougon May 10 at 8:52
  • Indeed for this operator that works. I have other operators for which the arguments can be taller, like a fraction, hence I'd use \left(#1\right) which I am too lazy to write, instead of (#1). And I want to understand a potential answer if there is one for other future scenarios which are in general more complicated. – josh May 10 at 9:05
2

You could do this with the xparse package by using \NewDocumentCommand. I'm not sure if this is such a good idea, however, since an optional brace-delimited argument is very un-LaTeX and it's easy to accidentally pass something in as an argument when you didn't mean to.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\NewDocumentCommand{\op}{e{_}ge{_}}{%
  \operatorname{J}%
  \IfValueT{#1}{_{#1}}\IfValueT{#3}{_{#3}}%
  \IfValueT{#2}{(#2)}%
}

\begin{document}

\begin{tabular}{lllll}
    $ \op $ &
    $ \op{a} $ &
    $ \op_f $ &
    $ \op_f{a} $ &
    $ \op{a}_f $ % probably doesn't make sense to use it like this
    \\
    $ \mathop{\mathrm J} $ &
    $ \mathop{\mathrm J}(a) $ &
    $ \mathop{\mathrm J}_f $ &
    $ \mathop{\mathrm J}_f(a) $ &
    $ \mathop{\mathrm J}_f(a) $
\end{tabular}

\end{document}

output

In the argument specification, e{_} denotes an optional argument preceded by a _ and g denotes an optional {}-delimited argument. \IfValueT is used to test if an optional argument was provided and to then act accordingly. See the documentation for more details.

Note that the g argument type is not listed at the top because it is only included for backwards compatibility as it “should largely be avoided nowadays as using them [it] in packages leads to inconsistent user interfaces” (from the documentation).

| improve this answer | |
1

The way you can do that is defining a new command with two paramaters as follows:

\newcommand{\op}[2]{\mathrm{J}_{#1}(#2)}

and then write the command in the form \op{subscript}{argument}. This works good, but in case you want to type only the operator, you must write \op{}{} instead of just \op, and you can't write superscripts directly on the operator (you can't write \op^2{}{}, for example).

If you will treat the new operator as a regular operator (I mean, like the \sin operator), my recommendation is to write in the preamble

\DeclareMathOperator{\op}{\mathrm{J}}

and just write the subscript and the argument as with \sin, i.e. in the form \op_f(a). This has the advantage that \op is treated just like \sin so you can write subscrpits and superscripts directly on the operator and omit putting the parenthesis if the argument doesn't exist.

| improve this answer | |

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.