# Passing * arguments using xparse

In xparse I want to define a command Command-One which calls a command Command-Two and calls the *ed version if and only if the *ed version of Command-One was called.

Specifically I would like to do something like:

\DeclareDocumentCommand{\CTwo}{s o m}
{\IfBooleanTF{#1}{#2}{#3}}

\DeclareDocumentCommand{\CTest}{s o}
{\CTwo#1[#2]{testing}}

\CTest*[work]


and have it output "work".

I realize I can do this with

\DeclareDocumentCommand{\CTest}{s o}
{\IfBooleanTF{#1}
{\CTwo*[#2]{testing}}
{\CTwo[#2]{testing}}}


but this is inelegant, plus I will be defining several distinct commands each with a different value of #3 in \CTwo and it will get repetitive having to always copy If-Then statement multiple times (as well as having to copy the specific argument twice)

Here is a more detailed example using environments. Specifically I would like to be able to define an environment template which I can then use to define several different environments by simply passing it parameters.

\documentclass{amsart}
\usepackage{amsthm}
\usepackage{xparse, l3fp,l3tl}
\begin{document}

\ExplSyntaxOn
\def\TheoremDepth{section}
\theoremstyle{plain}
\newtheorem{theorem}{Theorem}[\TheoremDepth]%
\newtheorem*{theoremstar}{Theorem}%

\DeclareDocumentEnvironment{TheoremTemplate}{s o m m}
{
\IfBooleanTF{#1}
{ Do something
\begin{#1}}
{ Do something else
\begin{#2} }
}
{
\IfBooleanTF{#1}
{ Do something
\end{#1}}
{ Do something else
\end{#2} }
}

\DeclareDocumentEnvironment{MyTheorem}{s o}
{
\begin{TheoremTemplate}#1[#2]{theorem}{theoremstar}
}
{
\end{TheoremTemplate}
}
\ExplSyntaxOff
\end{document}


I would then like to be able to call both \begin{theorem} and \begin{theorem}* and have them do the corresponding things.

• You have also problems with o argument, don't you? – egreg Jul 7 '14 at 7:10
• The fact that xparse doesn't naturally fit what you're trying to do is a good indication that perhaps a re-think is required. I don't think we can provide a good answer without more detail given in the question (e.g., what \CTwo is supposed to be doing). – Will Robertson Jul 7 '14 at 7:33
• As always, if you could post a compilable example, it would be great, starting from \documentclass and ending with \end{document}. That way (without any effort) you simplify our work – Manuel Jul 7 '14 at 14:08
• @Manuel: Sure, I updated it to include those. Please let me know if there is anything else I can do – Nate Ackerman Jul 7 '14 at 14:30

My impression is that you're using the wrong approach; besides, \begin{theorem}* doesn't seem the best syntax.

\documentclass{article}
\usepackage{xparse,amsthm}

\NewDocumentCommand{\DeclareTheorem}{momo}{%
% #1=environment's name
% #2=sibling counter
% #3=label
% #4=parent counter
\IfNoValueTF{#4}
{%
\IfNoValueTF{#2}
{\newtheorem{#1}{#3}}
{\newtheorem{#1}[#2]{#3}}%
}
{%
\newtheorem{#1}{#3}[#4]%
}
\newtheorem*{#1*}{#3}
}

\theoremstyle{plain}
\DeclareTheorem{thm}{Theorem}[section]
\DeclareTheorem{lem}[thm]{Lemma}

\begin{document}

\section{Test}

\begin{lem}
Numbered.
\end{lem}

\begin{thm}
This is numbered.
\end{thm}

\begin{lem*}
Unnumbered.
\end{lem*}

\begin{thm*}
This is unnumbered.
\end{thm*}

\end{document}


If you want that theorems are by default numbered according to sections or chapters, you can change the \NewDocumentCommand line into

\NewDocumentCommand{\DeclareTheorem}{momO{\TheoremDepth}}{%


giving the suitable definition to \TheoremDepth; in this case the declarations above would become

\DeclareTheorem{thm}{Theorem}
\DeclareTheorem{lem}[thm]{Lemma}


if \TheoremDepth is defined to be section.

• Thanks! I was able to use this to get what I wanted to work. – Nate Ackerman Jul 8 '14 at 4:29

To expand on my comment, a few hypotheticals. Why does \CTest need to call \CTwo? If \CTest takes an optional argument, why do you need the star to indicate whether to use it? How should \CTest behave without the optional argument in place?

Usually (in expl3 style) you'd define an underlying function with a fixed number of arguments that takes your input after processing optional arguments and so on. To cut your example down (and perhaps lose the complexity, but I hope you get the idea), you might write something like:

\documentclass{article}
\usepackage{xparse}
\begin{document}
\ExplSyntaxOn
\cs_new:Nn \my_output:n {#1}

\DeclareDocumentCommand{\CTest}{s O{testing}}
{
\IfBooleanTF{#1}
{ \my_output:n {#2} }
{ \my_output:n {testing} }
}
\ExplSyntaxOff

\CTest\\
\CTest*\\
\CTest[work]\\
\CTest*[work]\\

\end{document}


Because you seem to have two cases where a default value should be used (when the star is not present, and when the argument is not present), that needs two places for this default to be entered.

You might also want to have something like your \CTwo defined separately calling this \my_output:n command. If this doesn't capture the complexity of what you're trying to do, I suggest clarifying your question.

• Thanks. This doesn't quite get at what I want so I will clarify the question. Specifically I am trying to create a template for a (theorem like) environments so I can define multiple environments by simply passing arguments to the template. I have expanded the question to help clarify. – Nate Ackerman Jul 7 '14 at 13:56