1

I currently fail at creating macros that are capable of simplifying some math notation. I'm using two separate macros. One

\usepackage{xargs}
\usepackage{xifthen}

\newcommandx\vecidx[3][2= , 3= ]{  
    \mathbf{#1}^{
        \ifthenelse{\isempty{#2}}{}{\scriptscriptstyle (#2)}
    }_{
        \ifthenelse{\isempty{#3}}{}{\scriptscriptstyle #3}
    }
}

helps me in writing vector notation.\vecidx{z}[1] gives $\mathbf_{z}^{\scriptscriptstyle (1)}$, \vecidx{z}[1][ij] gives $\mathbf_{z}^{\scriptscriptstyle (1)}_{ij}$. The other macro is used in partial derivatives

\newcommandx\pd[2][1= ]{  
    \frac{\partial #1}{\partial #2}
}

Things work as expected as long as I don't pass \vecidx{z}[1] as an argument to \pd, like \pd[\vecidx{x}[1]]{\vecidx{z}[1]} which gives rise to an error Missing } inserted ....

I found that escaping \pd[{\vecidx{x}[1]}]{\vecidx{z}[1]} helps, but all those brackets make it hard to read. Is there anything I can do to simplify the syntax?

  • 1
    Off-topic: You know packages like skmath and physics? – Johannes_B Apr 17 '18 at 8:59
  • @Johannes_B no, these packages are new to me. skmath seems to have a bit of what I need. Thanks for the link! – cheind Apr 17 '18 at 9:10
3

Looks like a job for xparse

\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\vecidx}{moo} %
  {%
    \mathbf{#1}%
      \IfNoValueF{#2}{^{\scriptscriptstyle (#2)}}%
      \IfNoValueF{#3}{_{\scriptscriptstyle (#3)}}%
  }
\NewDocumentCommand{\pd}{O{}m}
  {\frac{\partial #1}{\partial #2}}
\begin{document}
$\pd[\vecidx{x}[1]]{\vecidx{z}[1]}$
\end{document}

(I'm not sure I'd be keen on using this syntax myself: tends to hide what is going on.)

This works as the xparse implementation of optional arguments using bracket-matching: it doesn't get 'confused' by the nested square brackets.

Note the syntax for specifying arguments:

  • m: mandatory argument in braces
  • o: optional argument in square brackets, which returns the special -NoValue- marker if the argument is not given
  • O{<default>}: optional argument in square brackets with <default> returned if the argument is not given
  • works like a charm, thanks! no sure what does mooand O{}m elements are doing though. Guess that's best understood from the package description. – cheind Apr 17 '18 at 9:13
  • thanks! One more minor thing, when trying todo \vecidx{y}[][i] I do see empty superscript brackets. Do you know how to fix this? I guess that's because [] is an empty value and not a no-value. – cheind Apr 17 '18 at 9:19
  • Was able to fix the above myself using \IfNoValueF{#2}{\@ifmtarg{#2}{}{^{\scriptscriptstyle (#2)}}}%. Taken from tex.stackexchange.com/questions/33749/… – cheind Apr 17 '18 at 9:29

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.