Questions tagged [expansion]

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

Filter by
Sorted by
Tagged with
3
votes
3answers
117 views

Expanding a parameter token argument to \g@addto@macro

Consider the following example. By default \g@addto@macro doesn't expand its arguments, which can have unexpected results when passing a macro as an argument, if the macro value is changed later. ...
0
votes
2answers
87 views

Pass and run a TeX macro from Lua

\starttext \startluacode userdata = userdata or {} function userdata.simple(c) context("\\%s{%s}{%s}",c,1,4) context.par() context[c](1,4) end \stopluacode \...
0
votes
0answers
31 views

What is * doing for these commands and how to replicate?

I'm trying to get a project's root folder path for usage later on in the preamble. My latex knowledge is... limited... but I've gotten a solution working using a combination of the currfile, and ...
8
votes
4answers
157 views

How to safely check by means of expansion-methods whether a list of tokens contains a token which is defined in terms of \outer?

I take the following question for a moot thing. Nonetheless it makes me sick that I don't see a good and robust solution myself. ;-) Assume something like this: \def\unfold{\umbrella My bald head ...
0
votes
1answer
28 views

fp and macro question

I try to feed macros doing FP calculations into other macros doing the same, and face problems with macro expansion. The usual "protect" trick didn't work for me. Any hints? An example is below. \...
0
votes
1answer
59 views

Why does \ifdim work here but \if doesn't?

I'm very new to LaTeX, and need to compare a length I've defined with a default value (and set another length accordingly). Consequently, I'm exploring the murky (to me anyway) world of LaTeX ...
0
votes
1answer
41 views

Metapost: replace variables with values before being consumed by a macro

I am trying to use the metaflow macro set for metapost. It and its documentation can be obtained from: https://personal.utdallas.edu/~hamlen/projects.html The relevant definition that I'm having ...
3
votes
2answers
68 views

\tl_set:Nf deletes beginning spaces in its #2

In syntax \tl_set:Nf <tl var> {<tokens>} if <tokens> begins with a space (input as ~), then this space does not appear in the definition of <tl var>. If a space appears in ...
5
votes
3answers
285 views

Dynamic variable name gets overwritten

I'm writing a class and thus storing a bunch of data into variables. Those variable needs to be access, and rely on a counter value. However, when accessing the values of previously-defined variable, ...
0
votes
1answer
31 views

construct \externaldocument command's argument for programmable cross referencing

My ver12.tex and ver12_SM.tex documents cross reference each other using the xr package. How could I program them such that when going over to ver13 the two \externaldocument commands do their job ...
0
votes
1answer
36 views

Bind a label to newwatermark / refactoring macros defined with newcommand

I trying to make a kind of a refactor of, and added feature to, newwatermark (and siblings). That feature is binding a label to the watermark, such that the page on which it is applied is ...
1
vote
1answer
27 views

Removing leading comma of a comma separated list generated by a macro

The goal of the program below is to apply certain modification to the values of certain keys within a cs list. It works fine, except for removing the leading comma. Try with/out lines A and B. \...
5
votes
1answer
79 views

Purpose of \expandafter in a particular macro

