My question is about reading and using the arguments of a custom created command using \def
.
Let's say I have the command \mytestcommand
and I want to read and use it's arguments (optional and mandatory of unknown number)
I have the code:
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\newcounter{NumOptionalArgFound}
\newcounter{NumMandatoryArgFound}
\xdef\MaxMandArgs{3}
\makeatletter
\def\SearchForOptional{%
\@ifnextchar[%
{\FoundOptional}
{\relax}
}
\def\FoundOptional[#1]{%
\stepcounter{NumOptionalArgFound}%
\xdef\curOptArg{#1}%
\global\expandafter\let\csname OptionalArgument\arabic{NumOptionalArgFound}\endcsname\curOptArg%
\@ifnextchar[%
{\FoundOptional}
{\SearchForMandatory}
}
\def\SearchForMandatory{%
\@ifnextchar\bgroup%
{\CheckIfMandatory}
{\relax}
}
\def\CheckIfMandatory#1{%
\ifnum\pdf@strcmp{#1}{\bgroup}=0%
#1%
\else
\ifnum\arabic{NumMandatoryArgFound}<\MaxMandArgs%
\stepcounter{NumMandatoryArgFound}%
\xdef\curManArg{#1}%
\global\expandafter\let\csname MandatoryArgument\arabic{NumMandatoryArgFound}\endcsname\curManArg%
\expandafter\expandafter\expandafter\SearchForMandatory%
\else
#1%
\fi
\fi
}
\makeatother
\def\mytestcommand{
\xdef\MaxMandArgs{2}
\SearchForOptional
}
\def\printArgs{
\foreach \i in {1,...,\theNumOptionalArgFound}{
Opt Arg \i: \csname OptionalArgument\i\endcsname\par
}
\foreach \i in {1,...,\theNumMandatoryArgFound}{
Mand Arg \i: \csname MandatoryArgument\i\endcsname\par
}
}
\begin{document}
\mytestcommand[Optional 1][Optional Second]{first Mand}{second Mand}{thirdMand}\bgroup noMand \egroup
\printArgs
\end{document}
This code works fine and finds-saves the optional and mandatory arguments of ot. My problem is that if i try to use one of the counters or of the arguments inside the definition of \mytestcommand
it fails to find the number or the argument.
More specific, I would like to use \printArgs
command inside of \mytestcommand
definition... (So I can know I am able to use the arguments inside my command definition).
The problem appears if I change the definition of \mytestcommand
to:
\def\mytestcommand{
\xdef\MaxMandArgs{2}
\SearchForOptional
\printArgs
}
Thank's=Thanks
PS: I am looking for a LaTeX2e solution, but LaTeX3 is probably helpful for other users... I would like not to use packages that I know exist for such jobs but if possible, "primary" LaTeX commands like mine...
\def
was just that you have already given up on a 2e approach. That's perfectly sensible, but then you are going the TeX route as opposed to the LaTeX one offered by L3. That will generally mean less readable code and the need to take much more responsibility for checks. And spurious spaces, of course.