Re: Macro to write new commands?



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
.



Relevant Pages

  • Re: automate journal abbreviations?
    ... I have full titles in the .bib file. ... abbreviation that matches. ... % Here, a macro is created for each journal title, storing the ... it definitely points me in a useful direction. ...
    (comp.text.tex)
  • Re: Macro to write new commands?
    ... then use the abbreviation from then on. ... LaTeX command to do it: ... | ...rescued by the Boston Fire Department... ... You are wasting a counter for each acronym. ...
    (comp.text.tex)
  • Re: automate journal abbreviations?
    ... I have full titles in the .bib file. ... abbreviation that matches. ... % Here, a macro is created for each journal title, storing the ... For standard BibTeX, ...
    (comp.text.tex)
  • Re: automate journal abbreviations?
    ... I have full titles in the .bib file. ... abbreviation that matches. ... % Here, a macro is created for each journal title, storing the ... For standard BibTeX, ...
    (comp.text.tex)
  • Re: setf with two lists
    ... is another abbreviation" "This is yet a third abbreviation"). ... to associate the corresponding expansion to the abbreviation. ... available to you at compile time* because that is when the macro needs the data. ... You can't do this with a macro if the data is not available until run-time without doing special tricks: the data the macro can see needs to be part of the source of your program. ...
    (comp.lang.lisp)