16

I've defined some setter and getter commands to facilitate entering complex content into my document. The basic definitions are as follows:

\newcommand\aeset[3]{\expandafter\newcommand\csname ae@#1@#2\endcsname{#3}}
\newcommand\aeget[2]{\csname ae@#1@#2\endcsname}

The problem I'm having is that if define the following

\aeset{this}{is}{an experiment}

and then later try to call it with

\aeget{that}{is}

The command just expands into a \relax without issuing a warning that

\ae@that@is

is not a defined control sequence. Is there a simple way to get around this without having to manually write all the testing arguments into my getter?

Werner
  • 603,163
A.Ellett
  • 50,533

2 Answers2

12

You can use \ifcsname...\endcsname, which tests both for an undefined or a \relax control sequence:

\documentclass{article}

\makeatletter
\newcommand\aeset[3]{\expandafter\newcommand\csname ae@#1@#2\endcsname{#3}}
\newcommand\aeget[2]{%
  \ifcsname ae@#1@#2\endcsname
    \csname ae@#1@#2\endcsname
  \else
    \@latex@warning{`\string\ae{#1}{#2}' does not exist}%
  \fi
}
\makeatother

\begin{document}

\aeset{this}{is}{an experiment}%

get:\aeget{that}{is}

\end{document}

The above produces the warning

LaTeX Warning: `\ae{that}{is}' does not exist on input line 18.
egreg
  • 1,121,712
Werner
  • 603,163
3
\documentclass{article}
\makeatletter
\newcommand\aeset[3]{\@namedef{ae@#1@#2}{#3}}
\newcommand*\aeget[2]{%
  \ifcsname ae@#1@#2\endcsname\@nameuse{ae@#1@#2}
    \else\@latex@warning{`\string\ae{#1}{#2}' does not exist} undefined!!!\fi}
\makeatother

\begin{document}    
    \aeset{this}{is}{an experiment} 

    get: \aeget{this}{is} 

    get: \aeget{that}{is}   
\end{document}

enter image description here