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.
4 Answers
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}
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
\fff \quad \fff[][][]\quad \fff[9]\quad \fff[][Z] \\
\fff[][][5]\quad \fff[3][B][4]\quad \fff
\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.– MGMKLMLJul 27, 2015 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)– MGMKLMLJul 31, 2015 at 1:22
-
@MGMKLML: I don't use
xparse
in my solution. Onlyxkeyval
, although usingkeyval
would suffice (with some change in the code).– Werner ♦Jul 31, 2015 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.– MGMKLMLJul 31, 2015 at 11:34
-
\newcommand{name-of-command}[number of options]{definition}
\newcommand{\eee}{\stackrel{1\to2}{l}}
, then add this new command to where you want ...