{expansion} is about expanding tokens, including the manipulation of expansion order and the suppressing of expansion.
85
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 ...
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 ...
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 ...
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 ...
52
votes
3answers
864 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 ...
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.
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?
...
11
votes
3answers
706 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 ...
13
votes
3answers
880 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 ...
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:
...
11
votes
2answers
597 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 ...
8
votes
2answers
445 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 ...
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}
...
4
votes
3answers
1k 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 ...
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; ...
6
votes
1answer
301 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 ...
12
votes
1answer
303 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 ...
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!
...
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 ...
6
votes
1answer
247 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 ...
6
votes
3answers
181 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 ...
9
votes
6answers
572 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 ...
5
votes
2answers
357 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 ...
3
votes
3answers
102 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:
...
35
votes
3answers
774 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 ...
9
votes
1answer
555 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 ...
7
votes
1answer
462 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
...
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
2answers
280 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 ...
10
votes
1answer
399 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 ...
10
votes
2answers
562 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 ...
9
votes
1answer
130 views
Is there a way of defining a command which is automatically converted to a no-op after its first execution?
Is there a way of defining a command that can be executed only once and is then redefined to be a no-op?
Related:
Is there a way of defining a command \nullifyifmoved{arg} that will normally act ...
8
votes
3answers
401 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 ...
8
votes
2answers
1k views
Cannot use \toprule when doing \input inside tabular — why?
Using \input inside a tabular environment does not seem to work in all cases, but I don't see why.
The MWE below gives the following error message:
! Misplaced \noalign.
\toprule ->\noalign
...
7
votes
2answers
105 views
Is there a way of defining a command \nullifyifmoved{arg} that will normally act like its argument but act like a no-op if the macro is “moved”?
I'm interested in a macro \nullifyifmoved that takes one argument arg and acts like arg in normal contexts, except it acts like a no-op wherever it is written to a file. Some background:
Martin ...
6
votes
2answers
709 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 ...
3
votes
1answer
1k 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 ...
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 ...
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 ...
7
votes
2answers
238 views
Convert string to file name and use in \href
I have a bunch of individual .tex files and want to be able to click on a link in one PDF file and have the appropriate PDF file open. These are small files, so don't really need to index into them, ...
7
votes
1answer
248 views
How do I store the upper case counterpart of a string?
I have a string stored in a variable \word, and I want to store its upper case counterpart in another variable \WORD. If I try
\def\WORD{\MakeUppercase{\word}}
\show\WORD
then the message ...
6
votes
2answers
359 views
Forcing tikz/pgf to expand macros within commands
I would like to use simple macros within tikz commands, but it seems tikz doesn't expand them in the ways I would expect.
For instance, the code
...
5
votes
1answer
306 views
Evaluation order during macro expansion.
From this question Creating PDF bookmarks in custom order arose another. (I hope it is correct to create a new question instead of commenting or editing/answering my old question.)
Using this macro ...
4
votes
1answer
302 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 ...
3
votes
3answers
330 views
Expand directory names using nested macro definitions
Background
Would like to use values from macros that reference other macros (n-levels deep) and apply their values to the \path command.
Problem
(Scenario A) The following works:
\usepackage{url}
...
2
votes
2answers
173 views
An expandable 'character scanning' command?
Please consider the following MWE, for a "character scanning" command "\looper" which utilizes \@tfor:
\documentclass{minimal}
\usepackage{trace}
\makeatletter
\newcommand\looper[1]{%
% ...
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}
...
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 ...
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 ...
