# \newcommand for a function with one optional argument inside other optional argument

SX. In this case, I want to create a \newcommand named \fun for function.

My problem arises when I use a \ fun command again with the name f (the letter f is widely used in my document) and avoid the first definition because otherwise, I will get the first case.

# Minimal example

\documentclass{memoir}
\usepackage{amsmath,amssymb}
\usepackage{xstring}
\newcommand{\rn}[1][n]{%
\IfEqCase{#1}{%
{1}{\mathbb{R}}%
{2}{\mathbb{R}^{2}}%
{3}{\mathbb{R}^{3}}%
{n}{\mathbb{R}^{n}}
{#1}{\mathbb{R}^{#1}}
% You can add a finite number of more cases :)
}%
}%

\newcommand{\fun}[3][f]{%
\IfEqCase{#1}{
{f}{f\colon\mathcal{A}\rightarrow\mathcal{B}}
{g}{g\colon\mathcal{C}\rightarrow\mathcal{D}}
{#1}{#1\colon #2\rightarrow #3}
% Maybe "othercase" can generate case like f or g.
}
}

\begin{document}

If I use $f$ function like this $\fun[f]{\rn}{\rn[m]}$,

but I expect to use a function called $f\colon\rn\rightarrow\rn[m]$.

This example works fine, e.g, $\fun[\omega]{\rn}{\rn[1]}$ but I want to use
this command

%\fun[othercase]{f}{\rn}{\rm[1]}

\end{document}


This example works fine, e.g, $\fun[\omega]{\rn}{\rn[1]}$.

If I use $f$ function like this $\fun[o]{f}{\rn}{\rn[m]}$,

but I expect to use a function called $f\colon\rn\rightarrow\rn[m]$.

# Update

I tried to construct the command like this:

\newcommand{\fun}[5][default]{%
\IfEqCase{#1}{
{default}{
\IfEqcase{#2}{
{f}{#2\colon\mathcal{A}\rightarrow\mathcal{B}}
{g}{#2\colon\mathcal{C}\rightarrow\mathcal{D}}
}
}
{other}{
{}{#3\colon #4 \rightarrow #5}
}
}
}


Because I don't want lost the default options with f (f\colon\mathcal{A}\rightarrow\mathcal{B}) and g (g\colon\mathcal{C}\rightarrow\mathcal{D}) respectively.

I see that I need one optional argument inside other optional argument.

# Source

I use this idea for create the command.

• your question would be clearer if you completed the example so it was a complete document showing the problem, but you say in comments that $\fun[\omega]{\rn}{\rn[1]}$ is fine, but it gives a runaway argument error. – David Carlisle Apr 26 '18 at 15:32
• if you really want three optionsl arguments (as the question states in the text), please add "optional" to the question title. that will make it much more clear and obvious. – barbara beeton Apr 26 '18 at 15:46
• I'm not sure how you can expect that a macro defined with one optional and two mandatory arguments will accept three mandatory arguments. – egreg Apr 26 '18 at 16:50
• Sorry, I expected the deleted code when I edited, I'm sorry. I hope that the example is clear. – Oromion Apr 26 '18 at 17:09
• If you uncomment the line you say you want you get an error that there is missing $ as you have omitted the $ if you surround the expression with $ you get the error ! Class memoir Error: Font command \rm is not supported. because \rm is not defined to do anything. perhaps you intended \rn but neither of those errors are comnnected with your macros at all. – David Carlisle Apr 26 '18 at 20:22 ## 3 Answers It's very unclear to me what you are trying to do here, or why (given the output you expect) you have defined \fun as you do. Trace the expansion (use \tracingmacros=1 if it helps, though there's a lot of junk from xstring): • You call \fun[f]{\rn}{\rn[m]} • After dealing with the optional argument, this boils down to using a command which has the template \fun[#1]{#2}{#3}. In your first example these #1=f, #2=\rn and #3=rn[m]. • Within \fun the first thing you do is use a case statement to consider the optional argument #1. The first line matches if #1 is f, which it is. • That line is therefore expanded, to \mathcal{A}\rightarrow\mathcal{B} • Since you haven't asked \fun to do anything with #2 and #3 in such a case, they just disappear, like any other unused argument. What I don't understand is why you have this definition of \fun if you want what you say you want: I can't understand why you are looking at the first optional argument at all. Looks to me like you just want: \newcommand{\fun}[3]{% \ensuremath{#1:#2#3}}  And then use it as \fun{f}{\rn}{\rn[m]} • Sorry, but I want to preserve this two options for f and g cases and don't generate conflict when I use f with other sets, predefined above – Oromion Apr 27 '18 at 1:04 • I don't think any of us can understand what you actually want! The best thing would be to give two or three examples: this input should produce that result. At the moment you seem to want the same input to produce different results depending on some criterion that you have not explained. It's quite clear that you cannot get what you want using your code. – Paul Stanley Apr 27 '18 at 6:49 • I think we have already found a solution. Thank you for your contribution, my friend. – Oromion Apr 28 '18 at 4:35 If you uncomment the line you say you want you get an error that there is missing $ as you have omitted the $ if you surround the expression with $ you get the error ! Class memoir Error: Font command \rm is not supported. because \rm is not defined to do anything. perhaps you intended \rn but neither of those errors are comnnected with your macros at all.

I do not see why you are using the test in \fun where it is not doing anything useful (in the first two cases it means you discard the arguments??) the xstring test isn't doing much in \rn either (only the test for 1, omitting the superscript, is doing anything useful) but I left that.

\documentclass{memoir}
\usepackage{amsmath,amssymb}
\usepackage{xstring}
\newcommand{\rn}[1][n]{%
\IfEqCase{#1}{%
{1}{\mathbb{R}}%
{2}{\mathbb{R}^{2}}%
{3}{\mathbb{R}^{3}}%
{n}{\mathbb{R}^{n}}
{#1}{\mathbb{R}^{#1}}
% You can add a finite number of more cases :)
}%
}%

\newcommand{\fun}[3][f]{%
#1\colon #2\rightarrow #3
}

\begin{document}

If I use $f$ function like this $\fun[f]{\rn}{\rn[m]}$,

but I expect to use a function called $f\colon\rn\rightarrow\rn[m]$.

This example works fine, e.g, $\fun[\omega]{\rn}{\rn[1]}$ but I want to use
this command

$\fun[f]{\rn}{\rn[1]}$

\end{document}

• Hi, I notice that I don't want to lose the default commands for f and g functions, you answer is clear but I want to create other option inside. Thank you. – Oromion Apr 27 '18 at 1:02
• @Oromion sorry I do not understand your comment at all, you had defined \fun to take three arguments but in most cases two of them were mandatory but not used, why is that useful? I can not guess what you wanted the command to do that is different to the code I show here, sorry. I have not lost any defaults here, the command has one optional argument defaulting to f just as you defined it. – David Carlisle Apr 27 '18 at 1:08

In this small example, I created two subcommands, the first one I called default and the second general, I used the xstring package the command \IfEqCase, but I have obtained some unexpected results, I hope it helps you.

\documentclass[14pt]{memoir}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{xstring}

\newcommand{\comdefault}[1][f]{
\IfEqCase{#1}{
{f}{\ensuremath{f\colon\mathcal{A}\rightarrow\mathcal{B}}}
{g}{\ensuremath{g\colon\mathcal{C}\rightarrow\mathcal{D}}}}%[other case]
}

\newcommand{\comgeneral}[3]{
\ensuremath{#1\colon#2\rightarrow#3}
}

\newcommand{\comfinal}[4][default]{
\IfEqCase{#1}{
{default}{\comdefault[#2]}
{general}{\comgeneral{#2}{#3}{#4}}}
}

\begin{document}

\begin{enumerate}
\item The function $g$ is \comdefault[g]. % Subcommand 1
\item The function $f$ is \comgeneral{f}{X}{Y}. % Subcommand 2
\item La función $h_1$ does not work correctly %\comfinal{h_1}. %{}%{}{}
\item La función $h_2$ does not work correctly either. %\comfinal{h_2}{}
\item The function $h_3$ works \comfinal[general]{X}{Y}{Z}
\end{enumerate}
\end{document}
`