I am new to TeX and LaTeX, and I am having trouble defining and using macros that define other macros. I've read about how to do so (for example, https://en.wikibooks.org/wiki/LaTeX/Plain_TeX#Plain_TeX_macros, as well as various previous questions here on this site), but I just haven't been able to get it to work.
Consider the following intentionally verbose snippet:
\def\apcodeEWR {EWR}
\def\apnameEWR {Newark Liberty International Airport}
\def\apcityEWR {Newark, NJ, USA}
\def\apcodeORY {ORY}
\def\apnameORY {Paris Orly Airport}
\def\apcityORY {Orly, France}
\def\apdescriptionEWR {\apnameEWR, located in \apcityEWR, has the airport code \apcodeEWR.}
\def\apdescriptionORY {\apnameORY, located in \apcityORY, has the airport code \apcodeORY.}
\begin{itemize}
\item \apdescriptionEWR
\item \apdescriptionORY
\end{itemize}
This outputs what I would expect:
* Newark Liberty International Airport, located in Newark, NJ, USA, has the airport code EWR.
* Paris Orly Airport, located in Orly, France, has the airport code ORY.
But I would like to instead do something like:
\apdefine{EWR}{Newark Liberty International Airport}{Newark, NJ, USA}
\apdefine{ORY}{Paris Orly Airport}{Paris, France}
\begin{itemize}
\apdescribe{EWR}
\apdescribe{ORY}
\end{itemize}
And I'd like to get past that hurdle first, but I feel I should just mention that ultimately, I'd really like to do something like this:
\begin{itemize}
\foreachairport\apdescribe
\end{itemize}
So, now I'm trying to make that "apdefine" macro. Based on the book that I linked to earlier, I'm trying this (having previously tried a bunch of other things):
\def\apdefine#1#2#3 {
\expandafter\def\csnameapcode#1\endcsname {##1}
\expandafter\def\csnameapname#1\endcsname {##2}
\expandafter\def\csnameapcity#1\endcsname {##3}
}
That compiles without error. But then I try to use it:
\apdefine{IBA}{Ibadan Airport}{Ibadan, Oyo, Nigeria}
I get nine errors, all on that line. They are three groups of the following three:
Undefined control sequence. ...IBA}{Ibadan Airport}{Ibadan, Oyo, Nigeria}
Missing control sequence inserted. ...IBA}{Ibadan Airport}{Ibadan, Oyo, Nigeria}
Illegal parameter number in definition of \inaccessible. ...IBA}{Ibadan Airport}{Ibadan, Oyo, Nigeria}
What am I doing wrong? How do I do it correctly? Or if there's a fundamentally different approach that can be used to do what I want, what's that? Thanks.
\csname
. Your other problem is that the##
in your\def
would become a single#
, so that your command would become\def\apacodeEWR{#1}
. The existing answers handle this problem (and nicely describe how you can achieve your ultimate goal). – Teepeemm Jul 1 '19 at 20:09