I have a fairly basic questions (I guess), but can't find a clear answer.

I have an existing command \ox{#1,#2} from a package (chemmacros) which has two arguments which are separated by commas. I want to change the command (i.e. \renewcommand), but I don't know how to handle the comma-separated arguments.

By basic idea would be:


But this doesn't work.

Does anyone have a suggestion how to separate the arguments?

  • 2
    You can specify delimiters of arguments with \def, e.g. \makeatletter\def\ox#1{\@ox[#1]}\def\@ox[#1,#2]{#1($\mathrm{#2}$)}\makeatother. Note that this doesn't work if your argument contains [ or ] -- in this case you would have to specify other delimiters. – Skillmon likes topanswers.xyz Jul 5 '17 at 8:29

Here's a simple version:


\DeclareDocumentCommand { \ox } { m }
        \clist_set:Nn \l_tmpa_clist { #1 }
        \clist_item:Nn \l_tmpa_clist { 1 }
        ($\mathrm{\clist_item:Nn \l_tmpa_clist {2}}$)

| improve this answer | |
  • 1
    You can even avoid setting the clist: \clist_item:nn { #1 } { 1 } (or {2}) will do. – egreg Jul 5 '17 at 8:47

Here are three ways of doing this:


Using xparse and SplitArgument to extract the values and then
pass them to a second helper macro:

\NewDocumentCommand\oxone{>{ \SplitArgument{1}{,} } m }{\realoxone#1}

Using a plain def to extract the values using a helper macro:


Using a pgf loop to extract the values into ox1, ox2, ...:

  \foreach \x [count=\n] in {#1} {
      \csxdef{ox\n}{\x}% save value n as ox<n>
  \csuse{ox1}($\mathrm{\csuse{ox2}}$)% use values

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