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
Different PCB color ( is it different material? )
What caused the tendency for conservatives to not support climate change reform?
Compact Mechanical Energy Source
What does the behaviour of water on the skin of an aircraft in flight tell us?
Crossword gone overboard
Crossing US border with music files I'm legally allowed to possess
Smart people send dumb people to a new planet on a space craft that crashes into a body of water
How to properly maintain eye contact with people that have distinct facial features?
What are these (utility?) boxes at the side of the house?
How to prevent bad sectors?
Comment dit-on « I’ll tell you what » ?
How do Russian speakers idiomatically express the idea of "Ce n’est pas donné à tout le monde de ..." in French?
What is the best linguistic term for describing the kw > p / gw > b change, and its usual companion s > h
What F1 in name of seeds/varieties means?
What does uniform continuity mean exactly?
Preserving culinary oils
Yandex Programming Contest: Alarms
Which noble houses were destroyed during the Game of Thrones?
Is it possible to change original filename of an exe?
1960s sci-fi novella with a character who is treated as invisible by being ignored
How do I subvert the tropes of a train heist?
Why did this prime-sequence puzzle not work?
Can a wire having a 610-670 THz (frequency of blue light) AC frequency supply, generate blue light?
I think I may have violated academic integrity last year - what should I do?
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
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
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
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
asked May 15 at 3:40
GamrCorpsGamrCorps
1313
1313
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
May 17 at 7:06
@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
May 17 at 11:07
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
May 17 at 10:35
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
May 17 at 10:43
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
May 17 at 10:54
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
May 17 at 5:48
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
);
);
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,73811732
5,73811732
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
May 17 at 7:06
@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
May 17 at 11:07
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
May 17 at 7:06
@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
May 17 at 11:07
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,97314
2,97314
TrytopictextbfA test
– egreg
May 17 at 7:06
@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
May 17 at 11:07
add a comment |
TrytopictextbfA test
– egreg
May 17 at 7:06
@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
May 17 at 11:07
Try
topictextbfA test
– egreg
May 17 at 7:06
Try
topictextbfA test
– egreg
May 17 at 7:06
@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
May 17 at 11:07
@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
May 17 at 11:07
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
May 17 at 10:35
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
May 17 at 10:43
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
May 17 at 10:54
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
May 17 at 10:35
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
May 17 at 10:43
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
May 17 at 10:54
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 May 17 at 5:50
answered May 15 at 7:11
Raoul KesselsRaoul Kessels
2,021517
2,021517
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
May 17 at 10:35
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
May 17 at 10:43
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
May 17 at 10:54
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
May 17 at 10:35
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
May 17 at 10:43
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
May 17 at 10:54
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
May 17 at 10:35
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
May 17 at 10:35
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
May 17 at 10:43
@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
May 17 at 10:43
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
May 17 at 10:54
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
May 17 at 10:54
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
May 17 at 5:48
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
May 17 at 5:48
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 May 17 at 10:58
answered May 16 at 17:53
Ulrich DiezUlrich Diez
6,060620
6,060620
Thx for the reference :-)
– Raoul Kessels
May 17 at 5:48
add a comment |
Thx for the reference :-)
– Raoul Kessels
May 17 at 5:48
Thx for the reference :-)
– Raoul Kessels
May 17 at 5:48
Thx for the reference :-)
– Raoul Kessels
May 17 at 5:48
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
744k8919493287
744k8919493287
add a comment |
add a comment |
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