Math & ScienceA single command with a different number of arguments

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

A single command with a different number of arguments

Post by svend_tveskaeg »

Hi all.

Consider the following MWE:

Code: Select all

\documentclass{article}

\usepackage{mathtools}
\usepackage{xparse}

\makeatletter
\def\resetMathstrut@{%
  \setbox\z@\hbox{%
    \mathchardef\@tempa\mathcode`\[\relax
    \def\@tempb##1"##2##3{\the\textfont"##3\char"}%
    \expandafter\@tempb\meaning\@tempa \relax
  }%
  \ht\Mathstrutbox@\ht\z@ \dp\Mathstrutbox@\dp\z@}
\makeatother
\begingroup
  \catcode`(\active \xdef({\left\string(}
  \catcode`)\active \xdef){\right\string)}
\endgroup
\mathcode`(="8000 \mathcode`)="8000

\newcommand{\testA}[2]{{\mkern -#1mu}#2{\mkern -#1mu}}
\newcommand{\testB}[4]{{\mkern -#1mu}#2^{\mkern -#3mu #4}}

\begin{document}

\begin{equation}
\testA{4}{(\frac{1}{3}a)} \qquad \testB{4}{(\frac{1}{3}a)}{6}{n}
\end{equation}

\end{document}
(where the code to the automatic scaling of parentheses in mathmode is taken from this TeX Stackexchange post).

I would like to make \testA and \testB into a single command, say \test, with a different number of arguments. I have a feeling that the \NewDocumentCommand command from the xparse package can do the trick, but I cannot get it to work.

How do I do this?

Thank you in advacnce!
Last edited by svend_tveskaeg on Sat Sep 15, 2012 2:53 pm, edited 4 times in total.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

A single command with a different number of arguments

Post by cgnieder »

How about this:

Code: Select all

\documentclass{article}

\usepackage{mathtools}
\usepackage{xparse}

\makeatletter
\def\resetMathstrut@{%
  \setbox\z@\hbox{%
    \mathchardef\@tempa\mathcode`\[\relax
    \def\@tempb##1"##2##3{\the\textfont"##3\char"}%
    \expandafter\@tempb\meaning\@tempa \relax
  }%
  \ht\Mathstrutbox@\ht\z@ \dp\Mathstrutbox@\dp\z@}
\makeatother
\begingroup
  \catcode`(\active \xdef({\left\string(}
  \catcode`)\active \xdef){\right\string)}
\endgroup
\mathcode`(="8000 \mathcode`)="8000

\newcommand{\testA}[2]{{\mkern -#1mu}#2{\mkern -#1mu}}
\newcommand{\testB}[4]{{\mkern -#1mu}#2^{\mkern -#3mu #4}}

\NewDocumentCommand\test{mmgg}
  {%
    {\mkern -#1mu}#2%
    \IfNoValueF{#3}{^}%
    {\mkern -\IfNoValueTF{#3}{#1}{#3}mu \IfNoValueF{#4}{#4}}%
  }

\begin{document}

\begin{equation}
\testA{4}{(\frac{1}{3}a)} \qquad \testB{4}{(\frac{1}{3}a)}{6}{n}
\end{equation}

\begin{equation}
 \test{4}{(\frac{1}{3}a)} \qquad \test{4}{(\frac{1}{3}a)}{6}{n}
\end{equation}

\end{document}
The argument identifier g is an optional argument used with {}. In the command code you can test with \IfNoValueT, \IfNoValueF or \IfNoValueTF what should happen if an argument is or isn't given.

Regards
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

A single command with a different number of arguments

Post by svend_tveskaeg »

Spot on.

Thank you very much!

Update.
A couple of formatting questions for the inline text:

(1) How do you get the typewriter font for the command names? [Solved.]
(2) What colour do you use for the command names? [Solved.]
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
Post Reply