{expansion} is about expanding tokens, including the manipulation of expansion order and the suppressing of expansion.
86
votes
3answers
4k 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 ...
66
votes
4answers
10k 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 ...
58
votes
3answers
2k 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 ...
52
votes
3answers
865 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 ...
42
votes
0answers
1k 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}
...
35
votes
3answers
775 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 ...
33
votes
3answers
2k 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; ...
25
votes
6answers
4k 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 ...
25
votes
1answer
346 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
...
24
votes
4answers
432 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 ...
23
votes
3answers
368 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 ...
22
votes
3answers
520 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 ...
22
votes
2answers
637 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!
...
21
votes
4answers
496 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.
21
votes
4answers
350 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:
...
18
votes
3answers
173 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 ...
18
votes
4answers
546 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)
...
18
votes
1answer
616 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 ...
18
votes
1answer
183 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
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 ...
17
votes
2answers
218 views
Difficulty with macro expansion (updated)
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}
...
16
votes
3answers
376 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
2answers
518 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:
...
15
votes
1answer
428 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
3answers
184 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 ...
14
votes
3answers
666 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 ...
14
votes
3answers
734 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 ...
14
votes
2answers
272 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
2answers
763 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}%
...
14
votes
4answers
431 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}
...
13
votes
3answers
881 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 ...
13
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 ...
13
votes
1answer
96 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
281 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 ...
12
votes
2answers
158 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 ...
12
votes
3answers
288 views
Expansion of first token in a tabular cell
Background
I currently do some work on the collcell package which collects the content of a tabular/array cell and provides it to a user defined macro, e.g. using ...
12
votes
2answers
214 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 ...
12
votes
4answers
444 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 ...
12
votes
1answer
257 views
Text being output when none should be
I am confused as to why the MWE below produces any black text. As the code is below, the output is:
However, if you comment out the \IfStrEq line then it works fine
and the black text goes away?
...
12
votes
1answer
772 views
renewcommand in a newcommand
I have a command \putansline{1}{2} that inserts 1 line of dots and prints the total score as [2]. I want to define a new command that if issued, will redefine this command so that it prints only the ...
12
votes
1answer
305 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 ...
11
votes
3answers
283 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 ...
11
votes
3answers
711 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 ...
11
votes
2answers
381 views
Macro to do nothing via a \def
I have encountered another expansion issue and have narrowed it down to this simple test case of a macro that does nothing, well at least it tries to do nothing, but is not all that successful.
If I ...
11
votes
2answers
383 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:
...
11
votes
2answers
598 views
Tricks to make macros expandable
Expandable macros are useful (I find working in the lion's mouth super cool). But they are difficult to write.
Can more experienced users give hints that help achieve expandability?
For example, we ...
11
votes
4answers
555 views
What's the best/right way to test an argument to a macro?
This is essentially asking how to do a case switch in LaTeX. I want to define a macro so that if the argument passed is one thing, then something happens, and if not then something else happens. I ...
11
votes
1answer
186 views
How to expand a macro used in the range of a foreach loop?
Motivating Example:
Suppose that I want to draw the following tree:
root
/ \
/ \ / \
A B C D
This is easy enough to do by explicitly drawing the two subtrees
as children of ...
10
votes
5answers
345 views
How to create a “delay expansion” environment?
I don't want to use the following
\begingroup
\edef\next{\endgroup
\noexpand\const{Xp}{\X(tp)}
\noexpand\const{Yp}{\Y(tp)}
\noexpand\const{Xf}{\X(tf)}
\noexpand\const{Yf}{\Y(tf)}
...
10
votes
4answers
254 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 ...


