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:


\optakes 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 '20 at 08:10\op_f(a)? – egreg May 10 '20 at 08:42\DeclareMathOperator{\op}{J}and normal syntax like$\begin{array}{*{4}{l}} \op & \op(a) & \op_f & \op_f(a) \end{array}$. – frougon May 10 '20 at 08:52\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 '20 at 09:05