{expansion} is about expanding tokens, including the manipulation of expansion order and the suppressing of expansion.

learn more… | top users | synonyms

126
votes
3answers
8k views

What is the difference between Fragile and Robust commands?

So I've read a few times about this, the “moving arguments” and all that. But what does it really mean? Could someone explain in a few words what is going on at the TeX level, what is the difference ...
91
votes
4answers
16k views

When to use \edef, \noexpand, and \expandafter?

I'm quite happy hacking TeX macros and cobbling together bits and pieces from different style files to suit my own ends, but I have a suspicion that my resulting hacks are not quite as elegant as they ...
75
votes
3answers
3k views

Showing the bibliographic entry in a popup when you hover over the citation key

I thought it would be useful to have bibliographic entries displayed as a tooltip so readers are not interrupted by following hyperlinks or trying to find the Bibliography, so I created the following ...
60
votes
1answer
2k views

A LaTeX log analyzer application (visualizing TeX expansion)

Consider the following MWE, test.tex: \documentclass[12pt]{article} \begin{document} \tracingassigns=1 \tracingmacros=1 \def\aaa{something} \def\bbb{else \aaa, else} \edef\ccc{third \bbb, level} ...
56
votes
3answers
1k views

Why isn't everything expandable?

TeX's macro processor does its work in a process called expansion. For an input stream of tokens, the macro processor repeatedly expands them until non-expandable tokens remain. The resulting stream ...
49
votes
3answers
1k views

Advantages and disadvantages of fully expandable macros

As I currently understand them, fully-expandable macros are analogous to pure/effect-free functions in functional programming. Correspondingly, things that are not expandable, such as \def, are ...
44
votes
4answers
2k views

What is “expansion”?

I'm relatively new to (La)TeX, but I have a reasonable amount of experience with computational programming languages. Until I began learning (La)TeX, I'd never heard of "expansion"... So, what is ...
42
votes
3answers
3k views

What is the difference between \let and \edef?

