1

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.

Oromion
  • 384
  • So you have 2 solutions for the problem. Then what's the question? The code in the two cases doesn't appear to do the same thing, however, so I don't really understand how these are equivalent. – cfr May 28 '18 at 01:31
  • I read your other question and the answers there, too. I guess you're still asking the same thing, but I'm still not clear what that is. – cfr May 28 '18 at 02:13
  • My attempt do not answer all three cases. Please compile the file. It fails in the 3rd case. – Oromion May 28 '18 at 02:49

3 Answers3

2

The syntax is with 3 optional arguments

\fun[<case>][<after colon>][<after right arrow>]

If only one optional argument is used, the predefined case will be invoked via \csname funcase#1\endcsname. If more than one optional argument is used, then the additional optional arguments are used to populate the fields, overriding the default case.

The MWE:

\documentclass{article}
\newcommand\fun[1][f]{\def\funcase{#1}\funauxA}
\newcommand\funauxA[1][\relax]{%
  \ifx\relax#1\relax\csname funcase\funcase\endcsname\else%
    \def\funargA{#1}\expandafter\funauxB%
  \fi%
}
\newcommand\funauxB[1][*]{%
  \funcase\colon\mathcal{\funargA}\rightarrow\mathcal{#1}
}
\def\funcasef{f\colon\mathcal{A}\rightarrow\mathcal{B}}
\def\funcaseg{g\colon\mathcal{C}\rightarrow\mathcal{D}}
% ADD MORE CASES HERE
\def\funcasem{m\colon\mathcal{Q}\rightarrow\mathcal{R}}
\begin{document}
    $\fun$ %$f\colon\mathcal{A}\rightarrow\mathcal{B}$

    $\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[m]$

$\fun[g][E][T]$

    $\fun[h][X][Y]$ %h\colon\mathcal{X}\rightarrow\mathcal{Y}
\end{document}

enter image description here

1

Use G arguments.

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand{\fun}{ G{f} G{C} G{D} }{%
  #1\colon\mathcal{#2}\rightarrow\mathcal{#3}%
}

\begin{document}

$\fun$

$\fun{g}$

$\fun{h}{X}{Y}$

\end{document}

enter image description here

Henri Menke
  • 109,596
  • Thanks, near, but with $\fun$ I would like take $f\mathcal{A}\rightarrow\mathcal{B}$, not $f\colon\mathcal{C}\rightarrow{D}$ – Oromion May 28 '18 at 02:47
1

Do you want this?

$\fun \quad \fun{f}\quad \fun{f}{N}{R}$

$\fun{g} \quad \fun{g}{M}$

$\fun{h}{X}{Y}\quad \fun{h}\quad \fun{h}{W}{Q}$

produces

possible target

[Suggested by your comment on Henri Menke's answer.]

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand{\fun}{ G{f} g g }{%
  \def\tempa{f}\def\tempb{#1}\def\tempc{g}%
  \IfValueTF{#2}{\def\tempe{#2}}{%
    \ifx\tempa\tempb\def\tempe{A}%
    \else\ifx\tempb\tempc\def\tempe{C}%
    \else\def\tempe{X}\fi\fi
  }%
  \IfValueTF{#3}{\def\tempf{#3}}{%
    \ifx\tempa\tempb\def\tempf{B}%
    \else\ifx\tempb\tempc\def\tempf{D}%
    \else\def\tempf{Y}\fi\fi
  }%
  #1\colon\mathcal{\tempe}\rightarrow\mathcal{\tempf}%
}

\begin{document}

$\fun \quad \fun{f}\quad \fun{f}{N}{R}$

$\fun{g} \quad \fun{g}{M}$

$\fun{h}{X}{Y}\quad \fun{h}\quad \fun{h}{W}{Q}$

\end{document}
cfr
  • 198,882
  • The examples are illustrative, now I can use it extensively in my tex's class notes, I'm sorry for not explaining it clearly. This answer is what I wanted, a compact notation, for $f$ (command alone), for $g$ command with single parameter and the general case. Even shown one in which the first set remains fixed and the second varies.

    Thank you to all, I am very happy!

    – Oromion May 28 '18 at 04:06
  • @Oromion You should consider accepting the answer which helps you most. You should definitely look at https://tex.stackexchange.com/a/433790/, which may be preferable to my solution, at least if you want to define many more set cases. – cfr May 28 '18 at 20:19