Document ClassesProblem in macro declaration

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
jlb
Posts: 2
Joined: Wed Sep 29, 2010 1:49 pm

Problem in macro declaration

Post by jlb »

Hi everybody

I am writing macros to help in the description of an API. I have one environment and one command
The command accumulate informations in a macro used by the closing part of the environment to display the informations in good order:

The environment is as follow :

Code: Select all

\newenvironment{arguments}{%
  \def\arglist{}
  \def\empty{}
  \def\argdesc{}
}
{
  {\tt \servicereturn\ \servicename(\arglist);}
  
  {\bf Arguments of \servicename\ are:}
  
  \argdesc
}
\argdesc and \arglist are filled by the following command

Code: Select all

\newcommand{\argument}[3]{%
  \edef\argdesc{\argdesc\par\begingroup\hangindent=2em\hangafter=1 #2 #3\par\endgroup}
  \ifx\arglist\empty
    \def\arglist{#1 #2}
  \else
    \edef\arglist{\arglist, #1 #2}
  \fi
}
The whole thing is used like that :

Code: Select all

\begin{arguments}
  \argument{TaskType}{TaskID}{the id of the task}
  \argument{EventMaskType}{Mask}{the event mask}
\end{arguments}
It works but when I try to add other macros in the

Code: Select all

  \edef\argdesc{\argdesc\par\begingroup\hangindent=2em\hangafter=1 #2 #3\par\endgroup}
definition, like :

\edef\argdesc{\argdesc\par\begingroup\hangindent=2em\hangafter=1 {\tt #2} #3\par\endgroup}
I get an error :
! Missing control sequence inserted.
<inserted text>
\inaccessible
l.73 \end{arguments}
If I use \texttt, I get a different error :
! Undefined control sequence.
<argument> \@nil

l.72 ...e}{TaskID}{The id of the task to activate}
I tried to use \protect before #2 but it does not change anything.

I am a little bit lost and need a TeX guru :oops:

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

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Problem in macro declaration

Post by gmedina »

Hi,

you can use a box to store the modification to the second argument before starting the grouping (I completed your code providing some dummy definition for \servicename and \servicereturn):

Code: Select all

\documentclass{article}
\usepackage{amsmath}
\usepackage{accents}

\newcommand\servicereturn{Service return}
\newcommand\servicename{Service name}

\newenvironment{arguments}{%
  \def\arglist{}
  \def\empty{}
  \def\argdesc{}
}
{
  {\ttfamily \servicereturn\ \servicename(\arglist);}
  
  {\bfseries Arguments of \servicename\ are:}
  
  \argdesc
}

\newsavebox\mybox
\newcommand{\argument}[3]{
  \sbox\mybox{\ttfamily #2}%
  \edef\argdesc{\argdesc\par\begingroup\hangindent=2em\hangafter=1 \usebox\mybox\ #3\par\endgroup}
  \ifx\arglist\empty
    \def\arglist{#1 #2}
  \else
    \edef\arglist{\arglist, #1 #2}
  \fi
}

\begin{document}

\begin{arguments}
  \argument{TaskType}{TaskID}{the id of the task}
  \argument{EventMaskType}{Mask}{the event mask}
\end{arguments}


\end{document}
I changed the obsolete commands \tt and \bf to \ttfamily, and \bfseries, respectively.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
jlb
Posts: 2
Joined: Wed Sep 29, 2010 1:49 pm

Re: Problem in macro declaration

Post by jlb »

Thanks a lot

It works almost

However, the last item saved in the box is replicated for every \argument

I suspect it is because the \edef is not expanded until \argdesc is expanded and at that time the box contains the last item.
Post Reply