1

I wish to make a command \DDF that prints $\delta\Delta\Phi$ with optional scripts. Could you please help me do this?

Here are desired inputs to give desired outputs:

  1. \DDF should give $\delta\Delta\Phi$
  2. \DDF{a}{b} should give $\delta\Delta\Phi_a^b$
  3. \DDF{a}{} should give $\delta\Delta\Phi_a$
  4. \DDF{a} should give $\delta\Delta\Phi_a$
  5. \DDF{}{b} should give $\delta\Delta\Phi^b$

I think the following may (almost) work -- except for (4) -- but even so I'm not sure it's the rigorous or elegant way to do it:

\newcommand{\DDF}[2]{\delta\Delta\Phi_{#1}^{#2}}
4
  • 4
    Why don't you just define \newcommand{\DDF}{\delta\Delta\Phi} then use \DDF or \DDF_a or \DDF^b? Jan 7, 2022 at 18:35
  • 1
    If \DDF - defined as intended by you - is used with mathmode, e.g., $a^2+\DDF{a}{b}+b^2$, then you get $a^2+$\delta\Delta\Phi_a^b$+b^2$ which is a problem as \delta\Delta\Phi_a^b is not in math-mode. If you do $a^2+$\DDF{a}{b}$+b^2$, then you get three separate adjacent formulas which probably is not what you want. Thus I suggest not to wrap entire definition-texts into $. Jan 7, 2022 at 19:07
  • 1
    What about things like \DDF{a}{\empty} or \DDF{\@gobbletwo{x}{y}}{b} ? Jan 7, 2022 at 19:15
  • 1
    as Phelype says, the standard ^ and _ syntax is preferable here, it makes it far more natural to have both optional, as your case 5 shows, you can't omit the first argument if you want to use the second. Jan 7, 2022 at 20:50

1 Answer 1

6

It's not difficult (but with brackets, not braces):

\documentclass{article}
\usepackage{amsmath}

\NewDocumentCommand{\DDF}{oo}{%
  \delta\Delta\Phi
  \IfNoValueTF{#2}{%
    \IfValueT{#1}{_{#1}}
  }{%
   _{#1}^{#2}%
  }%
}

\begin{document}

$\DDF$

$\DDF[a][b]$

$\DDF[a][]$

$\DDF[a]$

$\DDF[][b]$

\end{document}

enter image description here

However, it's not clear what the advantage is over

\newcommand{\DDF}{\delta\Delta\Phi}

and input as

$\DDF$

$\DDF_{a}^{b}$

$\DDF_{a}$

$\DDF_{a}$

$\DDF^{b}$

The coding you propose is much less clear, in my opinion, and you must remember that the subscript comes first, whereas you can do

\DDF^{b}_{a}

and get identical result to \DDF_{a}^{b}.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .