4

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. enter image description here

15
  • You can make a new command with 3 optinal arguments with\newcommand{name-of-command}[number of options]{definition}
    – juanuni
    Commented Jul 27, 2015 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
    Commented Jul 27, 2015 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
    Commented Jul 27, 2015 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
    Commented Jul 27, 2015 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
    Commented Jul 27, 2015 at 18:06

4 Answers 4

2

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}
1
  • Thanks, I'll try. That's exactly what I wanted to get. :-)
    – MGMKLML
    Commented Jul 27, 2015 at 18:34
3

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}

enter image description here

0

You can define something like this:

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

to make

\parcial{f}{x}:

enter image description here

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.

1
  • 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
    Commented Jul 27, 2015 at 17:58
0

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:

enter image description here

\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?

4
  • 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
    Commented Jul 31, 2015 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
    Commented 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.
    – MGMKLML
    Commented Jul 31, 2015 at 11:34
  • @MGMKLML: You should read the linked post in my answer.
    – Werner
    Commented Jul 31, 2015 at 15:10

You must log in to answer this question.

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