In a book I'm writing, I use the \IfStrEqCase
from the xstring
package in a command to organize access to several pieces of information I need throughout the document, without having to define a separate command for each. One of the keys is the edition/version of the book. Obviously, as this is liable to change, it's convenient to have this data localized to a single place.
In the colophon of the book, I have a copyright notice which includes the name of the book and the aforementioned edition number. I'm a big fan of the nth
package, and use it elsewhere in my document. However, when I try to combine it with my command, I get compilation errors. Here's an MWE which demonstrates the error:
\documentclass{article}
\usepackage{xstring}
\usepackage{nth}
\DeclareRobustCommand{\Gkeys}[1]{%
\IfStrEqCase{#1} {%
{edition}{10}%
{author}{John Doe}%
{title}{Title of My Book}%
}[\textbf{??key not found??}]%
}
\begin{document}
\textbf{\Gkeys{title}}, \textsc{\nth{\Gkeys{edition}} Edition}\\
\Gkeys{author} 2017
\end{document}
When I try to compile this document with pdflatex
I get the following error:
! Missing number, treated as zero.
<to be read again>
\protect
l.16 ...}}, \textsc{\nth{\Gkeys{edition}} Edition}
\\
?
I'm not sure what's going here to make this not work. If I define a simple command, like say \def\BookEditionNumber{10}
, this works fine, but obviously that loses the convenience of the \Gkeys
command I had declared.
EDIT:
A comment pointed out that since I declared \GKeys
with \DeclareRobusCommand
, I rendered it unexpandable, and thus \nth
can't do anything with it. My impetus for doing so was to allow it to work with commands like \section
. However, out of curiosity, I changed the command to be declared with \newcommand
instead. Interestingly, this also failed to work. When I tried to compile that document, I got the following error:
! Missing number, treated as zero.
<to be read again>
\let
l.16 ...}}, \textsc{\nth{\Gkeys{edition}} Edition}
\\
?
\GKeys
as robust command, this isn't expandable and\nth
can do nothing here – user31729 Mar 14 '17 at 6:24\section
. Is there any way to get it both ways? – Kevin Keith Mar 14 '17 at 6:26\IfStrEqCase
seems to be unexpandable as well, so even if\Gkeys
is not robust the next cause of mischief is\IfStrEqCase
– user31729 Mar 14 '17 at 6:28