# How to pass optional empty arguments in a newcommand down to another command with optional empty arguments in LaTeX?

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

Using if conditional in latex:

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

Highlighting text in Latex:

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

• Thank you. So, I would need to add \usepackage{xparse} for using \NewDocumentCommand, right? Apr 19 at 23:21
• @CypherX If you have LaTeX released before 2020-10-01 Apr 19 at 23:24
• Thank you. Could you please explain, what happens if you leave space between \sethlcolor{#1} and \hl{#2}? Apr 19 at 23:27
• @CypherX Then you get a space in the output, what else? (occasionally that's actually what you want, but it's very rare.) Further reading: symbols - What is the use of percent signs (%) at the end of lines? (Why is my macro creating extra space?) - TeX - LaTeX Stack Exchange Apr 20 at 4:55
• @egreg Thank you. So, the % is the LaTeX equivalent of - used in Jinja2. Apr 20 at 19:23

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}


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.

• @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. Apr 19 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. Apr 19 at 1:27
• @steven-b-segletes : Thank you. Apr 19 at 1:28