5

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:

\renewcommand{\ox}[2]{#1($\mathrm{#2}$)}

But this doesn't work.

Does anyone have a suggestion how to separate the arguments?

1
  • 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
    Jul 5, 2017 at 8:29

2 Answers 2

3

Here's a simple version:

\documentclass{article}

\usepackage{expl3,xparse}
\ExplSyntaxOn
\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}}$)
    }
\ExplSyntaxOff

\begin{document}
\ox{1,2}
\end{document}
1
  • 1
    You can even avoid setting the clist: \clist_item:nn { #1 } { 1 } (or {2}) will do.
    – egreg
    Jul 5, 2017 at 8:47
6

Here are three ways of doing this:

\documentclass{article}
\usepackage{xparse}
\usepackage{pgffor}
\usepackage{etoolbox}
\begin{document}

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

\NewDocumentCommand\oxone{>{ \SplitArgument{1}{,} } m }{\realoxone#1}
\newcommand\realoxone[2]{#1($\mathrm{#2}$)}
\oxone{one,two}

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

\newcommand\oxtwo[1]{\realoxtwo#1!}
\def\realoxtwo#1,#2!{#1($\mathrm{#2}$)}
\oxtwo{one,two}

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

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

\end{document}

You must log in to answer this question.

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