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

learn more… | top users | synonyms

127
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 ...
76
votes
3answers
4k 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 ...
34
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 ...
95
votes
4answers
17k 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 ...
43
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; ...
6
votes
2answers
541 views

Expanding (edef) a lipsum command?

Just trying to debug some stuff, and I thought about 'expanding' a \lipsum[1-2] statement using \edef into a new command (that would contain the "raw" text); strangely I cannot get it to work; this ...
57
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 ...
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 ...
15
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 ...
24
votes
5answers
593 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.
15
votes
4answers
559 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
1answer
517 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
1answer
293 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? ...
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 ...
11
votes
2answers
741 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 ...
21
votes
4answers
507 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: ...
10
votes
3answers
642 views

Censor text spanning multiple lines

I'd like to censor or hide some text while replacing it with the appropriate amount of white-space. The replacement text cannot be searchable. The text to be hidden usually exists in the LR mode. The ...
10
votes
2answers
540 views

Concurrently interleaving execution of Lua and TeX in LuaTeX

The usual way to interleave Lua and TeX in generating documents is by invoking Lua from Tex through the \directlua facility and generate Tex from Lua using tex.print(). For example, consider the ...
4
votes
3answers
2k views

Can (pdf)LaTeX recognize text strings and replace (expand) them?

I'd like LaTeX to replace e^ with \mathrm{e}^. I know I could just make a macro \e that does this, but I know I'll forget using it.. Can LaTeX find and replace and replace letter combinations (I ...
6
votes
1answer
367 views

Use \csname … \endcsname to compute the starred-version of some command

I want to define some commands declaring paired delimiters using the mathtools package in LaTeX. For example, \paren{...} should result in \left(...\right), whereas \paren[Big]{...} should give ...
6
votes
1answer
2k views

Is there a LaTeX/TeX preprocessor/inliner that replaces only selected macros with their definitions?

I have the following situation: Like most LaTeX users that write much, I have my own header files where the packages I use normally are required and the macros I use often are defined. So if I write ...
6
votes
2answers
487 views

On unprotecting (expanding) \protected macros (or, “the space after command name”)

I quite often come into a situation, where I need to obtain some text produced by a package macro as a string, like in say: Expanding (edef) a lipsum command? How do I "unprotect" an ...
15
votes
3answers
313 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 ...
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} ...
28
votes
2answers
737 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! ...
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) ...
8
votes
1answer
796 views

Problem with environment expansion and the Tikz external library.

I've created my own environment mytikz, which basically looks like this (full MWE): \documentclass[a4paper]{article} \usepackage{tikz} \usetikzlibrary{external} \newenvironment{mytikz}{%begin code ...
9
votes
1answer
792 views

Highlighting line ranges in a lstlisting: partial solution, but expansion issues

In my quest for a better solution for How to change appearance of a particular line when using \lstincludelisting, I came across the linebackground= option Martin Scharrer added to the listings ...
20
votes
2answers
317 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 ...
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}% ...
12
votes
2answers
872 views

Minimal \protected@edef example

I recently came across \protected@edef in the/a LaTeX2e source. Can anyone give me a minimal or simple example, where I need this? I know this has to do with fragile commands/robustness, but I am ...
6
votes
1answer
430 views

How to expand values stored in a token defined by newtoks

In this project, the directory structure and file names are based on three parameters, with there being one exception to both. Directory Path: \GetTexFilePath{}{}{} The directory path is based on ...
12
votes
3answers
331 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 ...
7
votes
3answers
195 views

Are we inside an edef?

How would you test whether we are in an expansion only context (i.e., \edef, \xdef, \write,...) or not? The only ways I could think of leave things behind (but the full expansion of that extra stuff ...
6
votes
2answers
1k views

Save current value of counter in a command

I would like to define kind of an own index, that collects certain information and one counter value - in the following small example the \addstuff-command that may get the value of a counter as its ...
21
votes
2answers
699 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: ...
9
votes
6answers
657 views

Is there a '\ReallyDontExpandUntilTheLastMinute' command?

There's a couple of things I want to be able to do that are quite similar and basically involve keeping back a macro expansion until the last minute. Possibly the simplest example is that I have a ...
8
votes
1answer
368 views

Print name of command, defined by \csname

First off: I do not understand \expandafter and \noexpand completely which is probably the main problem. I have found a question that explains what it does, but still I cannot seem to make it work. I ...
4
votes
3answers
135 views

Macro to be used in a \section command that has no effect in the table of contents

I need a macro that is executed only in the main document (within a section title) and then disappears (for the table of contents). A minimal example where this occurs is this: ...
4
votes
1answer
447 views

pgfmath expansion - call a command from within a pgfmath environment

I am trying to define a newcommand using pgfmath. This commands calls a previously defined command but it fails. It propably has to do something with expansion. I've read this question about ...
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 ...
19
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 ...
15
votes
1answer
206 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 ...
15
votes
4answers
615 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 ...
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 ...
9
votes
3answers
590 views

Problem with nested \noexpand and \edef

I have a LaTeX problem and want to explain it with the following code snippet: \documentclass{article} \begin{document} \def\myline{A} \edef\myline{\myline \noexpand\bf B} \edef\myline{\myline ...
13
votes
2answers
981 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
1answer
2k 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 ...
11
votes
1answer
611 views

How to do \scantokens inside \edef without triggering “Runaway definition”

I'm trying to find a way to use the e-TeX \scantokens command inside [the equivalent of] an \edef (well, actually an \xdef, but presumably this does not make any real difference since that's just the ...