Re: Macro to write new commands?
- From: Enrico Gregorio <gregorio@xxxxxxxxxxxxx>
- Date: Thu, 21 Feb 2008 16:29:09 +0100
Joel J. Adamson <jadamson@xxxxxxxxxxxx> wrote:
Howdy LaTeXers,
I work writing scientific papers all day and a common thing to do is to
spell out a future abbreviated piece of jargon with the abbreviation in
parentheses, then use the abbreviation from then on.
Instead of doing this in my favorite editor (any guesses?) I wrote a new
LaTeX command to do it:
\usepackage{ifthen}
\newcounter{bfdabbrev}
\newcommand{\bfd}
{
\ifthenelse{\equal{\value{bfdabbrev}}{0}}
{Boston Fire Department (BFD)}
{BFD}
\addtocounter{bfdabbrev}{1}
}
So the first time \bfd appears in a document, I get
,----
| ...rescued by the Boston Fire Department (BFD)...
`----
and all subsequent times, I get
,----
| ...contract negotiations with the BFD...
`----
This works perfectly. Of course I have about twenty abbreviations and I
want to write a package that can generate these new commands, so that I
can write something like
\abnew{cdabbrev}{\cd}{Conduct Disorder}{CD}
and have the new command \cd available.
I tried
\newcommand{\abnew}[4]{
\newcounter{#1}
\newcommand{#2}
{
\ifthenelse{\equal{\value{#1}}{0}}
{#3 (#4)}
{#4}
\addtocounter{#1}{1}
}
}
But this didn't work: I got "missing \begin{document}." I can see
already that the scoping of the parameters is mixed up (the inner
"\newcommand" has no parameters, but the outer one has four).
How do I write a macro to write the macros?
You are wasting a counter for each acronym. The package acronym already
does what you ask. A quick implementation requires only to redefine
the macro at its first call:
\newcommand{\abnew}[3]{\newcommand{#1}{#2~(#3)\gdef#1{#3}}}
\abnew{\bfd}{Boston Fire Department}{BFD}
I use \gdef instead of \renewcommand, because the first occurrence
of the abbreviation macro might fall inside a group. Put all acronym
definitions in the preamble of your document.
Ciao
Enrico
.
- Follow-Ups:
- Re: Macro to write new commands?
- From: Joseph Wright
- Re: Macro to write new commands?
- References:
- Macro to write new commands?
- From: Joel J. Adamson
- Macro to write new commands?
- Prev by Date: Re: recent problem with TOC creation | hyperref
- Next by Date: Re: recent problem with TOC creation | hyperref
- Previous by thread: Macro to write new commands?
- Next by thread: Re: Macro to write new commands?
- Index(es):
Relevant Pages
|