Appending to a command while also expanding a counterExpanding (edef) a lipsum command?Save current value of counter in a commandExpanding command in addchaptertocentryPartially expanding a commandExpanding all arguments of a command before appending it to another macroSave counter value inside command?Appending a command with evaluated parameters to a macronew command is expanding as blankForce expansion of counter value in command argumentCollecting into a command values of a counter
How does the Earth's center produce heat?
size of pointers and architecture
Why "strap-on" boosters, and how do other people say it?
VHDL: Why is it hard to design a floating point unit in hardware?
How would a physicist explain this starship engine?
Are there historical examples of audiences drawn to a work that was "so bad it's good"?
DeleteCases using two lists but with partial match?
Why is unzipped file smaller than zipped file
Real Analysis: Proof of the equivalent definitions of the derivative.
Surface of the 3x3x3 cube as a graph
Why is the reciprocal used in fraction division?
If a character has cast the Fly spell on themselves, can they "hand off" to the Levitate spell without interruption?
Palindromic Pan digital Special Square wants you to reveal its Root
Split into three!
Is it normal to "extract a paper" from a master thesis?
How many wires should be in a new thermostat cable?
Meaning of "half-crown enclosure"
Illustrating that universal optimality is stronger than sphere packing
Caught with my phone during an exam
Ribbon Cable Cross Talk - Is there a fix after the fact?
Why the work done is positive when bringing 2 opposite charges together?
Does science define life as "beginning at conception"?
Shell builtin `printf` line limit?
If change in free energy (G) is positive, how do those reactions still occur?
Appending to a command while also expanding a counter
Expanding (edef) a lipsum command?Save current value of counter in a commandExpanding command in addchaptertocentryPartially expanding a commandExpanding all arguments of a command before appending it to another macroSave counter value inside command?Appending a command with evaluated parameters to a macronew command is expanding as blankForce expansion of counter value in command argumentCollecting into a command values of a counter
I want to append some text to a command but expand a counter while doing so. Here is an example of what the problem is:
MWE:
documentclassarticle
newcommandtopics
newcountertopic
newcommandtopic[1]stepcountertopicexpandafterdefexpandaftertopicsexpandaftertopicshangindent=0.5cm Topic #thetopic: #1par
topictest A
topictest B
topictest C
begindocument
topics
enddocument
This produces:
Topic #3: test A
Topic #3: test B
Topic #3: test C
But it should produce:
Topic #1: test A
Topic #2: test B
Topic #3: test C
I've also tried this, to no avail:
newcommandtopic[1]stepcountertopicg@addto@macrotopicshangindent=0.5cm Topic #thetopic: #1par
macros counters expansion
New contributor
add a comment |
I want to append some text to a command but expand a counter while doing so. Here is an example of what the problem is:
MWE:
documentclassarticle
newcommandtopics
newcountertopic
newcommandtopic[1]stepcountertopicexpandafterdefexpandaftertopicsexpandaftertopicshangindent=0.5cm Topic #thetopic: #1par
topictest A
topictest B
topictest C
begindocument
topics
enddocument
This produces:
Topic #3: test A
Topic #3: test B
Topic #3: test C
But it should produce:
Topic #1: test A
Topic #2: test B
Topic #3: test C
I've also tried this, to no avail:
newcommandtopic[1]stepcountertopicg@addto@macrotopicshangindent=0.5cm Topic #thetopic: #1par
macros counters expansion
New contributor
add a comment |
I want to append some text to a command but expand a counter while doing so. Here is an example of what the problem is:
MWE:
documentclassarticle
newcommandtopics
newcountertopic
newcommandtopic[1]stepcountertopicexpandafterdefexpandaftertopicsexpandaftertopicshangindent=0.5cm Topic #thetopic: #1par
topictest A
topictest B
topictest C
begindocument
topics
enddocument
This produces:
Topic #3: test A
Topic #3: test B
Topic #3: test C
But it should produce:
Topic #1: test A
Topic #2: test B
Topic #3: test C
I've also tried this, to no avail:
newcommandtopic[1]stepcountertopicg@addto@macrotopicshangindent=0.5cm Topic #thetopic: #1par
macros counters expansion
New contributor
I want to append some text to a command but expand a counter while doing so. Here is an example of what the problem is:
MWE:
documentclassarticle
newcommandtopics
newcountertopic
newcommandtopic[1]stepcountertopicexpandafterdefexpandaftertopicsexpandaftertopicshangindent=0.5cm Topic #thetopic: #1par
topictest A
topictest B
topictest C
begindocument
topics
enddocument
This produces:
Topic #3: test A
Topic #3: test B
Topic #3: test C
But it should produce:
Topic #1: test A
Topic #2: test B
Topic #3: test C
I've also tried this, to no avail:
newcommandtopic[1]stepcountertopicg@addto@macrotopicshangindent=0.5cm Topic #thetopic: #1par
macros counters expansion
macros counters expansion
New contributor
New contributor
New contributor
asked May 15 at 3:40
GamrCorpsGamrCorps
1313
1313
New contributor
New contributor
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
You can use edef
here:
newcommandtopic[1]%
stepcountertopic%
edeftopicsunexpandedexpandaftertopicshangindent=0.5cm
noexpandtextbfTopic #thetopic: unexpanded#1par%
The unexpandedexpandafter...
expands the current topics
list once but prevents it from being fully expanded. This is important on later calls of topic
to make the expansion not break other commands that shouldn't be fully expanded (see the use of textbf
in my example).
Using this macros gives the desired output
add a comment |
documentclassarticle
newcommandtopics
newcountertopic
newcommandtopic[1]%
stepcountertopic%
edeftopicstopicshangindent=0.5cm Topic #thetopic: #1par
topictest A
topictest B
topictest C
begindocument
topics
enddocument
TrytopictextbfA test
– egreg
2 days ago
@egreg I think that's why in siracusa's answerunexpanded
is used... @Red-Cloud: Another approach could be putting#1
into a token-register and taking advantage of the fact thatedef
won't expand the result ofthemytoks
any further...
– Ulrich Diez
2 days ago
add a comment |
Your macro works fine if you move the stepcounter
after the expansion of topics
:
documentclassarticle
deftopics
newcountertopic
deftopics
deftopic#1%
expandafterdefexpandaftertopicsexpandaftertopics
stepcountertopic
hangindent=0.5cm
Topic #thetopic: #1par
topictest A
topictest B
topictest C
begindocument
topics
enddocument
Since topics
is only expanded once, you can have al kind of macros in it and in the argument of topic
.
Due to (La)TeX'sendlinechar
-thingie at the time of reading and tokenizing you will get a space token behindstepcountertopics
. The first one might cause undesired horizontal glue as LaTeX might not yet have left horizontal mode whentopics
gets executed. Subsequent ones in any case are preceded bypar
which implies that LaTeX will encounter them while in vertical mode where space tokens don't yield (horizontal) glue. Perhaps it is a good idea to initialize thetopics
-macro not to emptiness but topar
.
– Ulrich Diez
2 days ago
1
@UlrichDiez Thanks, but I just wanted show the OP that his macro was almost good. I have not intended to make a better macro. It is just that when you make a macro and it does not work and everything seems ok, it is very frustrating. So if it is only a detail, it is nice to know that you were not so wrong. At least, that is what happens to me and, I think, to many other non-TeXperts.
– Raoul Kessels
2 days ago
I think I can understand your feelings. ;-) For me (La)TeX is fun as long as I have the leisure and the muse for facing the challenge. But getting stuck and struggling with one inscrutable error message before the next while having to work productively and meeting deadlines is a horror.
– Ulrich Diez
2 days ago
add a comment |
In case you both don't wish to use edef
(which would also totally expand the token list contained in the argument #1
of the topic
-macro) and don't wish to integrate the stepcounter
-directives into the topics
-macro, you can easily use a combination of romannumeral0
-expansion and exchange
— the gist is:
romannumeral
is used for "gobbling" tokens that form a number and in return for them delivering character-tokens which form the representation of that number in lowercase roman numerals.romannumeral
in any case triggers "gobbling" those tokens that form the number. But in case that number does form an integer which is not positive, silently, i.e., without error-message or the like, no tokens at all will be delivered in return.
Besides this when searching for more digits or the terminating space-token of a digit-sequence, romannumeral
will trigger expansion of expandable tokens. With a digit-sequence, the terminating space-token will be removed silently.
Thus you can (ab?)use romannumeral
for having (La)TeX doing a lot of expansion-work as long as it is ensured that in the end romannumeral
will find a non-positive integral number.
In the example below romannumeral
is used for triggering expansion of thetopic
three times (because expansion needs to be triggered three times until you get the value of the topic-counter) before exchanging things and triggering expansion of the topics
-macro once.
In the example below each instance of romannumeral
will in the end find the digit(-sequence) "0", which is formed by the character-token(-sequence) 0
, trailed by a space-token (which terminates that digit-sequence(character-token-sequence) and thus will take these things for the non-positive number 0 and therefore will "gobble" both the character-token "0" which forms the digit(-sequence)/number "0" and the space-token while silently in return for them not delivering any token at all.
documentclassarticle
newcommandexchange[2]#2#1%
newcommand*topics%
newcountertopic
newcommandtopic[1]%
stepcountertopic%
expandafterdefexpandaftertopicsexpandafter%
romannumeral0%
expandafterexchangeexpandafter%
romannumeral0exchange %
expandafterexpandafterexpandafter
expandafterexpandafterexpandafter
expandafter
%
thetopic: #1par%
ifxtopicsempty
exchange expandafterexpandafterexpandafter%
expandafterexpandafterexpandafterpar
else
exchange expandafterexpandafterexpandafter%
fi
topicshangindent=0.5cm Topic #%
%
%
%
topictest A
topictest B
topictest C
%showtopics
begindocument
frenchspacingtextttstringtopics: meaningtopics%
bigskip
noindenthrulefillnull
bigskip
topics
enddocument
Nonetheless the solution provided by Raoul Kessels, where stepcounter
-directives get integrated into the topics
-macro is preferable because when you do it in Raoul Kessel's way you can instead also use refstepcounter
-directives and place referencing-label
s into the argument of the topic
-macro for referencing single topics by means of the label
-(page)ref
-mechanism.
Thx for the reference :-)
– Raoul Kessels
2 days ago
add a comment |
You need to expand thetopic
before doing the append operation:
documentclassarticle
newcommandtopics
newcountertopic
newcommandtopic[1]%
stepcountertopic%
% newer versions of the engines (TeX Live 2019)
expandafterappendtotopicsexpandafterexpandedthetopic#1%
% for older versions of the TeX engines use the following
%begingroupedefxendgroupnoexpandappendtotopicsthetopicx#1%
newcommandappendtotopics[2]%
expandafterdefexpandaftertopicsexpandafter%
topicshangindent=0.5cm Topic ##1: #2par
%
topictest A
topictest B
topictest C
begindocument
topics
enddocument
An expl3
version:
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewDocumentCommandtopics
tl_use:N g_kessels_topics_tl
NewDocumentCommandtopicm
kessels_topic:n #1
int_new:N g_kessels_topic_int
tl_new:N g_kessels_topics_tl
cs_new_protected:Nn kessels_topic:n
int_gincr:N g_kessels_topic_int
__kessels_topic_add:fn int_to_arabic:n g_kessels_topic_int #1
cs_new_protected:Nn __kessels_topic_add:nn
tl_gput_right:Nn g_kessels_topics_tl hangindent=0.5cm~Topic~##1:~#2par
cs_generate_variant:Nn __kessels_topic_add:nn f
ExplSyntaxOff
topictest A
topictest B
topictest C
begindocument
topics
enddocument
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "85"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
GamrCorps is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f490905%2fappending-to-a-command-while-also-expanding-a-counter%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use edef
here:
newcommandtopic[1]%
stepcountertopic%
edeftopicsunexpandedexpandaftertopicshangindent=0.5cm
noexpandtextbfTopic #thetopic: unexpanded#1par%
The unexpandedexpandafter...
expands the current topics
list once but prevents it from being fully expanded. This is important on later calls of topic
to make the expansion not break other commands that shouldn't be fully expanded (see the use of textbf
in my example).
Using this macros gives the desired output
add a comment |
You can use edef
here:
newcommandtopic[1]%
stepcountertopic%
edeftopicsunexpandedexpandaftertopicshangindent=0.5cm
noexpandtextbfTopic #thetopic: unexpanded#1par%
The unexpandedexpandafter...
expands the current topics
list once but prevents it from being fully expanded. This is important on later calls of topic
to make the expansion not break other commands that shouldn't be fully expanded (see the use of textbf
in my example).
Using this macros gives the desired output
add a comment |
You can use edef
here:
newcommandtopic[1]%
stepcountertopic%
edeftopicsunexpandedexpandaftertopicshangindent=0.5cm
noexpandtextbfTopic #thetopic: unexpanded#1par%
The unexpandedexpandafter...
expands the current topics
list once but prevents it from being fully expanded. This is important on later calls of topic
to make the expansion not break other commands that shouldn't be fully expanded (see the use of textbf
in my example).
Using this macros gives the desired output
You can use edef
here:
newcommandtopic[1]%
stepcountertopic%
edeftopicsunexpandedexpandaftertopicshangindent=0.5cm
noexpandtextbfTopic #thetopic: unexpanded#1par%
The unexpandedexpandafter...
expands the current topics
list once but prevents it from being fully expanded. This is important on later calls of topic
to make the expansion not break other commands that shouldn't be fully expanded (see the use of textbf
in my example).
Using this macros gives the desired output
edited May 15 at 5:04
answered May 15 at 4:01
siracusasiracusa
5,56711730
5,56711730
add a comment |
add a comment |
documentclassarticle
newcommandtopics
newcountertopic
newcommandtopic[1]%
stepcountertopic%
edeftopicstopicshangindent=0.5cm Topic #thetopic: #1par
topictest A
topictest B
topictest C
begindocument
topics
enddocument
TrytopictextbfA test
– egreg
2 days ago
@egreg I think that's why in siracusa's answerunexpanded
is used... @Red-Cloud: Another approach could be putting#1
into a token-register and taking advantage of the fact thatedef
won't expand the result ofthemytoks
any further...
– Ulrich Diez
2 days ago
add a comment |
documentclassarticle
newcommandtopics
newcountertopic
newcommandtopic[1]%
stepcountertopic%
edeftopicstopicshangindent=0.5cm Topic #thetopic: #1par
topictest A
topictest B
topictest C
begindocument
topics
enddocument
TrytopictextbfA test
– egreg
2 days ago
@egreg I think that's why in siracusa's answerunexpanded
is used... @Red-Cloud: Another approach could be putting#1
into a token-register and taking advantage of the fact thatedef
won't expand the result ofthemytoks
any further...
– Ulrich Diez
2 days ago
add a comment |
documentclassarticle
newcommandtopics
newcountertopic
newcommandtopic[1]%
stepcountertopic%
edeftopicstopicshangindent=0.5cm Topic #thetopic: #1par
topictest A
topictest B
topictest C
begindocument
topics
enddocument
documentclassarticle
newcommandtopics
newcountertopic
newcommandtopic[1]%
stepcountertopic%
edeftopicstopicshangindent=0.5cm Topic #thetopic: #1par
topictest A
topictest B
topictest C
begindocument
topics
enddocument
answered May 15 at 4:38
Red-CloudRed-Cloud
2,52813
2,52813
TrytopictextbfA test
– egreg
2 days ago
@egreg I think that's why in siracusa's answerunexpanded
is used... @Red-Cloud: Another approach could be putting#1
into a token-register and taking advantage of the fact thatedef
won't expand the result ofthemytoks
any further...
– Ulrich Diez
2 days ago
add a comment |
TrytopictextbfA test
– egreg
2 days ago
@egreg I think that's why in siracusa's answerunexpanded
is used... @Red-Cloud: Another approach could be putting#1
into a token-register and taking advantage of the fact thatedef
won't expand the result ofthemytoks
any further...
– Ulrich Diez
2 days ago
Try
topictextbfA test
– egreg
2 days ago
Try
topictextbfA test
– egreg
2 days ago
@egreg I think that's why in siracusa's answer
unexpanded
is used... @Red-Cloud: Another approach could be putting #1
into a token-register and taking advantage of the fact that edef
won't expand the result of themytoks
any further...– Ulrich Diez
2 days ago
@egreg I think that's why in siracusa's answer
unexpanded
is used... @Red-Cloud: Another approach could be putting #1
into a token-register and taking advantage of the fact that edef
won't expand the result of themytoks
any further...– Ulrich Diez
2 days ago
add a comment |
Your macro works fine if you move the stepcounter
after the expansion of topics
:
documentclassarticle
deftopics
newcountertopic
deftopics
deftopic#1%
expandafterdefexpandaftertopicsexpandaftertopics
stepcountertopic
hangindent=0.5cm
Topic #thetopic: #1par
topictest A
topictest B
topictest C
begindocument
topics
enddocument
Since topics
is only expanded once, you can have al kind of macros in it and in the argument of topic
.
Due to (La)TeX'sendlinechar
-thingie at the time of reading and tokenizing you will get a space token behindstepcountertopics
. The first one might cause undesired horizontal glue as LaTeX might not yet have left horizontal mode whentopics
gets executed. Subsequent ones in any case are preceded bypar
which implies that LaTeX will encounter them while in vertical mode where space tokens don't yield (horizontal) glue. Perhaps it is a good idea to initialize thetopics
-macro not to emptiness but topar
.
– Ulrich Diez
2 days ago
1
@UlrichDiez Thanks, but I just wanted show the OP that his macro was almost good. I have not intended to make a better macro. It is just that when you make a macro and it does not work and everything seems ok, it is very frustrating. So if it is only a detail, it is nice to know that you were not so wrong. At least, that is what happens to me and, I think, to many other non-TeXperts.
– Raoul Kessels
2 days ago
I think I can understand your feelings. ;-) For me (La)TeX is fun as long as I have the leisure and the muse for facing the challenge. But getting stuck and struggling with one inscrutable error message before the next while having to work productively and meeting deadlines is a horror.
– Ulrich Diez
2 days ago
add a comment |
Your macro works fine if you move the stepcounter
after the expansion of topics
:
documentclassarticle
deftopics
newcountertopic
deftopics
deftopic#1%
expandafterdefexpandaftertopicsexpandaftertopics
stepcountertopic
hangindent=0.5cm
Topic #thetopic: #1par
topictest A
topictest B
topictest C
begindocument
topics
enddocument
Since topics
is only expanded once, you can have al kind of macros in it and in the argument of topic
.
Due to (La)TeX'sendlinechar
-thingie at the time of reading and tokenizing you will get a space token behindstepcountertopics
. The first one might cause undesired horizontal glue as LaTeX might not yet have left horizontal mode whentopics
gets executed. Subsequent ones in any case are preceded bypar
which implies that LaTeX will encounter them while in vertical mode where space tokens don't yield (horizontal) glue. Perhaps it is a good idea to initialize thetopics
-macro not to emptiness but topar
.
– Ulrich Diez
2 days ago
1
@UlrichDiez Thanks, but I just wanted show the OP that his macro was almost good. I have not intended to make a better macro. It is just that when you make a macro and it does not work and everything seems ok, it is very frustrating. So if it is only a detail, it is nice to know that you were not so wrong. At least, that is what happens to me and, I think, to many other non-TeXperts.
– Raoul Kessels
2 days ago
I think I can understand your feelings. ;-) For me (La)TeX is fun as long as I have the leisure and the muse for facing the challenge. But getting stuck and struggling with one inscrutable error message before the next while having to work productively and meeting deadlines is a horror.
– Ulrich Diez
2 days ago
add a comment |
Your macro works fine if you move the stepcounter
after the expansion of topics
:
documentclassarticle
deftopics
newcountertopic
deftopics
deftopic#1%
expandafterdefexpandaftertopicsexpandaftertopics
stepcountertopic
hangindent=0.5cm
Topic #thetopic: #1par
topictest A
topictest B
topictest C
begindocument
topics
enddocument
Since topics
is only expanded once, you can have al kind of macros in it and in the argument of topic
.
Your macro works fine if you move the stepcounter
after the expansion of topics
:
documentclassarticle
deftopics
newcountertopic
deftopics
deftopic#1%
expandafterdefexpandaftertopicsexpandaftertopics
stepcountertopic
hangindent=0.5cm
Topic #thetopic: #1par
topictest A
topictest B
topictest C
begindocument
topics
enddocument
Since topics
is only expanded once, you can have al kind of macros in it and in the argument of topic
.
edited 2 days ago
answered May 15 at 7:11
Raoul KesselsRaoul Kessels
1,986517
1,986517
Due to (La)TeX'sendlinechar
-thingie at the time of reading and tokenizing you will get a space token behindstepcountertopics
. The first one might cause undesired horizontal glue as LaTeX might not yet have left horizontal mode whentopics
gets executed. Subsequent ones in any case are preceded bypar
which implies that LaTeX will encounter them while in vertical mode where space tokens don't yield (horizontal) glue. Perhaps it is a good idea to initialize thetopics
-macro not to emptiness but topar
.
– Ulrich Diez
2 days ago
1
@UlrichDiez Thanks, but I just wanted show the OP that his macro was almost good. I have not intended to make a better macro. It is just that when you make a macro and it does not work and everything seems ok, it is very frustrating. So if it is only a detail, it is nice to know that you were not so wrong. At least, that is what happens to me and, I think, to many other non-TeXperts.
– Raoul Kessels
2 days ago
I think I can understand your feelings. ;-) For me (La)TeX is fun as long as I have the leisure and the muse for facing the challenge. But getting stuck and struggling with one inscrutable error message before the next while having to work productively and meeting deadlines is a horror.
– Ulrich Diez
2 days ago
add a comment |
Due to (La)TeX'sendlinechar
-thingie at the time of reading and tokenizing you will get a space token behindstepcountertopics
. The first one might cause undesired horizontal glue as LaTeX might not yet have left horizontal mode whentopics
gets executed. Subsequent ones in any case are preceded bypar
which implies that LaTeX will encounter them while in vertical mode where space tokens don't yield (horizontal) glue. Perhaps it is a good idea to initialize thetopics
-macro not to emptiness but topar
.
– Ulrich Diez
2 days ago
1
@UlrichDiez Thanks, but I just wanted show the OP that his macro was almost good. I have not intended to make a better macro. It is just that when you make a macro and it does not work and everything seems ok, it is very frustrating. So if it is only a detail, it is nice to know that you were not so wrong. At least, that is what happens to me and, I think, to many other non-TeXperts.
– Raoul Kessels
2 days ago
I think I can understand your feelings. ;-) For me (La)TeX is fun as long as I have the leisure and the muse for facing the challenge. But getting stuck and struggling with one inscrutable error message before the next while having to work productively and meeting deadlines is a horror.
– Ulrich Diez
2 days ago
Due to (La)TeX's
endlinechar
-thingie at the time of reading and tokenizing you will get a space token behind stepcountertopics
. The first one might cause undesired horizontal glue as LaTeX might not yet have left horizontal mode when topics
gets executed. Subsequent ones in any case are preceded by par
which implies that LaTeX will encounter them while in vertical mode where space tokens don't yield (horizontal) glue. Perhaps it is a good idea to initialize the topics
-macro not to emptiness but to par
.– Ulrich Diez
2 days ago
Due to (La)TeX's
endlinechar
-thingie at the time of reading and tokenizing you will get a space token behind stepcountertopics
. The first one might cause undesired horizontal glue as LaTeX might not yet have left horizontal mode when topics
gets executed. Subsequent ones in any case are preceded by par
which implies that LaTeX will encounter them while in vertical mode where space tokens don't yield (horizontal) glue. Perhaps it is a good idea to initialize the topics
-macro not to emptiness but to par
.– Ulrich Diez
2 days ago
1
1
@UlrichDiez Thanks, but I just wanted show the OP that his macro was almost good. I have not intended to make a better macro. It is just that when you make a macro and it does not work and everything seems ok, it is very frustrating. So if it is only a detail, it is nice to know that you were not so wrong. At least, that is what happens to me and, I think, to many other non-TeXperts.
– Raoul Kessels
2 days ago
@UlrichDiez Thanks, but I just wanted show the OP that his macro was almost good. I have not intended to make a better macro. It is just that when you make a macro and it does not work and everything seems ok, it is very frustrating. So if it is only a detail, it is nice to know that you were not so wrong. At least, that is what happens to me and, I think, to many other non-TeXperts.
– Raoul Kessels
2 days ago
I think I can understand your feelings. ;-) For me (La)TeX is fun as long as I have the leisure and the muse for facing the challenge. But getting stuck and struggling with one inscrutable error message before the next while having to work productively and meeting deadlines is a horror.
– Ulrich Diez
2 days ago
I think I can understand your feelings. ;-) For me (La)TeX is fun as long as I have the leisure and the muse for facing the challenge. But getting stuck and struggling with one inscrutable error message before the next while having to work productively and meeting deadlines is a horror.
– Ulrich Diez
2 days ago
add a comment |
In case you both don't wish to use edef
(which would also totally expand the token list contained in the argument #1
of the topic
-macro) and don't wish to integrate the stepcounter
-directives into the topics
-macro, you can easily use a combination of romannumeral0
-expansion and exchange
— the gist is:
romannumeral
is used for "gobbling" tokens that form a number and in return for them delivering character-tokens which form the representation of that number in lowercase roman numerals.romannumeral
in any case triggers "gobbling" those tokens that form the number. But in case that number does form an integer which is not positive, silently, i.e., without error-message or the like, no tokens at all will be delivered in return.
Besides this when searching for more digits or the terminating space-token of a digit-sequence, romannumeral
will trigger expansion of expandable tokens. With a digit-sequence, the terminating space-token will be removed silently.
Thus you can (ab?)use romannumeral
for having (La)TeX doing a lot of expansion-work as long as it is ensured that in the end romannumeral
will find a non-positive integral number.
In the example below romannumeral
is used for triggering expansion of thetopic
three times (because expansion needs to be triggered three times until you get the value of the topic-counter) before exchanging things and triggering expansion of the topics
-macro once.
In the example below each instance of romannumeral
will in the end find the digit(-sequence) "0", which is formed by the character-token(-sequence) 0
, trailed by a space-token (which terminates that digit-sequence(character-token-sequence) and thus will take these things for the non-positive number 0 and therefore will "gobble" both the character-token "0" which forms the digit(-sequence)/number "0" and the space-token while silently in return for them not delivering any token at all.
documentclassarticle
newcommandexchange[2]#2#1%
newcommand*topics%
newcountertopic
newcommandtopic[1]%
stepcountertopic%
expandafterdefexpandaftertopicsexpandafter%
romannumeral0%
expandafterexchangeexpandafter%
romannumeral0exchange %
expandafterexpandafterexpandafter
expandafterexpandafterexpandafter
expandafter
%
thetopic: #1par%
ifxtopicsempty
exchange expandafterexpandafterexpandafter%
expandafterexpandafterexpandafterpar
else
exchange expandafterexpandafterexpandafter%
fi
topicshangindent=0.5cm Topic #%
%
%
%
topictest A
topictest B
topictest C
%showtopics
begindocument
frenchspacingtextttstringtopics: meaningtopics%
bigskip
noindenthrulefillnull
bigskip
topics
enddocument
Nonetheless the solution provided by Raoul Kessels, where stepcounter
-directives get integrated into the topics
-macro is preferable because when you do it in Raoul Kessel's way you can instead also use refstepcounter
-directives and place referencing-label
s into the argument of the topic
-macro for referencing single topics by means of the label
-(page)ref
-mechanism.
Thx for the reference :-)
– Raoul Kessels
2 days ago
add a comment |
In case you both don't wish to use edef
(which would also totally expand the token list contained in the argument #1
of the topic
-macro) and don't wish to integrate the stepcounter
-directives into the topics
-macro, you can easily use a combination of romannumeral0
-expansion and exchange
— the gist is:
romannumeral
is used for "gobbling" tokens that form a number and in return for them delivering character-tokens which form the representation of that number in lowercase roman numerals.romannumeral
in any case triggers "gobbling" those tokens that form the number. But in case that number does form an integer which is not positive, silently, i.e., without error-message or the like, no tokens at all will be delivered in return.
Besides this when searching for more digits or the terminating space-token of a digit-sequence, romannumeral
will trigger expansion of expandable tokens. With a digit-sequence, the terminating space-token will be removed silently.
Thus you can (ab?)use romannumeral
for having (La)TeX doing a lot of expansion-work as long as it is ensured that in the end romannumeral
will find a non-positive integral number.
In the example below romannumeral
is used for triggering expansion of thetopic
three times (because expansion needs to be triggered three times until you get the value of the topic-counter) before exchanging things and triggering expansion of the topics
-macro once.
In the example below each instance of romannumeral
will in the end find the digit(-sequence) "0", which is formed by the character-token(-sequence) 0
, trailed by a space-token (which terminates that digit-sequence(character-token-sequence) and thus will take these things for the non-positive number 0 and therefore will "gobble" both the character-token "0" which forms the digit(-sequence)/number "0" and the space-token while silently in return for them not delivering any token at all.
documentclassarticle
newcommandexchange[2]#2#1%
newcommand*topics%
newcountertopic
newcommandtopic[1]%
stepcountertopic%
expandafterdefexpandaftertopicsexpandafter%
romannumeral0%
expandafterexchangeexpandafter%
romannumeral0exchange %
expandafterexpandafterexpandafter
expandafterexpandafterexpandafter
expandafter
%
thetopic: #1par%
ifxtopicsempty
exchange expandafterexpandafterexpandafter%
expandafterexpandafterexpandafterpar
else
exchange expandafterexpandafterexpandafter%
fi
topicshangindent=0.5cm Topic #%
%
%
%
topictest A
topictest B
topictest C
%showtopics
begindocument
frenchspacingtextttstringtopics: meaningtopics%
bigskip
noindenthrulefillnull
bigskip
topics
enddocument
Nonetheless the solution provided by Raoul Kessels, where stepcounter
-directives get integrated into the topics
-macro is preferable because when you do it in Raoul Kessel's way you can instead also use refstepcounter
-directives and place referencing-label
s into the argument of the topic
-macro for referencing single topics by means of the label
-(page)ref
-mechanism.
Thx for the reference :-)
– Raoul Kessels
2 days ago
add a comment |
In case you both don't wish to use edef
(which would also totally expand the token list contained in the argument #1
of the topic
-macro) and don't wish to integrate the stepcounter
-directives into the topics
-macro, you can easily use a combination of romannumeral0
-expansion and exchange
— the gist is:
romannumeral
is used for "gobbling" tokens that form a number and in return for them delivering character-tokens which form the representation of that number in lowercase roman numerals.romannumeral
in any case triggers "gobbling" those tokens that form the number. But in case that number does form an integer which is not positive, silently, i.e., without error-message or the like, no tokens at all will be delivered in return.
Besides this when searching for more digits or the terminating space-token of a digit-sequence, romannumeral
will trigger expansion of expandable tokens. With a digit-sequence, the terminating space-token will be removed silently.
Thus you can (ab?)use romannumeral
for having (La)TeX doing a lot of expansion-work as long as it is ensured that in the end romannumeral
will find a non-positive integral number.
In the example below romannumeral
is used for triggering expansion of thetopic
three times (because expansion needs to be triggered three times until you get the value of the topic-counter) before exchanging things and triggering expansion of the topics
-macro once.
In the example below each instance of romannumeral
will in the end find the digit(-sequence) "0", which is formed by the character-token(-sequence) 0
, trailed by a space-token (which terminates that digit-sequence(character-token-sequence) and thus will take these things for the non-positive number 0 and therefore will "gobble" both the character-token "0" which forms the digit(-sequence)/number "0" and the space-token while silently in return for them not delivering any token at all.
documentclassarticle
newcommandexchange[2]#2#1%
newcommand*topics%
newcountertopic
newcommandtopic[1]%
stepcountertopic%
expandafterdefexpandaftertopicsexpandafter%
romannumeral0%
expandafterexchangeexpandafter%
romannumeral0exchange %
expandafterexpandafterexpandafter
expandafterexpandafterexpandafter
expandafter
%
thetopic: #1par%
ifxtopicsempty
exchange expandafterexpandafterexpandafter%
expandafterexpandafterexpandafterpar
else
exchange expandafterexpandafterexpandafter%
fi
topicshangindent=0.5cm Topic #%
%
%
%
topictest A
topictest B
topictest C
%showtopics
begindocument
frenchspacingtextttstringtopics: meaningtopics%
bigskip
noindenthrulefillnull
bigskip
topics
enddocument
Nonetheless the solution provided by Raoul Kessels, where stepcounter
-directives get integrated into the topics
-macro is preferable because when you do it in Raoul Kessel's way you can instead also use refstepcounter
-directives and place referencing-label
s into the argument of the topic
-macro for referencing single topics by means of the label
-(page)ref
-mechanism.
In case you both don't wish to use edef
(which would also totally expand the token list contained in the argument #1
of the topic
-macro) and don't wish to integrate the stepcounter
-directives into the topics
-macro, you can easily use a combination of romannumeral0
-expansion and exchange
— the gist is:
romannumeral
is used for "gobbling" tokens that form a number and in return for them delivering character-tokens which form the representation of that number in lowercase roman numerals.romannumeral
in any case triggers "gobbling" those tokens that form the number. But in case that number does form an integer which is not positive, silently, i.e., without error-message or the like, no tokens at all will be delivered in return.
Besides this when searching for more digits or the terminating space-token of a digit-sequence, romannumeral
will trigger expansion of expandable tokens. With a digit-sequence, the terminating space-token will be removed silently.
Thus you can (ab?)use romannumeral
for having (La)TeX doing a lot of expansion-work as long as it is ensured that in the end romannumeral
will find a non-positive integral number.
In the example below romannumeral
is used for triggering expansion of thetopic
three times (because expansion needs to be triggered three times until you get the value of the topic-counter) before exchanging things and triggering expansion of the topics
-macro once.
In the example below each instance of romannumeral
will in the end find the digit(-sequence) "0", which is formed by the character-token(-sequence) 0
, trailed by a space-token (which terminates that digit-sequence(character-token-sequence) and thus will take these things for the non-positive number 0 and therefore will "gobble" both the character-token "0" which forms the digit(-sequence)/number "0" and the space-token while silently in return for them not delivering any token at all.
documentclassarticle
newcommandexchange[2]#2#1%
newcommand*topics%
newcountertopic
newcommandtopic[1]%
stepcountertopic%
expandafterdefexpandaftertopicsexpandafter%
romannumeral0%
expandafterexchangeexpandafter%
romannumeral0exchange %
expandafterexpandafterexpandafter
expandafterexpandafterexpandafter
expandafter
%
thetopic: #1par%
ifxtopicsempty
exchange expandafterexpandafterexpandafter%
expandafterexpandafterexpandafterpar
else
exchange expandafterexpandafterexpandafter%
fi
topicshangindent=0.5cm Topic #%
%
%
%
topictest A
topictest B
topictest C
%showtopics
begindocument
frenchspacingtextttstringtopics: meaningtopics%
bigskip
noindenthrulefillnull
bigskip
topics
enddocument
Nonetheless the solution provided by Raoul Kessels, where stepcounter
-directives get integrated into the topics
-macro is preferable because when you do it in Raoul Kessel's way you can instead also use refstepcounter
-directives and place referencing-label
s into the argument of the topic
-macro for referencing single topics by means of the label
-(page)ref
-mechanism.
edited 2 days ago
answered May 16 at 17:53
Ulrich DiezUlrich Diez
5,950620
5,950620
Thx for the reference :-)
– Raoul Kessels
2 days ago
add a comment |
Thx for the reference :-)
– Raoul Kessels
2 days ago
Thx for the reference :-)
– Raoul Kessels
2 days ago
Thx for the reference :-)
– Raoul Kessels
2 days ago
add a comment |
You need to expand thetopic
before doing the append operation:
documentclassarticle
newcommandtopics
newcountertopic
newcommandtopic[1]%
stepcountertopic%
% newer versions of the engines (TeX Live 2019)
expandafterappendtotopicsexpandafterexpandedthetopic#1%
% for older versions of the TeX engines use the following
%begingroupedefxendgroupnoexpandappendtotopicsthetopicx#1%
newcommandappendtotopics[2]%
expandafterdefexpandaftertopicsexpandafter%
topicshangindent=0.5cm Topic ##1: #2par
%
topictest A
topictest B
topictest C
begindocument
topics
enddocument
An expl3
version:
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewDocumentCommandtopics
tl_use:N g_kessels_topics_tl
NewDocumentCommandtopicm
kessels_topic:n #1
int_new:N g_kessels_topic_int
tl_new:N g_kessels_topics_tl
cs_new_protected:Nn kessels_topic:n
int_gincr:N g_kessels_topic_int
__kessels_topic_add:fn int_to_arabic:n g_kessels_topic_int #1
cs_new_protected:Nn __kessels_topic_add:nn
tl_gput_right:Nn g_kessels_topics_tl hangindent=0.5cm~Topic~##1:~#2par
cs_generate_variant:Nn __kessels_topic_add:nn f
ExplSyntaxOff
topictest A
topictest B
topictest C
begindocument
topics
enddocument
add a comment |
You need to expand thetopic
before doing the append operation:
documentclassarticle
newcommandtopics
newcountertopic
newcommandtopic[1]%
stepcountertopic%
% newer versions of the engines (TeX Live 2019)
expandafterappendtotopicsexpandafterexpandedthetopic#1%
% for older versions of the TeX engines use the following
%begingroupedefxendgroupnoexpandappendtotopicsthetopicx#1%
newcommandappendtotopics[2]%
expandafterdefexpandaftertopicsexpandafter%
topicshangindent=0.5cm Topic ##1: #2par
%
topictest A
topictest B
topictest C
begindocument
topics
enddocument
An expl3
version:
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewDocumentCommandtopics
tl_use:N g_kessels_topics_tl
NewDocumentCommandtopicm
kessels_topic:n #1
int_new:N g_kessels_topic_int
tl_new:N g_kessels_topics_tl
cs_new_protected:Nn kessels_topic:n
int_gincr:N g_kessels_topic_int
__kessels_topic_add:fn int_to_arabic:n g_kessels_topic_int #1
cs_new_protected:Nn __kessels_topic_add:nn
tl_gput_right:Nn g_kessels_topics_tl hangindent=0.5cm~Topic~##1:~#2par
cs_generate_variant:Nn __kessels_topic_add:nn f
ExplSyntaxOff
topictest A
topictest B
topictest C
begindocument
topics
enddocument
add a comment |
You need to expand thetopic
before doing the append operation:
documentclassarticle
newcommandtopics
newcountertopic
newcommandtopic[1]%
stepcountertopic%
% newer versions of the engines (TeX Live 2019)
expandafterappendtotopicsexpandafterexpandedthetopic#1%
% for older versions of the TeX engines use the following
%begingroupedefxendgroupnoexpandappendtotopicsthetopicx#1%
newcommandappendtotopics[2]%
expandafterdefexpandaftertopicsexpandafter%
topicshangindent=0.5cm Topic ##1: #2par
%
topictest A
topictest B
topictest C
begindocument
topics
enddocument
An expl3
version:
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewDocumentCommandtopics
tl_use:N g_kessels_topics_tl
NewDocumentCommandtopicm
kessels_topic:n #1
int_new:N g_kessels_topic_int
tl_new:N g_kessels_topics_tl
cs_new_protected:Nn kessels_topic:n
int_gincr:N g_kessels_topic_int
__kessels_topic_add:fn int_to_arabic:n g_kessels_topic_int #1
cs_new_protected:Nn __kessels_topic_add:nn
tl_gput_right:Nn g_kessels_topics_tl hangindent=0.5cm~Topic~##1:~#2par
cs_generate_variant:Nn __kessels_topic_add:nn f
ExplSyntaxOff
topictest A
topictest B
topictest C
begindocument
topics
enddocument
You need to expand thetopic
before doing the append operation:
documentclassarticle
newcommandtopics
newcountertopic
newcommandtopic[1]%
stepcountertopic%
% newer versions of the engines (TeX Live 2019)
expandafterappendtotopicsexpandafterexpandedthetopic#1%
% for older versions of the TeX engines use the following
%begingroupedefxendgroupnoexpandappendtotopicsthetopicx#1%
newcommandappendtotopics[2]%
expandafterdefexpandaftertopicsexpandafter%
topicshangindent=0.5cm Topic ##1: #2par
%
topictest A
topictest B
topictest C
begindocument
topics
enddocument
An expl3
version:
documentclassarticle
usepackagexparse
ExplSyntaxOn
NewDocumentCommandtopics
tl_use:N g_kessels_topics_tl
NewDocumentCommandtopicm
kessels_topic:n #1
int_new:N g_kessels_topic_int
tl_new:N g_kessels_topics_tl
cs_new_protected:Nn kessels_topic:n
int_gincr:N g_kessels_topic_int
__kessels_topic_add:fn int_to_arabic:n g_kessels_topic_int #1
cs_new_protected:Nn __kessels_topic_add:nn
tl_gput_right:Nn g_kessels_topics_tl hangindent=0.5cm~Topic~##1:~#2par
cs_generate_variant:Nn __kessels_topic_add:nn f
ExplSyntaxOff
topictest A
topictest B
topictest C
begindocument
topics
enddocument
answered May 16 at 21:56
egregegreg
742k8919453277
742k8919453277
add a comment |
add a comment |
GamrCorps is a new contributor. Be nice, and check out our Code of Conduct.
GamrCorps is a new contributor. Be nice, and check out our Code of Conduct.
GamrCorps is a new contributor. Be nice, and check out our Code of Conduct.
GamrCorps is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f490905%2fappending-to-a-command-while-also-expanding-a-counter%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown