1

I'm trying to build a macro that in-turn generates a couple related commands. Here's a really simplistic example:

\documentclass{article}

\newcommand{\myabbv}[3]{%
  \newcommand{#1}{#3}
  \newcommand{#1full}{#2}
}

\myabbv{\alky}{Alcoholics Anonymous}{AA}

\begin{document}
\alkyfull is a place for people that go to \alky
\end{document}

This chokes though, complaining I already defined \alky when I run the command to make the other commands. It seems like TeX doesn't concatenate the argument with my suffix; is there a fix?

Nick T
  • 141
  • Welcome to TeX.SX! Of course it chokes about the \aa# command. You should explain what you want to achieve with your \myabbv command. –  Nov 17 '14 at 20:15
  • @ChristianHupfer oops, looks like I simplified it too much...fixed \aa. As I mention initially I'm trying to make a command that creates a couple of related commands (varying with a suffix). – Nick T Nov 17 '14 at 20:18
  • \aa is already defined, it's for Scandinavian Angstr\"om characters... (or whatever they are called) Otherwise use the solutions by Peter Grill... he was (again) quicker than me :-( –  Nov 17 '14 at 20:23

1 Answers1

2

The best way to define new macros is to use the etoolboxs \csdef:

enter image description here

Notes:

  • Note that \aa is already defined., so you should use an alternate name. As \csdef won't issue an error in that case, I have added a test to make sure that the code issues an error if you attempt to redefine an existing macro.
  • You can also use \csname...\endcsname to build names of macros as shown in the second MWE.

Code: Using \csdef:

\documentclass{article}
\usepackage{etoolbox}

\newcommand{\myabbv}[3]{%
    %% First lets check that we are not redefining an exsting macro:
    \ifcsdef{#1}{\PackageError{myabbv}{Macro #1 is already defined}{}}{}%
    \ifcsdef{#1full}{\PackageError{myabbv}{Macro #1full is already defined}{}}{}%
    % --------------
    \csdef{#1}{#3}%
    \csdef{#1full}{#2}%
}

\myabbv{Xaa}{Alcoholics Anonymous}{AA}

\begin{document}
\Xaafull is a place for people that go to \Xaa
\end{document}

Code: Using \csname...\endcsname:

\documentclass{article}

\newcommand{\myabbv}[3]{%
  \expandafter\newcommand\csname#1\endcsname{#3}%
  \expandafter\newcommand\csname#1full\endcsname{#2}%
}

\myabbv{Xaa}{Alcoholics Anonymous}{AA}

\begin{document}
\Xaafull is a place for people that go to \Xaa
\end{document}
Peter Grill
  • 223,288