SX.](https://tex.stackexchange.com), I would like to define a new command, in this occasion I want to denote a function, as in the title of the question.
My attempt
\documentclass{memoir}
\usepackage{xparse} % Possible \usepackage{xstring} :)
\NewDocumentCommand{\fun}{o}{%
\IfValueTF{#1}{%
#1\colon\mathcal{C}\rightarrow\mathcal{D}%
}{%
f\colon\mathcal{A}\rightarrow\mathcal{B}%
}%
}
\begin{document}
$\fun$ %$f\colon\mathcal{A}\rightarrow\mathcal{C}$
$\fun[g]$ %$g\colon\mathcal{C}\rightarrow\mathcal{D}$
% You can add more cases, i.e, $\fun[*]$
% where * is the letter that is defined.
$\fun{h}{X}{Y}$ %h\colon\mathcal{X}\rightarrow\mathcal{Y}
\end{document}
Another valid suggestion to solve is with the \IfEqCase
or\IfStrEqCase
commands in the xstring
package:
\IfEqCase{f}{
{f}{f\colon\mathcal{A}\rightarrow\mathcal{C}}
{g}{g\colon\mathcal{C}\rightarrow\mathcal{D}}
}[{#1#2#3}{#1\colon\mathcal{#2}\rightarrow\mathcal{#3}}]
The output that you would expect is $\fun$.
\IfEqCase{g}{
{f}{f\colon\mathcal{A}\rightarrow\mathcal{C}}
{g}{g\colon\mathcal{C}\rightarrow\mathcal{D}}
}[{#1#2#3}{#1\colon\mathcal{#2}\rightarrow\mathcal{#3}}]
The output that you would expect is $\fun[g]$.
\IfEqCase{hXY}{
{f}{f\colon\mathcal{A}\rightarrow\mathcal{C}}
{g}{g\colon\mathcal{C}\rightarrow\mathcal{D}}
}[{#1#2#3}{#1\colon\mathcal{#2}\rightarrow\mathcal{#3}}]
The output that you would expect is $\fun{h}{X}{Y}$.
As shown, I want you to have a default parameter and at least one optional parameter.
If this suggestion is valid, then could you help me implement it in the creation of the command?
PD: Some time ago I asked a similar question. I have a "confused solution" that did me think over about using extra braces {}{}
in order to do not obtain an error.
Thanks in advance.