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

learn more… | top users | synonyms

8
votes
1answer
230 views

Expandable, standard TeX only way to compare balanced token lists

Is there a (reasonably) efficient macro that does something similar to \long\def\comparets#1#2{\def\aa{#1}\def\bb{#2}\ifx\aa\bb true\else false\fi} except is expandable (i.e. ...
8
votes
1answer
71 views

Moving the optional argument

When I create macros using \newcommand, I usually provide default variants setting some of the first arguments in a sort of partial application style \newcommand\five[5]{#1,#2,#3,#4,#5} ...
8
votes
1answer
159 views

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 ...
8
votes
1answer
169 views

Formatting code fragments extracted from file with minted

I would like to use the catchfilebetweentags package to grab code snippets from a source file and use minted to highlight them. Imagine I have the following in program.scala: //<a def ...
8
votes
0answers
98 views

Can I detect (from within a macro) if I'm in an \edef (or similar)? [duplicate]

Possible Duplicate: Are we inside an edef? I've defined a macro that changes its behaviour depending on the next character, so it uses \@ifnextchar. This makes it behave badly inside ...
7
votes
3answers
196 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 ...
7
votes
3answers
524 views

Command that changes its definition the nth time it's used

I need a command to behave differently the first time than the next times it's called. Right now I use \newcommand\foo{First\gdef\foo{Next}}, that is, it redefines itself the first time. Moreover, I ...
7
votes
2answers
120 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 ...
7
votes
2answers
178 views

Expand LaTeX macros to modify

I am using the polynom package for polynomial long division. For example: \documentclass{article} \usepackage{polynom} \begin{document} \polylongdiv{x^3-x^2+d}{x-2} \end{document} Notice that I ...
7
votes
2answers
514 views

Expand command read from file

I have a file, that contains a list of latex commands each command per line. Now I want to read this file line per line and execute/expand the macros automatically. I tried the following but this only ...
7
votes
5answers
2k views

\includegraphics: a command in the optional argument

I want to pass some arguments to \includegraphics via a command, but I can't get it working as I would expect. My M(n)WE: \documentclass{article} \usepackage{graphicx} \begin{document} ...
7
votes
3answers
513 views

\expandafter parameters that are not commands

I'm using a custom command to format all tables in a document. Some tables share properties other than style, such as column definitions and headers, which is why this answer inspired me to even more ...
7
votes
3answers
846 views

\uline within a pgfmath expression produces an error

Why does using \uline within a pgfmath expression result in an error? \documentclass{article} \usepackage[normalem]{ulem} \usepackage{tikz} \begin{document} \pgfmathparse{width("\uline{blabla}")} ...
7
votes
2answers
177 views

Expanding all arguments of a command before appending it to another macro

\documentclass{article} \makeatletter \usepackage{ltxcmds} \newcounter{mycounter} \setcounter{mycounter}{7} \newcommand{\mysndc}[3]{#1 #2 #3} \newcommand{\mycommand}{} ...
7
votes
2answers
64 views

How to manage expansion for this case?

My objective is to automatically compile parameterized template from within a main input file. For each compilation, a unique jobname and parameter are passed to the compiler. The problem is, passing ...
7
votes
1answer
222 views

\ifmmode self-contradictory in amsmath (OK in just LaTeX)

File: \documentclass[11pt]{article} \usepackage{amsmath} \newcommand{\argghhh}{\relax\ifmmode\bar\else\expandafter\=\fi} \begin{document} $ \argghhh a $ \end{document} produces error (but ...
7
votes
2answers
312 views

Expanding a sequence inside a TikZ path

I am trying to draw a simple path using TikZ, with a custom syntax to specify it. This works: \documentclass{article} \usepackage{tikz} \begin{document} \newcommand{\latticepath}[1]{ ...
7
votes
2answers
320 views

Why does this protection not work?

The answers to this question explain in detail what protection is so I'm not wondering about that. My question is why a certain command definition with expl3 does not give a protected command. ...
7
votes
1answer
1k views

A \typeout (or expandable command) that can expand once - or with variable levels of expansion?

Consider the following MWE: \documentclass[12pt]{article} \begin{document} \def\aaa{something} \typeout{=1==\aaa==} \typeout{=1==\meaning\aaa==} \typeout{} \def\bbb{else \aaa, else} ...
7
votes
1answer
902 views

\newif conditional causing problem in \typeout or \edef

The following code produces result as expected, i.e., False True on the typeset document. \documentclass{minimal} \newif\iftest \newcommand{\test}[2]{ \iftest #1 \else #2 \fi } ...
7
votes
2answers
153 views

How to use the total page count in a conditional expression?

I would like to have different footer or page style depending on whether the document has one or more pages. I am not very experienced with TeX programming, so the answers about "Comparing an argument ...
7
votes
2answers
156 views

Using an expanded macro for optional environment argument

I'd like to open an environment, passing the content of a macro to the optional arguments. Up to now I tried different approaches using \expandafter, however I could not get the expected behaviour. ...
7
votes
3answers
148 views

Command that expands to the contents of a file

I'd like to make a macro that expands to the contents of a file. Basically something like \def{\mycommand}{\input{file.txt}} % file.txt contains "Hello%" \anothercommand{\mycommand} ...
7
votes
2answers
164 views

How to put a conditional construct in \edef

\ifthenelse works fine normally, but when used in \edef: \documentclass{article} \usepackage{ifthen} \newcommand\testing[1][]{% \edef\tmp{\ifthenelse{\equal{#1}{}}{Blank}{#1}}% } \begin{document} ...
7
votes
2answers
581 views

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]{% ...
7
votes
1answer
318 views

Replacement for \unexpanded without eTeX extension

As comment on Can the extended register pool be used for \newinsert?, there is a reference to Dealing with tons of floats. It is interesting but uses \unexpanded: \edef\@freelist{% ...
7
votes
1answer
194 views

expansion of loop macro

I have a macro that contains a loop using \@for and it works until I try to write its output to a file. The following document shows the problem. \documentclass{article} \makeatletter ...
7
votes
1answer
111 views

Make the pattern of a \def depend on another macro

I would like to set up a macro defined something like this: \def\processline string: #1.#2\relax{\gdef\errorline{#1}} The catch is that the string should be given by the contents of another macro. ...
7
votes
2answers
371 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
514 views

Is there a robust \renewcommand replacement?

Replacing \newcommand by \DeclareRobustCommand makes a new command robust. But what about \renewcommand? Is there a command to robustly renew a command? It would be possible to first use ...
7
votes
1answer
254 views

\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 ...
7
votes
2answers
1k views

Building labels out of counters

There is a simple version to my question, and a complex version. First, the simple version: I want to generate label names by combining a constant string with a counter's value, something like what is ...
7
votes
1answer
98 views

Define macro based on Datatool contents

I can define a macro of the form \csname../dirX Color\endcsname manually and use it as desired. However, I am unable to do the same thing based on the contents as read by the datatool package. The ...
7
votes
1answer
300 views

Defining inside an \edef

The following throws an "Undefined control sequence" error: \edef\testa{\edef\testb{foobar}\testb} when it gets to expanding \testb. This problem is also mentioned in "TEX by Topic" 12.6.2. But no ...
7
votes
1answer
81 views

Expand package option, to make contained character math-active

In an answer to another question of mine, egreg posted code, which activates a character in math mode and gives it the function of \operatorname. Now I want to put this into a package and set the ...
7
votes
1answer
160 views

Forcing macro expansion with keyval

While trying to provide an answer to this question, I've run into a stumbling block that I can't seem to get over. The listings package (like many others) uses keyval to handle configuration. What ...
7
votes
1answer
55 views

expl3: \tl_put_right:Nx chokes on \{

I am trying to compose a command into a token list. \documentclass{article} \usepackage{expl3} \begin{document} \ExplSyntaxOn \tl_new:N \__a \tl_new:N \__b \tl_new:N \__c \tl_set:Nn \__a { ...
7
votes
1answer
158 views

String Comparison with a \protected\def

I am using a \protected\def as that solved an earlier issue of mine. I need to compare this value of the token to see if it is initialized to the same value as the \protected\def in different ways. ...
7
votes
1answer
141 views

Modified \overline over first character of complex argument

I've been using zref to handle my references. It has allowed me to customize a reference the way I want through its property lists. In particular, I reference a variety of structures in my document ...
7
votes
2answers
651 views

Using \nameref within \path

Background Looking to apply a consistent style to file and directory names. The book's appendix has a subsection for every file referenced by the book. Every subsection has a label whose value is ...
7
votes
1answer
133 views

I'm confused about how expansion is happening here

I was really pleased that I could come up with a means of building the contents of a tabular environment using \foreach \x in {...} {...} structure. But I was curious about what exactly it would take ...
7
votes
1answer
93 views

Expand macro at float typesetting (not at definition) time

To my knowledge, the contents of a float environment are put into a box when the float is defined. Is it possible to proccess a user defined command/macro when the float is actually typeset, e.g. to ...
7
votes
1answer
231 views

Why do some arguments in a macro need to be preceded by \noexpand?

I keep on studying the use of nested tikzpicture environments (sorry) and I discover the next problem. I'm not sure but it sounds like a problem with "fragile" commands. Perhaps, the problem is the ...
6
votes
4answers
357 views

How to repeat text

I get interesting errors with the following macro. Mostly it works, but I get extra spaces after \noindent (but not after \newline). I tried using \loop or \whiledo (from ifthen) but get stack ...
6
votes
2answers
525 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 ...
6
votes
4answers
403 views

Signalling error in “expandable context”

I'm reviewing a few old macros of mine to make them fully expandable. One of the macros takes one argument, which should either be a two-digit or a four-digit integer (>1955). The macro then converts ...
6
votes
3answers
140 views

Report all duplicate macro names defined by \newcommand

I have a shell script that produces a large list of macros in an external file in the form: \expandafter\newcommand\csname XX Name ABC\endcsname{some text ABC 1} \expandafter\newcommand\csname XX ...
6
votes
3answers
202 views

How to “demux” several aspects from a `\foreach` variable with `\ifstrequal`?

How can I "demux" several features from a mode specification given in a \foreach? I would like to use a \foreach to draw several nodes in a TikZ picture. The loop will specify a mode for each ...
6
votes
4answers
213 views

Expansion of commands in errmessage

When I run \documentclass{article} \def\pet{dog} \errmessage{My \pet\ is {sick}} \begin{document} My \pet\ is {sick}. \end{document} the error message is: My dog\ is {sick}. but the PDF ...
6
votes
3answers
160 views

Expansion issue when adding to CSV list from within a \foreach

Another expansion issue that I am unable to guess at the correct combination of \edef/\expandafters to get to work. Background: Below I have defined \AddToCommaSeparateList which uses \g@addto@macro ...