GeneralNew Command with Arguments in Parentheses

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
hkyz
Posts: 9
Joined: Wed Apr 25, 2012 9:37 am

New Command with Arguments in Parentheses

Post by hkyz »

I want to overload a definition with three variations using arguments in parenthesis. Something like the non-working code below:

Code: Select all

\documentclass{article}

\def\f{F}
\def\f(#1){F(#1)}
\def\f(#1,#2){F_{#1}^{#2}}

\begin{document}

$\f$
$\f(1)$
$\f(1,2)$

\end{document}
Any ideas on how to do such a thing without too much effort?

Thanks.

Recommended reading 2024:

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

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

New Command with Arguments in Parentheses

Post by sommerfee »

Code: Select all

\documentclass{article}
\begin{document}

\makeatletter
\def\f{\@ifnextchar(\@f F}
\def\@f(#1){\@@f(#1,,)}
\def\@@f(#1,#2,#3){%
  \ifx\relax#2\relax
    F(#1)%
  \else
    F_{#1}^{#2}%
  \fi}
\makeatother

$\f$
$\f(1)$
$\f(1,2)$

\end{document}
For explanation see http://www.ctan.org/pkg/texbytopic
hkyz
Posts: 9
Joined: Wed Apr 25, 2012 9:37 am

Re: New Command with Arguments in Parentheses

Post by hkyz »

Thanks for the answer.
Post Reply