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.

Possible Duplicate:
What does \begingroup\expandafter…\endgroup do?

In many of Heiko Oberdiek's packages one sees this type of construction:

\expandafter\ifx\csname pdf@filesize\endcsname\relax

What is the purpose of enclosing the \expandafter commands within a begingroup...\endgroup? What are the advantages of such an approach?

share|improve this question

marked as duplicate by Bruno Le Floch, David Carlisle, egreg, Torbjørn T., Joseph Wright Mar 14 '12 at 8:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Oh sorry @egreg's answer (and the following discussion in comments) is more complete as well. –  David Carlisle Mar 13 '12 at 9:36

2 Answers 2

up vote 18 down vote accepted

oh sneaky Heiko:-)

It tests whether the csname was undefined (or relax) but in the case that it was previously undefined, it leaves it undefined as the implicit definition to \relax happens inside the group.

plain TeX:

\expandafter\ifx\csname aaaa\endcsname\relax

\expandafter\ifx\csname bbbb\endcsname\relax



> \aaaa=undefined.
l.3 \show\aaaa


> \bbbb=\relax.
l.7 \show\bbbb

share|improve this answer

Actually, it has to do with avoiding undue filling of the hash table.

The snag with

\expandafter\ifx\csname pdf@filesize\endcsname\relax

is that it isn't expandable. With eTeX, you can use \ifcsname instead. If it is a control sequence, and not name, you are testing, you can use \ifdefined instead. But Heiko creates platform-independent packages.

You can think also of etoolbox packages's definition of \csletcs:

\newrobustcmd*{\csletcs}[2]{% [2011/01/03]

The catoptions package defines this as (using a pseudoname here):


since \let is not expandable anyway. This leaves #1 and #2 undefined if #2 was originally undefined.

share|improve this answer

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