Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
    
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 '12 at 13:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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