4

I would like to define a newcommand with two optional arguments, which in turn, calls on another command with two optional arguments.

Something like the following (\B calls \A). How do I do it?

\newcommand{A}[3][][]{{#1 + #2 + #3}}
\newcommand{B}[3][][]{#1 + \A[#2][#3]{#1}}

\B{9}
\B[1][]{9}
\B[][3]{9}
\B[1][3]{9}

EDIT

I tried to give a similar MWE, but I am afraid that the above example actually may not properly convey exactly what I need.

The reason I had this question is to create a command that takes in a certain bib-item and highlights it. I would like to pass two optional arguments to \parencite[pre][post]{#1} (where pre and post could be empty) via a newcommand \hlcite[pre][post]{#1}. The command \hlcite would then highlight the references.

\usepackage{graphicx, color}
\usepackage[dvipsnames]{xcolor}
\usepackage{bookmark}
\usepackage[
    backend=biber,          % backend: biber
    style=authoryear,       % style: numeric-comp, authoryear
    sorting=ynt,            % sorting: none, ynt
]{biblatex}
\usepackage{soul} 
\newcommand{\hlc}[2][yellow]{\sethlcolor{#1} \hl{#2}}
%% This works
\newcommand{\hlcite}[1]{\colorbox[green]{\mbox{\parencite{#1}}}}
%% But this throws error
\newcommand{\hlcite}[3]{\hlc[green]{\mbox{\parencite[#2][#3]{#1}}}}

References

Adding optional arguments

Non-stackoverflow/stackexchange links:

Using if conditional in latex:

References to use \mbox with \cite or \parencite inside \newcommand for highlighting.

Highlighting text in Latex:

4

2 Answers 2

5

You can use \NewDocumentCommand.

\NewDocumentCommand{\hlc}{O{yellow}m}{\sethlcolor{#1}\hl{#2}}

\NewDocumentCommand{\hlcite}{oom}{%
  \hlc[green]{%
    \IfNoValueTF{#1}{% no optional argument
      \parencite{#3}%
    }{%
      \IfNoValueTF{#2}{% just one optional argument
        \parencite[#1]{#3}%
      }{% both optional arguments
        \parencite[#1][#2]{#3}%
      }%
    }%
  }% end of \hlc
}

Note that you don't want a space between \sethlcolor{#1} ad \hl{#2}.

6
3

Maybe something like this? Using \newcommand, one has to use at least two commands sequentially, if one wishes to absorb two optional arguments.

\documentclass{article}
\newcommand\A[1][0]{\def\Atmp{0#1}\Ahelp}
\newcommand\Ahelp[2][0]{\expandafter\Axhelp\expandafter{\Atmp}{0#1}{#2}}
\newcommand\Axhelp[3]{\the\numexpr#1 + #2 + #3\relax}
\newcommand\B[1][0]{\def\Btmp{0#1}\Bhelp}
\newcommand\Bhelp[2][0]{\expandafter\Bxhelp\expandafter{\Btmp}{0#1}{#2}}
\newcommand\Bxhelp[3]{\the\numexpr#1 + \Axhelp{#2}{#3}{#1}\relax}
\begin{document}

\B{9}

\B[1][]{9}

\B[][3]{9}

\B[1][3]{9}

\end{document}

enter image description here

The \B command could be implemented more simply with

\NewDocumentCommand\B{ O{0} O{0} m }{\the\numexpr#1 + \Axhelp{0#2}{#3}{0#1}\relax}

However, the same is not true of \A because \Ahelp in the present case is expandable, whereas \A defined via \NewDocumentCommand is not.

4
  • Thank you. But, what I need more generally is way to pass empty arguments (not just zero). Basically what I need is this: \newcommand{\hlcite}[3]{\hlc[green]{\mbox{\parencite[#2][#3]{#1}}}}, where \newcommand{\hlc}[2][yellow]{\sethlcolor{#1} \hl{#2}} defines \hlc.
    – CypherX
    Commented Apr 19, 2022 at 0:58
  • @steven-b-segletes : Since, your answer does what I asked initially, I voted it up. But it would be nice, if you could kindly help me with the actual problem of interest, I posted in the question later. Thank you.
    – CypherX
    Commented Apr 19, 2022 at 1:16
  • @CypherX To allow true blank arguments (which do not work in the context of the \numexpr example originally specified), you need to do two things to my code: remove the [0] optional argument defaults and replace them with []; and in \A, \Ahelp, etc, change the 0#1 to simply #1. Commented Apr 19, 2022 at 1:27
  • @steven-b-segletes : Thank you.
    – CypherX
    Commented Apr 19, 2022 at 1:28

You must log in to answer this question.

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