Consider the following set of definitions... \documentclass[12pt]{article} \def\foreach{% \begingroup \catcode`\^^M=12 \xforeach} {\catcode`\^^M=12 \gdef\xforeach #1^^M{% \first #1,,\...
3
votes
4answers
180 views

Prevent hash doubling with \message

Assume a token sequence: Token 1: Explicit hash character of category code 6. Token 2: Explicit 1 of category code 12. Token 3: Explicit hash character of category code 6. Token 4: Control word token ...
1
vote
2answers
62 views

Why parameter whose tokens are being iterated through should be ended with a non-empty macro, and what should that macro actually contain?

I've asked this question more than once here, but all I could get is that "it's safer" to use a non-empty macro. I was never told why, and (more importantly) it was never clarified what exactly the ...
0
votes
2answers
52 views

Expand variable name before writing to aux file

How can I have a variable in variable name while writing to aux file in the answer on this link: https://tex.stackexchange.com/a/416272/192717 For example, from the above link: \edef\mysuffix{...
1
vote
1answer
26 views

Immediately expand \thepage (custom TOC)

I want to define a command to add a line to a TOC with the page number where the command is called. The first try was: \def\mytoc{} \def\addlinetomytoc#1{ \g@addto@macro\mytoc{ #1\dotfill\...
5
votes
2answers
303 views

Microtype expansion gets disabled when fontsize is changed! (lualatex)

It seems like microtype's expansion gets disabled when fontsize is changed from default fontsize. Here are two pieces of code, in the first one expansion does work, in the second one it does not (as ...
5
votes
1answer
241 views

Question about exercise 21.10 in The TeXbook

This exercise asks how to store expansion of \chapno at the moment when \write is processed, but \the\count0 must be expanded at the moment of \shipout. Here is a full example: \def\chapno{5} \...
2
votes
1answer
43 views

Why can't the name of a `totcounter` be defined with a variable

I want to print the sum of some counter per subject at the beginning of that subject and I want it to be done automatically. I decided to use the totcount-package. I then define a counter like: \...
1
vote
3answers
66 views

\def, math, and order of operations, expansion?

I have what looks like a simple task but I cannot seem to get it to work. take the following: \def\varlength{1in}%has units \def\varscalar{0.2}%does not have units \raisebox{\varscalar\varlength}[...
4
votes
3answers
119 views

How to iterate through a token list to make characters uppercase, while preserving spaces?

The following (classical, I assume) "iterator" definition would be examined here: \def\zEnd{\zEnd} \def\zzIterator#1{% \ifx#1\zEnd \else #1% \expandafter\zzIterator \fi } \...
4
votes
3answers
465 views

How to capitalize letter (using \uppercase) that has been assigned to \let (or to \def)?

I don't seem to be able to capitalize a letter that has been assigned to \let. \let\zTest=t \uppercase{\zTest} % still small ADDENDUM: I've tried \def\zTest{t} just now and it didn't work either (...
10
votes
4answers
296 views

Conversion of space characters into space tokens

I'm trying to understand how the input, the expansion, and the execution processors work together. Running TeX on \def\foo#1{(#1)\baz}% \def\baz{baz}% \foo{bla} Bar \bye yields "(bla)baz Bar" with a ...
0
votes
1answer
75 views

What `\cs_new_protected` and `\cs_generate_variant` do?

After my question Undefined control sequence while adding a new parameter to a existing \ExplSyntaxOn code, I trying to fully understand the code behind it. So far, so good, with: The expl3 package ...
2
votes
1answer
29 views

How to pass custom commands to LetLtxMacro?

I think it's fairly common to redefine macros. And I'm creating two functions to help storing and restoring these redefined macros. And so I tried using the following: \documentclass{article} \...
1
vote
1answer
72 views

Writing expanded macro with space to file

I'm trying to write an expanded macro to a file without the expansion eating the space after it. I could replace the {} empty group with an explicit \space, but I want this to be fairly robust. Is ...
2
votes
1answer
31 views

Build a shortcut for string to be used as an argument later

I need to pass the result of a macro as a string to another macro (I'm using xparse). Here is a minimum working example: \documentclass{minimal} \usepackage{xparse} \NewDocumentCommand{\...
6
votes
3answers
246 views

Questions about `\cs_new:…` and friends and their TeX relatives regarding global, local, long, nopar, protected, expandable and naming

This question is two-fold. Unfortunately, one side is opinionated, i.e. something to close-vote, so I put that part at the end and hope no one notices... The other side (coming first) are serious ...
0
votes
0answers
12 views

How to use macro as a `foreach` range? [duplicate]

These two loops do not produce the same result: \documentclass{article} \usepackage{tikz} \begin{document} \foreach \i in {a,b,c} {% \i+ } \def\Index{a,b,c} \foreach \i in {\Index} {% \i+ ...
0
votes
0answers
39 views

Expanding user defined macros for compatibility

I have an annoying reoccurring problem. I often use user-defined macros with for example \newcommand in my LaTeX code. I also use LaTeX a lot in for example Jupyter Notebooks. Often Some things render ...
0
votes
1answer
39 views

Math in lwarp (via controlled expansion?)

Here is the issue I am thinking about, and some possible approaches. When writing a latex/html document using lwarp, the math code is left embedded directly into the resulting HTML file and left up to ...
2
votes
1answer
36 views

Why does an index entry generated from a listings' token preceeding a comma get rejected?

I'm trying to index the tokens in a listing, but they always get rejected if they are followed by a comma. Here's mwe.tex: \documentclass{article} \usepackage{listings} \lstset{index={foobar}} \...
1
vote
1answer
52 views

Using xstring to generate index keys

I have a document containing many instances of an environment with a mandatory argument providing a short "tag." These tags are something like "M04", and I've written a macro using xstring which ...
7
votes
2answers
238 views

How to have hashes doubled _and_ things expanded?

I'd like to learn some TeX programming technique which I can always apply for handling the same type of situation: The code \newtoks\mytokenregister \def\five{\four5} \def\four{\three4} \def\three{\...
1
vote
0answers
89 views

String check against a sage command, issue of expansion order? [closed]

I am trying to write a macro to do a conditional check against two strings. The problem I'm having is that I want to check the output of a \sage command and do a string comparison without expanding ...
5
votes
1answer
65 views

How can I use \expandafter the expand the definition of this control sequence?

I define a command \test that does nothing but define a control sequence \c[count], where [count] is the current text value of the counter count at the time \test is executed. I want \c[count] to ...
0
votes
1answer
78 views

Is there a command, macro or tool that can get the string result (as in other languages) of any macro in an easy to use way? [closed]

I don't want to go down into the depths of expandable and not expandable. I doubt that I will be able to understand my self-developed designs a few weeks later. Therefore I am looking for a universal ...
2
votes
2answers
64 views

How to pass the contents of a newcommand to answers package?

I've got the following code, which uses the exam document class and the answers package. I'd like to pass the point value of the question into a separate file so that I can make use of the question's ...
8
votes
3answers
342 views

Is there a way to retrieve the “source code” and the argument pattern from a macro for reuse (i.e. what \show shows)?

I want to concatenate the “source code” of two macros which share the same argument signature. How do I grab (i.e. expand once) the macro without TeX searching for the arguments which the macro ...
0
votes
0answers
74 views

ConTeXt: Cross reference expansion and processing with Lua

I want to process cross-references in ConTeXt/LuaTeX, that is, to do some calculations in Lua with the page and line numbers returned by the \at and \inlinerange commands. But this does not work, as ...
0
votes
1answer
30 views

define \label and \ref with shortcuts

Is there a way define \label and \pageref with shortcuts depending of context. In the mwe neither work. \documentclass[a4paper]{article} \newcommand\phasevie[2]{\section{#1-#2}\def\cod{#2}} \...
2
votes
1answer
32 views

Defining a command with star argument to be used in pdf strings (e.g. via \section)

I want to define a command with an optional star argument and be able to use it inside pdf strings, e.g. via \section. With the MWE below, I get a Token not allowed in a PDF string warning and \foo is ...
3
votes
2answers
69 views

Expand \def in \write18

I have defined the following command: \newcommand{\formatcommand}[1]{ \def\temp{#1} \def\s{\ifx\temp\empty empty\else not empty\fi} echo "\s" } This works correctly: \formatcommand{} \...
0
votes
1answer
66 views

Expandable contact information

Introduction I am sorry for the long MWE, but I was not able to make it shorter and still illustrate my problem. Again I am working on streamlining the creation of exams for my university. In the ...
1
vote
2answers
46 views

How can I avoid to copy and paste this code (similar newcommand definitions using different functions) :

I've been improving my code thanks to you guys, but this still looks improvable as it looks like an ugly copy/paste : \newcommand*{\LegendFlecheInf}[5]{ \coordinate (C2) at (axis cs:#2, { \...
0
votes
1answer
52 views

How to define a macro with digits in its name using \def and a counter value?

I need a \def whose name involves a number, with that number being value of a counter. \documentclass[varwidth,border=5mm]{standalone} \begin{document} % COUNTER \newcounter{myC} \addtocounter{myC}{...
6
votes
2answers
336 views

Issue with Expansions of Nested Macros

I am trying to incorporate membership checking macros by marmot and Phelype Oleinik (thanks to both of them for their neat solutions) into an argument of another macro: \singleBox{\...
1
vote
1answer
43 views

Problem with expansion in xparse

I am trying to use function from this answer \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewExpandableDocumentCommand{\switchcondition}{ O{string} m m m } { \use:c { ...
1
vote
2answers
51 views

Writing into a file, strange behavior with expansion

I want to write some data into a file, but I need to replace a subtring in these data beforehand. For some reason, the expansion through the \write macro does not give the expected result. Instead, a ...

1 2 3 4 5 17