When you write \edef\a{\b} \let\a\b the results are often identical. What is the difference between these two commands? When do they behave the same? When do they not? (The question isn't for me; ...
36
votes
4answers
2k views

How does your mind bend \expandafter to its will?

Expansion is often cited as one of the most arcane aspects of TeX, more akin to witchcraft than to something easily picked up by the newcomer. There are many great questions and great answers about ...
32
votes
7answers
5k views

LaTeX macro expander

Is it possible to have LaTeX (or another program) to expand a the macros in a .tex file, and output the expanded LaTeX source? If no such program exists: are LaTeX macros processed simply as string ...
28
votes
2answers
723 views

Why do we need eleven expandafters to expand four tokens in the right order?

In Appendix D - Dirty Tricks Knuth gives an example of expanding four tokens in the right order by using eleven \expandafter commands. Here, is the code slightly adapted for to-morrow! ...
27
votes
1answer
412 views

Why six \noexpand in a row?

Trying to understand TeX's rules for parsing dimensions, I came up with the following weird behaviour: \dimen0=1\noexpand\noexpand\noexpand\noexpand\noexpand\empty pt is valid, but ...
25
votes
3answers
631 views

A guide to understanding expandability: when to write protected functions and when not to

I'm having difficulty understanding (and appreciating) the concept of expandability. I'm very murky about understanding when and how expandability impacts me in writing code for my documents. I've ...
25
votes
4answers
580 views

Why isn't a command defined by \newcommand with an optional argument expandable?

In trying to fix a problem with a nested macro repeatedly calling itself, I tried expanding the inner call before passing it as an argument to the outer one. This failed miserably because the macro ...
24
votes
3answers
1k views

How to know whether or not we need \expandafter?

The following is quoted from Peter Grill's answer on question LaTeX dynamic macro definition. The \expandafter is requried as the \newcommand needs to be dealt with after the \csname. As a ...
24
votes
4answers
2k views

On testing two fully expanded character strings for equality

I know three macros that test two fully expanded character strings for equality: \ifthenelse{\equal{<op1>}{<op2>}}{<true>}{<false>} (ifthen package) ...
23
votes
5answers
582 views

Can one define a '\superexpandafter{n}' that would expand to 2^n-1 '\expandafter's?'

This question was inspired by comment banter in this question. See title.
22
votes
3answers
295 views

Why the \expandafter\@firstoftwo idiom?

I see a lot of macros which are conditional tests followed by a {true}{false} pair defined so that they result in either: \expandafter\@firstoftwo or \expandafter\@secondoftwo Why are these ...
21
votes
4answers
493 views

Can one define an expandable command that removes control sequences from its argument?

I am trying to create an expandable command that accepts a single argument that may contain control sequences, and expands to that same argument with all control sequences and braces removed. That is: ...
21
votes
2answers
673 views

Stripping the pt from a dimension

LaTeX defines in ltfssbas.dtx a macro called \strip@pt (at line [246]) which strips the pt part from a dimension returned by \the\dimension. The definition is shown as a minimal working example below: ...
21
votes
1answer
667 views

Why do the definitions of \quad, etc. have \relax in them?

Plain Tex defines \quad so: \def\quad{\hskip1em\relax} Why is the \relax there? It's usually used as a token that can't be expanded, serving as a "do nothing" operation after macro expansion is ...
20
votes
1answer
122 views

Is implicit `\relax` in conditionals explained anywhere in the TeXbook?

I have been relying for years on TeX's behavior where, say \if1\fi expands to nothing and \if1\else\fi does the same. In the second case TeX inserts an 'implicit' \relax when it tries to expand \else. ...
19
votes
2answers
323 views

Difficulty with macro expansion

I'm not getting the output I want from my LaTeX code, and I think the problem is that I'm not expanding things at the right times. Code \documentclass{article} \usepackage{tikz} ...
18
votes
2answers
2k views

What does \begingroup\expandafter…\endgroup do?

I often see code of the following form in package implementations (this example is from the LaTeX3 sources): \begingroup\expandafter\expandafter\expandafter\endgroup \expandafter\ifx\csname ...
18
votes
1answer
321 views

Unusual numbering: combine alph, Alph, arabic

I would like to have an unusual numbering aAa going as follows: 0 -> * ------------------------- 1 -> a 2 -> b ... 25 -> y 26 -> z ------------------------- 27 -> A 28 -> B ...
17
votes
3answers
167 views

How do I have to invoke \expandafter for a macro with multiple arguments?

\documentclass[pstricks,border=12pt]{standalone} \usepackage{pst-node} \begin{document} \begin{pspicture}[showgrid](-5,-5)(5,5) \pnodes{A}(-2,3)(3,4)(2,-1)(-2,-4)(-4,0) \def\points{(0,0)} ...
17
votes
2answers
365 views

Why does this simple \ifx test fail?

This should be easy, but I don't understand why the test below fails... I expected Hurrah! to be printed when the \currentchar count reaches 90 (i.e. the character code of Z). There must be some ...
17
votes
2answers
302 views

Get the lion to run in loops. Tersely

Various constructions can cause TeX (aka the lion) to go into an infinite loop. The simplest example is \def~{~}~, which defines ~ to expand to itself, then expands it. Now, if we exclude macro ...
17
votes
5answers
1k views

Are there purely expandable variants of \MakeUppercase?

\MakeUppercase and \uppercase use TeX's uccode, and they are not purely expandable. Say, \edef\temp{\MakeUppercase{abc}} will fail. Sometimes purely expandable macros are very useful. They are ...
16
votes
3answers
824 views

Why does \dimexpr swallow \relax?

Following the comments at Doing maths with distance values in LaTeX source code: +1 for teaching me that \dimexpr can optionally swallow a \relax. Yesterday I tried to use \relax as a delimiter ...
16
votes
3answers
415 views

(Why) Can't we get 'fully expandable' versions of every command?

I'm just starting to figure out what 'expandable' means in LaTeX, and why it is useful to have commands that expand as far as possible. In particular: If you want to \typeout some string to the log ...
16
votes
3answers
889 views

Does `\noexpand` have to be a primitive?

Background: I'm trying to deepen my understanding of TeX and how the intricacies work (spurred a little by this place, I should say, and hoping one day to not be a "Cargo Cult Programmer" - see When ...
15
votes
3answers
1k views

Expand after all that stuff?

Often I find myself in the need of expanding some macro, whose contents should be given as an argument to another command. The usual solution is to invoke some \expandafter concoctions to somehow ...
15
votes
3answers
305 views

Parsing strings containing diacritical marks (macros?)

I apologize that this question may seem very much like several other recent ones I have asked (Parsing leading hardspaces, Parsing a \$ as part of an improved \getargs command). They are all related ...
15
votes
2answers
318 views

How to display command name and its value

Latex displays the command values by default - I am finding it more difficult to display the command names along with the values. I have a list of commands that I would like to loop over and print ...
15
votes
4answers
602 views

Detokenizing without extra spaces?

I have a need to pass through potentially arbitrary characters untouched and found this macro: \def\test#1{\expandafter\zap@space\detokenize{#1} \@empty} The problem is that \detokenize inserts ...
15
votes
2answers
1k views

How to add a hook to a macro

LaTeX uses hooks extensively, one method employed is the command g@addto@macro. It is defined in source2e as: \long\def\g@addto@macro#1#2{% \begingroup \toks@\expandafter{#1#2}% ...
15
votes
4answers
553 views

Expandable full expansion of tokens that preserves catcodes

Is it possible to fully expand tokens in an expandable manner and preserves category codes? I'd like to do this just using pdfTeX. I'm looking for something that would work like this: \def\foo{foo} ...
15
votes
1answer
465 views

How do I ‘expand’ a control sequence `\let` to a character?

Consider the following: \let\1=X \edef\2{\1} \show\1 \show\2 The result is > \1=the letter X. > \2=macro: ->\1. Is it possible to ‘expand’ the meaning of \1 so that \2 contains just ...
14
votes
4answers
709 views

\aftergroup a list of tokens

\aftergroup executes the next token after the end of the current group. Is there a variant, \Vaftergroup{\some\tokens\a\b} that would put \some\tokens\a\b after the current group ends? Of course, one ...
14
votes
2answers
1k views

Expanding arguments before macro call

I've been banging my head against this for days. I have a macro which takes a macro name and its arguments. It then re-arranges the arguments a bit and then calls the passed macro with the re-arranged ...
14
votes
3answers
1k views

Expandable version of \InputIfFileExists or \IfFileExists

From this question: Cannot use \toprule when doing \input inside tabular -- why? I know that everything inside the tabular environment should better be expandable. The MWE below suggests that ...
14
votes
2answers
332 views

LaTeX3: How to get reliable control over the level of expansion?

Say you want to write LaTeX3 code that manages user data. Sometimes you want to process that data, but other times you just want to store it, move it around and return it unchanged (when developing a ...
14
votes
1answer
192 views

A problem with counters

While trying to answer a question I came upon a problem with counters and I need some help. The idea is to collect answers to some numbered exercises, so the collected answers can be all typeset at ...
13
votes
3answers
550 views

TeX macro and command expansion?

I have implemented couple of macros that should remove commas from numbers. It's nasty dirty stuff, but it works when input is number: \removecommas{12,34,56.7} % => 123456.7 But it stop working ...
13
votes
2answers
473 views

\expandafter within a \begingroup … \endgroup [duplicate]

Possible Duplicate: What does \begingroup\expandafter…\endgroup do? In many of Heiko Oberdiek's packages one sees this type of construction: ...
13
votes
1answer
139 views

\( \) don't work with TOC?

If i write this line of code \subsection{Ranger \( n \) boules distinguables dans \( M \) boîtes} I obtain an error over the second \) (./Probabilité.toc ./Probabilité.toc:8: LaTeX Error: Bad ...
13
votes
2answers
951 views

Allow underscore in PGFPlots tick labels read from table

I'm trying to use the text data from a table column as the tick labels in a plot. The text data contains underscores, which makes LaTeX throw a Missing $ inserted error. I'd usually get around this by ...
13
votes
1answer
494 views

Why is \input not expandable?

In latex.ltx I found \def\input{\@ifnextchar\bgroup\@iinput\@@input} where \@@inputis expandable. I would like to know if there is an expandable version of \input. The problem comes from an example ...
12
votes
4answers
385 views

Create a capitalized macro token using \csname

Two things I understand and one thing I don't: I often use \csname to create user-defined tokens. For instance, the following code uses \expandafter to expand everything between \csname and ...