Questions tagged [expansion]

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

(La)TeX has a macro programming language that expands the macros to their definitions. Only TeX primitives for the direct typesetting, boxing and assignments are not expandable, but must be executed by TeX. In advanced macros the order of expansion must sometimes be changed, content must be fully expanded or certain macros must not be expanded until later.

Often used TeX macros for this are:

  • \expandafter
  • \noexpand
  • \edef and its global form \xdef
  • \unexpanded (e-TeX)
1115 questions
31
votes
3 answers

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 \expandafters there? I would have though they'd just…
PLK
  • 22,776
16
votes
1 answer

Why does \expandafter not expand the full { } group?

I thought I had understood the principle of expansion ... but apparently not! \def\a{AAA} \def\b{BBB} \expandafter\detokenize\expandafter{\a\b} I thought that the 2 \expandafter would store \detokenize and {} resp. and then expand \a into AAA…
Loic Rosnay
  • 8,167
13
votes
4 answers

Insert a period / full stop if caption argument doesn't end with one

I have a command \Notes which simply is a caption under a figure: \newcommand{\Notes}[1]{\captionsetup{position=below}\caption*{#1}} I would like the punctuation to be consistent in the caption, always closing with a full stop. That is, I'd like…
Hugh
  • 2,374
  • 17
  • 30
12
votes
1 answer

expandable unit conversion

I'm writing a package to assist typesetting documents for some special Pen & Paper Fantasy RPG. In this fantasy setting, there are three types of coins: gold, silver and copper pieces, which convert 10:1 each. However, many groups play with a…
carsten
  • 2,966
11
votes
2 answers

How to use \noexpand in an \edef?

Continuing with my struggle to deal with expansion issues, I am stuck on how to use an \edef properly. The code below with the switches set as: %\def\ApplyColorToTitle{} %\def\UseMathrmInTitle{} \def\UseEdefInsteasOfDef{} produces: but as soon as…
Peter Grill
  • 223,288
11
votes
3 answers

How to delay expansion

Consider the following document: \documentclass{article} \usepackage{polynomial} \begin{document} \def\a{-3} \def\b{-2} \def\c{-4} \[ \polynomial{\a,\b,\c}\quad\polynomial{-3,-2,-4} \] \end{document} As can be seen in the result the first…
Gonzalo Medina
  • 505,128
11
votes
5 answers

How do I expand exactly once a macro which takes an argument

I thought I was finally understanding expansion, but then I wrote something along the following lines, and it has me a bit…
A.Ellett
  • 50,533
10
votes
1 answer

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 simply want to print the name of a command using…
Didii
  • 1,323
10
votes
2 answers

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 example:…
sdaau
  • 17,079
10
votes
2 answers

Expansion issue with \begingroup and \foreach

This question led to a new package: nowidow I'm trying to define a dynamic \begingroup command to get rid of widows: \usepackage{pgffor} \newcommand{\nowidow}[1][2]{% \begingroup\widowpenalties #1 \foreach \n in {2,...,#1}{10000 } 0 …
raphink
  • 31,894
10
votes
1 answer

\forcsvlist and expansion

I've got an expansion issue with the following code: \def\sizes{huge, LARGE, Large, large, normalsize, small, footnotesize, scriptsize, tiny} \forcsvlist\sampletext{\sizes} My problem is that \sizes should be expanded before being passed to…
raphink
  • 31,894
10
votes
1 answer

Prevent \caption from expanding argument too early

I have a macro that defines some internal macros and does so locally as to not clutter the global namespace. However, \caption seems to expand the macro's argument too early. Here is a minimal…
mh543
  • 405
10
votes
1 answer

Expansion inside \AtBeginDocument

I'd like to expand a macro inside \AtBeginDocument, although I guess the concrete macro does not matter. \documentclass{article} \makeatletter \@for\x:=1,2,3\do{% \AtBeginDocument{\@input@{helper\x .tex}}% } \makeatother \begin{document} Do…
mafp
  • 19,096
9
votes
1 answer

Getting to grips with Expansion

I've been trying to read through some examples and text about how expansion works, but I seem to be unable to understand how it really works. The code I am trying to get to work, is the following: \documentclass[11pt]{article} \usepackage{lipsum,…
Runar
  • 6,082
9
votes
2 answers

Could someone explain the tokenization process that's leading the whitespace creeping in here?

Here's a MWE that creates spurious whitespace: \documentclass{article} \usepackage[T1]{fontenc} \usepackage{lmodern} \newcommand\aetest[2][\relax]{%% \def\aejunk{<#2>}%% \ifx\relax#1 \else \def\aejunk{(#2:#1)}%% \fi …
A.Ellett
  • 50,533
1
2 3 4