Discussion:
Defining commands with \def giving parameter default values
(too old to reply)
Leonardo Freitas
2004-08-05 14:12:35 UTC
Permalink
Hello,

I wanted to define the following command

\newcommand{\mycmd}[1][1]{\addtocounter{myctr}{#1}}

It is supposed to increase the value of myctr
by the ammount passed as a parameter where 1
is given as the default value.

LaTeX wasn't happy with this definition.
It gives an error message saying

""

and I cannot understand it.

1) Do someone know about this error, its reasons and the
proper way to fix it??

Anyway, I then tried something in TeX as

\def\mycmd#1{\addtocounter{myctr}{#1}}

This works fine but it does not have the default
value I need on the definition.

2) What should do to define default values in \def marcos
that are defining new commands with parameters??

Best Regards,
Leo
Rasmus Villemoes
2004-08-05 14:34:14 UTC
Permalink
Post by Leonardo Freitas
Hello,
I wanted to define the following command
\newcommand{\mycmd}[1][1]{\addtocounter{myctr}{#1}}
It is supposed to increase the value of myctr
by the ammount passed as a parameter where 1
is given as the default value.
Well, the following works for me:

\documentclass[a4paper,12pt]{minimal}

\newcommand{\mycmd}[1][1]{\addtocounter{myctr}{#1}}
\newcounter{myctr}

\setcounter{myctr}{0}

\begin{document}
The initial value is \themyctr. Now we increase it by a large
number\mycmd[5] and the result is \themyctr. Let's increase it a
little bit\mycmd\ and get the result \themyctr.
\end{document}

Perhaps you forgot that by this construction you have to give the
argument in brackets and not in braces?

Regards

Rasmus

--
David Carlisle
2004-08-05 14:46:40 UTC
Permalink
Post by Leonardo Freitas
I wanted to define the following command
\newcommand{\mycmd}[1][1]{\addtocounter{myctr}{#1}}
It is supposed to increase the value of myctr
by the ammount passed as a parameter where 1
is given as the default value.
LaTeX wasn't happy with this definition.
It gives an error message saying
""
The definition looks OK to me, did you forget to use [] when you
use \myctr (as it has an optional argument)

see:


*\newcounter{myctr}

*\newcommand{\mycmd}[1][1]{\addtocounter{myctr}{#1}}

*\mycmd[4]

*\makeatletter
Post by Leonardo Freitas
4.
<*> \showthe\***@myctr

?

*\mycmd \relax
Post by Leonardo Freitas
5.
<*> \showthe\***@myctr

? x
No pages of output.
Transcript written on texput.log.
bash-2.05b$


David

Loading...