# How to make a command with three optional arguments?

I'm sorry for my English, but, anyway, I'd like to know how to create a new command with three optional arguments? I wanna get what is shown on the picture. I have some other ways how to achieve that, but they are not so comfy as I want. I tried to use twoopt package but there're only two possible optional arguments so it didn't satisfy me. I would like that "1", "2" and "l" to be optional so that I just could put in the text something like \fff and get exactly what is on the picture or, e.g., to put \fff[][][3] and get what I need. Are there any ways to make what I'm asking about? Thank you.

• You can make a new command with 3 optinal arguments with\newcommand{name-of-command}[number of options]{definition} – juanuni Jul 27 '15 at 17:51
• No, you're wrong. This allows you to create a command with only ONE optional arguments and from 1 to 9 mandatory arguments. – MGMKLML Jul 27 '15 at 17:55
• Not quite. One optional and up to 8 mandatory. Remember #1 still counts at we can have at most up to #9 – daleif Jul 27 '15 at 18:00
• Have a look at the xparse package, then this is easy. But you still need a lot of typing because of the empty []'s. Might be better with a key-val based interface. – daleif Jul 27 '15 at 18:03
• @MGMKLML maybe, I don't understand yet, but if you try making another command for 1,2 and l, something like \newcommand{\eee}{\stackrel{1\to2}{l}}, then add this new command to where you want ... – juanuni Jul 27 '15 at 18:06

Here a possible solution

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\DeclareDocumentCommand\fff { O{1} O{A} O{2} } {#1 \underset{l}{\overset{#2}{\to}} #3 }
\begin{document}
$\fff \quad \fff[3][B][4]$
\end{document}

• Thanks, I'll try. That's exactly what I wanted to get. :-) – MGMKLML Jul 27 '15 at 18:34

Here a solution without xparse - actually it exhibits how superior xparse is compared with the traditional approach:

\documentclass[10pt]{article}
\usepackage{amsmath}
\makeatletter

\def\fff{%
\bgroup
\@ifnextchar[{\@fff}{\@fff[]}%
}

\def\@fff[#1]{%
\ifx\\#1\\%
\def\@fffi{1}%
\else
\def\@fffi{#1}%
\fi
\@ifnextchar[{\@@fff}{\@@fff[]}%
}

\def\@@fff[#1]{%
\ifx\\#1\\%
\def\@fffii{A}%
\else
\def\@fffii{#1}%
\fi
\@ifnextchar[{\@@@fff}{\@@@fff[]}%
}

\def\@@@fff[#1]{%
\ifx\\#1\\%
\def\@fffiii{2}%
\else
\def\@fffiii{#1}%
\fi
\ensuremath{\@fffi
\underset{l}{\overset{\@fffii}{\to}}\@fffiii}%
\egroup
}
\makeatother

\begin{document}
\noindent
\end{document}


You can define something like this:

\newcommand{\parcial}[2]{\frac{\partial #1}{\partial #2}}


to make

\parcial{f}{x}:


Note that [2] is for the quantity of variables. Note the order of variables too. You can play with this code to make your own new commands.

• Again, this is the way where I gotta write arguments everytime. I would like to get the command where I don't have to put arguments if it's not needed. – MGMKLML Jul 27 '15 at 17:58

It's almost always more convenient to use a key-value approach when trying to handle more than one optional argument. Here's an implementation using xkeyval:

\documentclass{article}

\usepackage{xkeyval,amsmath}

\makeatletter
\define@cmdkey{myfam}{left}{}
\define@cmdkey{myfam}{right}{}
\define@cmdkey{myfam}{bottom}{}

\newcommand{\fff}[2][]{%
\setkeys{myfam}{%
left=1,right=2,bottom=l,% Defaults
#1}%
\underset{%
\overset
{\cmdKV@myfam@left \rightarrow \cmdKV@myfam@right}
{\scriptscriptstyle \cmdKV@myfam@bottom}%
}{#2}
}
\makeatother

\begin{document}

$\fff{A} \quad \fff[left=a]{A} \quad \fff[right=b]{A} \quad \fff[left=c,right=d]{A} \quad \fff[left=e,right=f,bottom=h]{A}$

\end{document}


I've made one argument to \fff mandatory, but that can also be hard-coded in the function, or made a key-value option.

Reference: How to create a command with key values?

• Thank you. It looks nice, but I hardly understand the principles of creating such commands with xparse). There're so many unknown things for me) – MGMKLML Jul 31 '15 at 1:22
• @MGMKLML: I don't use xparse in my solution. Only xkeyval, although using keyval would suffice (with some change in the code). – Werner Jul 31 '15 at 1:29
• Excuse me, but where can I read about the xkeyval and about creating such things by xkeyval? It is very desirable to read somewhere where it's clear and evident. – MGMKLML Jul 31 '15 at 11:34
• @MGMKLML: You should read the linked post in my answer. – Werner Jul 31 '15 at 15:10