Quantcast
Channel: Defining a macro with three optional arguments in the form \newmacro{a}{b}[c]{d}[e][f] and \newmacro{a}{b}[c]{d}*[f] - TeX - LaTeX Stack Exchange
Viewing all articles
Browse latest Browse all 3

Defining a macro with three optional arguments in the form \newmacro{a}{b}[c]{d}[e][f] and \newmacro{a}{b}[c]{d}*[f]

$
0
0

I am defining a macro in LaTeX2e for writing function declarations.

I want that the macro has tree compulsory arguments: the name of the function (v.g f), a depiction of the type of variable it takes (v.g. x), and the space in which the function is defined (v.g. \mathbb R), as well as three optional arguments: the domain (if the domain is not the same as the codomain), the writing of the function applied to its variables, and the asignment/meaning of the function.

For the sake of clarity when using the macro I had decided for the following structure:

\funcdef{functionName}{variables}[domain]{codomain}[notation][definition]

To avoid ambiguity in the last two optional elements, when notation is given but no definition it takes the form:

\funcdef{functionName}{variables}[domain]{codomain}[notation]

And when definition is given but no notation:

\funcdef{functionName}{variables}[domain]{codomain}*[definition]

I came with the following code (both declaration and example), which is working but seems a little too complicated:

\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}Simple function: \[  \funcdef fx{\mathbf R}\]Simple function with declaration: \[  \funcdef fx{\mathbf R}*[x^2]\]Function with alternative writing: \[  \funcdef{\textrm{exp}}x{\mathbf R}[e^x]\]textrm{exp}Function with alternative writing and declaration: \[  \funcdef\exp x{\mathbf R}[e^x][\lim\limits_{n\to\infty}%    \left(1+\frac xn\right)^n]\]Function with different domain and codomain: \[  \funcdef{\textrm{sqrt}}n[\mathbf N]{\mathbf R}\]Function with different domain and codomain, and alternativewriting: \[  \funcdef{\textrm{sqrt}}n[\mathbf N]{\mathbf R}[\sqrt n]\]Function with different domain and codomain, alternative writing     and declaration: \[  \funcdef{\textrm{sqrt}}n[\mathbf N]{\mathbf R}[\sqrt n][\exp(\frac12\ln n)]\]\end{document}

Compilation result

Is there any other way to define in LaTeX2e these kind of macros that does not require too many supporting macros (5 in this sample) and temporal definitions (2 in this sample)?

How would it work in pain-TeX or LaTeX3? (answers to this will be upvoted but not accepted)


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images