1

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):

enter image description here

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}.

\section{Header with retrieval: \awruler{9}}
\end{document}
2
  • your \awruler command is fragile so use \protect\awruler – David Carlisle Jan 4 '18 at 15:07
  • you could also get rid of the awcounter and \setcounter commands and just use \newcommand{\awruler}[1]{\csname awruler\romannumber#1\endcsname} or \newcommand{\awruler}[1]{\csname awruler#1\endcsname} – David Carlisle Jan 4 '18 at 15:10
1

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}.

\section{Header with retrieval: \awruler{9}}
\end{document}
4
  • 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
  • Ah, I see. It is grabbing the token 9, rather than it being part of the macro name. I get it now! – K.G. Feuerherm Jan 4 '18 at 18:09
  • Tested on main file with multiple records and fields, works fine! – K.G. Feuerherm Jan 4 '18 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.