8

This is a second question on this macro definition: Defining a macro with three optional arguments in the form \newmacro{a}{b}[c]{d}[e][f] and \newmacro{a}{b}[c]{d}*[f]

To sum up: I had define a macro \funcdef for writing function declarations inside math-mode. This macro takes three compulsory arguments and three optional arguments, one of them is the notation the function takes. v.g.

\funcdef{f}{a}[A]{B}

means that f is the name of the function whose declaration I am making. a is the sample variable, and A and B are respectively domain and codomain. It renders as
f:A-->B \ a-->f(a)

However if I want to define a multiple-variable domain, I will use it as:

\funcdef{f}{(a,b)}[A\times B]{C}[f(a,b)]

So I get the correct form
f:AxB-->C \ (a,b)-->f(a,b)

I have to use the optional argument to avoid the undesirables
*wrong f(a,b)

\funcdef{f}{a,b}[A\times B]{C}
\funcdef{f}{(a,b)}[A\times B]{C}

The original idea of this optional parameter was to provide an alternative notation to the standard functional notation, compare, for example:
a times b

\funcdef{\times}{(a,b)}[A\times A]{A}[a\times b]
\funcdef{\times}{(a,b)}[A\times A]{A}

The question:

When using standard functional notation on a multiple variable domain, I would like to either add or remove the parenthesis, so that the code is either

\funcdef{f}{a,b}[A\times B]{C}

or

\funcdef{f}{(a,b)}[A\times B]{C}

An I would get the result as
f:AxB-->C \ (a,b)-->f(a,b)


For a minimal functional code of these examples:

\documentclass{article}
\makeatletter
\newcommand\funcdef[2]{\def\func@name{#1}\def\func@var{#2}\begin{array}{r@{\ }c@{\,}c@{\,}l}#1:\func@dom}
\newcommand\func@dom[2][\@empty]{&\ifx#1\@empty#2\else#1\fi&\to&#2\\&\func@var&\mapsto&\func@use}
\newcommand\func@use{\@ifstar\func@use@\func@usei}
\newcommand\func@use@{\func@name(\func@var)\func@def}
\newcommand\func@usei[1][\@empty]{\ifx#1\@empty\func@name(\func@var)\else#1\fi\func@def}
\newcommand\func@def[1][\@empty]{\ifx#1\@empty\relax\else\mathrel{:=}#1\fi\end{array}}
\makeatother
\begin{document}
$$\funcdef{f}{a}[A]{B}$$

$$\funcdef{f}{(a,b)}[A\times B]{C}[f(a,b)]$$

$$\funcdef{f}{a,b}[A\times B]{C}$$
$$\funcdef{f}{(a,b)}[A\times B]{C}$$

$$\funcdef{\times}{(a,b)}[A\times A]{A}[a\times b]$$
$$\funcdef{\times}{(a,b)}[A\times A]{A}$$
\end{document}
1

1 Answer 1

8

Just modify the definition of the key-value interface and add a couple of checks. Note that, if the list of variables contains a comma, you have to type

variables={a,b},

for setting the value.

\documentclass{article}
\usepackage{amsmath,keyval}

\makeatletter
\newcommand{\funcdef@key}[1]{%
  \define@key{funcdef}{#1}{\@namedef{cet@#1}{##1}}%
  \expandafter\let\csname cet@#1\endcsname\@empty
}
\funcdef@key{name}
\funcdef@key{domain}
\funcdef@key{codomain}
\funcdef@key{variable}
\funcdef@key{variables}
\funcdef@key{notation}
\funcdef@key{definition}
\newcommand{\funcdef@check}[1]{%
  \expandafter\ifx\csname cet@#1\endcsname\@empty
    \@latex@error{Missing `#1'}{Provide `#1'}%
  \fi
}

\newcommand{\funcdef}[1]{%
  \begingroup
  \setkeys{funcdef}{#1}%
  \ifx\cet@codomain\@empty\let\cet@codomain\cet@domain\fi
  \funcdef@check{name}%
  \funcdef@check{domain}%
  \ifx\cet@variables\@empty
    \funcdef@check{variable}%
  \fi
  \begin{array}{l@{}r@{}l@{}l}
  \cet@name\colon{} & 
  \cet@domain &
  {}\to \cet@codomain \\ &
  \ifx\cet@variable\@empty
    (\cet@variables)
  \else
    \cet@variable
  \fi &
  {}\mapsto
  \ifx\cet@notation\@empty
    \cet@name(
      \ifx\cet@variable\@empty
        \cet@variables
      \else
        \cet@variable
      \fi
    )
  \else
    \cet@notation
  \fi
  \ifx\cet@definition\@empty
    \expandafter\@gobble
  \else
    \expandafter\@firstofone
  \fi
  {& {}\mathrel{:}=\cet@definition}
  \\
  \end{array}
  \endgroup
}
\makeatother

\begin{document}

\noindent
Simple function:
\[
\funcdef{name=f,variable=x,domain=\mathbf{R}}
\]
Simple function with declaration:
\[
\funcdef{
  name=f,
  variable=x,
  domain=\mathbf{R},
  notation=x^2
}
\]
Function with alternative writing:
\[
\funcdef{
  name=\exp,
  variable=x,
  domain=\mathbf{R},
  notation=e^x
}
\]
Function with alternative writing and declaration:
\[
\funcdef{
  name=\exp,
  variable=x,
  domain=\mathbf{R},
  notation=e^x,
  definition=\lim\limits_{n\to\infty}\left(1+\frac xn\right)^n
}
\]
Function with different domain and codomain:
\[
\funcdef{
  name=\operatorname{sqrt},
  variable=n,
  domain=\mathbf{N},
  codomain=\mathbf{R}
}
\]
Function with different domain and codomain, and alternative
writing:
\[
\funcdef{
  name=\operatorname{sqrt},
  variable=n,
  domain=\mathbf{N},
  codomain=\mathbf{R},
  definition=\sqrt{n}
}
\]
Function with different domain and codomain, alternative writing
and declaration:
\[
\funcdef{
  name=\operatorname{sqrt},
  variable=n,
  domain=\mathbf{N},
  codomain=\mathbf{R},
  notation=\sqrt{n},
  definition=\exp\bigl(\frac{1}{2}\ln n\bigr)
}
\]
Function of two variables:
\[
\funcdef{
  name=f,
  variables={a,b},
  domain=A\times B,
  codomain=C,
}
\]
\end{document}

I show only the last one.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .