5

I am trying to do what I though was fairly simple - use an xargs defined macro in a CS. MWE:

\documentclass{article}
\usepackage{xargs}
\usepackage{etoolbox}
\makeatletter
\def\abx@foo@baz@bar{test}
\def\abx@field@original@default@test{baz}

\newcommandx*{\blx@imc@thefield}[3][1=original,2=default]{\csuse{abx@field@#1@#2@#3}}

\def\blx@getformat#1#2{%
  \ifcsundef{abx@#1@\blx@imc@thefield{test}@#2}
    {no}
    {yes}}

\begin{document}
\blx@getformat{foo}{bar}
\makeatother
\end{document}

For me, this always gives:

! Missing \endcsname inserted.
<to be read again> 
                   \begingroup 
l.17 \blx@getformat{foo}{bar}

I cannot for the life of me work out why. Running it through tracing, it dies in an internal xargs macro. The \abx@field@original@default@test macro works fine on its own, it just doesn't like being in the \ifcsundef test. If I replace the xargs \newcommandx with a plain \def (without optional args, of course), it works. I've tried protecting the newcommandx call, tried passing optional args with defaults to the macro etc. but always get the same error. Any ideas much appreciated.

1 Answer 1

6

This cannot work, at least using xargs. Inside a \csname construct, everything has to be fully expandable or consist of tokens TeX will allow in a name (letters, numbers, spaces, $, ...). However, detecting optional arguments does not work that way: there is a \futurelet involved, but here there is also a \begingroup.

It is possible to grab optional arguments in an expandable manner, subject to some restrictions. The xparse package provides one wrapper for this

\documentclass{article}
\usepackage{xparse}
\usepackage{etoolbox}
\makeatletter
\def\abx@foo@baz@bar{test}
\def\abx@field@original@default@test{baz}

\DeclareExpandableDocumentCommand{\blx@imc@thefield}
  {O{original}O{default}m}
  {\csuse{abx@field@#1@#2@#3}}

\def\blx@getformat#1#2{%
  \ifcsundef{abx@#1@\blx@imc@thefield{test}@#2}
    {no}
    {yes}}

\begin{document}
\blx@getformat{foo}{bar}
\makeatother
\end{document}

(although this is not a document command!), while etextools also provides a similar concept. However, I'm not really sure I'd want to handle an internal macro is this way: I'd much prefer something with only mandatory arguments.

1
  • Ah, I see, thanks. I am just experimenting to see how to tackle some things that need doing (in a package I think you know ...). xparse looks rather powerful. What are your thoughts on using this package for biblatex?
    – PLK
    Nov 15, 2012 at 13:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .