# Including xifthen package breaks macro expansion in header

In the following MWE, I build a 'database' which I then query later in body text and in headers.

It works fine until I load the xifthen package. This is not used in the MWE, but I do use the \isempty{} feature it provides elsewhere in my real file.

With some searching I found this similar problem (i.e. same error message): Undefined control sequence when including pdfpages package but adding \noexpand before the (second) \setcounter didn't fix the problem.

Error (log file seems empty for some reason):

Can someone suggest a fix (ideally) other than working around xifthen?

\documentclass{memoir}

\usepackage{xifthen}        % PROBLEMATIC PACKAGE (ifthen works)

% Database counter
\newcounter{awcounter}

% Database creation command
\newcommand{\awset}[3]{% #1 specifies the Aw document number, #2 the field, and #3 the value
\setcounter{awcounter}{#1}%
\expandafter\def\csname aw#2\roman{awcounter}\endcsname{#3}%
}

% Build database for Aw no. 9
\awset{9}{ruler}{RULER}

% Database retrieval command
\newcommand{\awruler}[1]{% #1 specifies the Aw document number
\setcounter{awcounter}{#1}%
\expandafter\csname awruler\roman{awcounter}\endcsname%
}

\begin{document}
\mainmatter
\chapter{Chapter}
Here is some text with a retrieval: \awruler{9}.

\end{document}

• your \awruler command is fragile so use \protect\awruler – David Carlisle Jan 4 '18 at 15:07

You can simplify your definitions leaving them naturally robust and safe to use in a section heading:

\documentclass{memoir}

%\usepackage{xifthen}        % PROBLEMATIC PACKAGE (ifthen works)  (must be the authors:-)

% Database counter
%\newcounter{awcounter}

% Database creation command
\newcommand{\awset}[3]{% #1 specifies the Aw document number, #2 the field, and #3 the value
%    \setcounter{awcounter}{#1}%
\expandafter\def\csname aw#2#1\endcsname{#3}%
}

% Build database for Aw no. 9
\awset{9}{ruler}{RULER}

% Database retrieval command
\newcommand{\awruler}[1]{% #1 specifies the Aw document number
\csname awruler#1\endcsname
}

\begin{document}
\mainmatter
\chapter{Chapter}
Here is some text with a retrieval: \awruler{9}.


• I never liked the use of a counter, but it was code I borrowed and I didn't know of an alternative. Works like a charm on my end on the MWE, will try the main file and report back. I am puzzled, though, I thought that macro names couldn't contain digits (hence the roman conversion)? or is that exempted due to csname bracketing? (I've been very frustrated by not being able to create simple macros with digits at the end thus far and I've now tested that a call to \awruler9 in my LaTeX code works just fine.... – K.G. Feuerherm Jan 4 '18 at 16:28
• @K.G.Feuerherm \awruler9 is \awruler{9} so works but of course you need the braces if there is more than one digit. \awruler{10} – David Carlisle Jan 4 '18 at 16:33