How to robustly store bits of text for later useHow to use def or similar command to store an arbitrary number of variables for later processing?How to collect text for later usage in appendix?How to store and append text for later use (inside enumerate environment)Save original variable/command for invocation laterWrap text to quote it later in the same documentCollecting contents of environment and store them for later retrievalHow reuse latex code for later expansion?How to store to-be-credited contributors for acknowledgment later in the appendixStore values, text, commands in variablesHow to store multiple fragile definitions for re-importing later

Output visual diagram of picture

Do native speakers use "ultima" and "proxima" frequently in spoken English?

Pre-Employment Background Check With Consent For Future Checks

What's the meaning of "what it means for something to be something"?

Writing in a Christian voice

Why is indicated airspeed rather than ground speed used during the takeoff roll?

How are passwords stolen from companies if they only store hashes?

Mortal danger in mid-grade literature

Is there any common country to visit for persons holding UK and Schengen visas?

Magnifying glass in hyperbolic space

Why do Radio Buttons not fill the entire outer circle?

Did I make a mistake by ccing email to boss to others?

Is there a distance limit for minecart tracks?

I keep switching characters, how do I stop?

Connection Between Knot Theory and Number Theory

categorizing a variable turns it from insignificant to significant

Asserting that Atheism and Theism are both faith based positions

Not hide and seek

Offset in split text content

How would a solely written language work mechanically

Air travel with refrigerated insulin

Strange behavior in TikZ draw command

If the Dominion rule using their Jem'Hadar troops, why is their life expectancy so low?

Make a Bowl of Alphabet Soup



How to robustly store bits of text for later use


How to use def or similar command to store an arbitrary number of variables for later processing?How to collect text for later usage in appendix?How to store and append text for later use (inside enumerate environment)Save original variable/command for invocation laterWrap text to quote it later in the same documentCollecting contents of environment and store them for later retrievalHow reuse latex code for later expansion?How to store to-be-credited contributors for acknowledgment later in the appendixStore values, text, commands in variablesHow to store multiple fragile definitions for re-importing later













5















I'm trying to write myself a package, similar to the exercise package, so I can prepare problem sets for my students. I wish to delay the output of problems and/or solutions until a suitable place in the document has been reached. I have something that sort of works but it's kind of clunky. For example, this works pretty well:



usepackageenviron
NewEnvirontestbglobalexpandafterletcsname barendcsnameBODY


I use csname because the names are dynamically generated. This doesn't work so well if the command is anything other than BODY. For example, if I do letbarBODY, latex has an aneurysm (it binds bar to just {).



There is an alternate way of doing this, which is:



NewEnvirontestcglobalexpandafteredefcsname fooendcsnameBODY


This sort of works. The following thing works:
begintestchiendtestc, but this gives LaTeX an aneurysm: begintestcbf hiendtestc. (The error message is incomplete). I tried to debug it myself but I get hopelessly tangled up in packages that I don't understand. If you do it with tiny instead of bf you get the wildly different LaTeX error message: ! TeX capacity exceeded, sorry [input stack size=5000].



How can I squirrel away for later, not just BODY, but some sophisticated combination of BODY and other stuff, while having it robust to what's between the begin and end?



Edit: ok multiple solutions below










share|improve this question









New contributor




Sébastien Loisel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1





    Why not use def? let does what it should do: use the the next token.

    – TeXnician
    10 hours ago











  • If you mean this: NewEnvirontestcglobalexpandafterdefcsname fooendcsnameBODY, I get: ! Undefined control sequence. foo ->BODY

    – Sébastien Loisel
    9 hours ago






  • 1





    I'm not sure why you don't like globalexpandafterletcsname barendcsnameBODY: it's entirely normal TeX.

    – Joseph Wright
    9 hours ago











  • I started writing a class file to prepare conference programmes using the same sort of "save text for later" features. github.com/scmbradley/schedule I have just realised that this was six years ago. My god. Six years!?

    – Seamus
    9 hours ago











  • You should definitely use let with BODY, def with anything whose definition doesn't change, and edef with things that do change (and you don't mind expanding them).

    – John Kormylo
    7 hours ago
















5















I'm trying to write myself a package, similar to the exercise package, so I can prepare problem sets for my students. I wish to delay the output of problems and/or solutions until a suitable place in the document has been reached. I have something that sort of works but it's kind of clunky. For example, this works pretty well:



usepackageenviron
NewEnvirontestbglobalexpandafterletcsname barendcsnameBODY


I use csname because the names are dynamically generated. This doesn't work so well if the command is anything other than BODY. For example, if I do letbarBODY, latex has an aneurysm (it binds bar to just {).



There is an alternate way of doing this, which is:



NewEnvirontestcglobalexpandafteredefcsname fooendcsnameBODY


This sort of works. The following thing works:
begintestchiendtestc, but this gives LaTeX an aneurysm: begintestcbf hiendtestc. (The error message is incomplete). I tried to debug it myself but I get hopelessly tangled up in packages that I don't understand. If you do it with tiny instead of bf you get the wildly different LaTeX error message: ! TeX capacity exceeded, sorry [input stack size=5000].



How can I squirrel away for later, not just BODY, but some sophisticated combination of BODY and other stuff, while having it robust to what's between the begin and end?



Edit: ok multiple solutions below










share|improve this question









New contributor




Sébastien Loisel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1





    Why not use def? let does what it should do: use the the next token.

    – TeXnician
    10 hours ago











  • If you mean this: NewEnvirontestcglobalexpandafterdefcsname fooendcsnameBODY, I get: ! Undefined control sequence. foo ->BODY

    – Sébastien Loisel
    9 hours ago






  • 1





    I'm not sure why you don't like globalexpandafterletcsname barendcsnameBODY: it's entirely normal TeX.

    – Joseph Wright
    9 hours ago











  • I started writing a class file to prepare conference programmes using the same sort of "save text for later" features. github.com/scmbradley/schedule I have just realised that this was six years ago. My god. Six years!?

    – Seamus
    9 hours ago











  • You should definitely use let with BODY, def with anything whose definition doesn't change, and edef with things that do change (and you don't mind expanding them).

    – John Kormylo
    7 hours ago














5












5








5








I'm trying to write myself a package, similar to the exercise package, so I can prepare problem sets for my students. I wish to delay the output of problems and/or solutions until a suitable place in the document has been reached. I have something that sort of works but it's kind of clunky. For example, this works pretty well:



usepackageenviron
NewEnvirontestbglobalexpandafterletcsname barendcsnameBODY


I use csname because the names are dynamically generated. This doesn't work so well if the command is anything other than BODY. For example, if I do letbarBODY, latex has an aneurysm (it binds bar to just {).



There is an alternate way of doing this, which is:



NewEnvirontestcglobalexpandafteredefcsname fooendcsnameBODY


This sort of works. The following thing works:
begintestchiendtestc, but this gives LaTeX an aneurysm: begintestcbf hiendtestc. (The error message is incomplete). I tried to debug it myself but I get hopelessly tangled up in packages that I don't understand. If you do it with tiny instead of bf you get the wildly different LaTeX error message: ! TeX capacity exceeded, sorry [input stack size=5000].



How can I squirrel away for later, not just BODY, but some sophisticated combination of BODY and other stuff, while having it robust to what's between the begin and end?



Edit: ok multiple solutions below










share|improve this question









New contributor




Sébastien Loisel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I'm trying to write myself a package, similar to the exercise package, so I can prepare problem sets for my students. I wish to delay the output of problems and/or solutions until a suitable place in the document has been reached. I have something that sort of works but it's kind of clunky. For example, this works pretty well:



usepackageenviron
NewEnvirontestbglobalexpandafterletcsname barendcsnameBODY


I use csname because the names are dynamically generated. This doesn't work so well if the command is anything other than BODY. For example, if I do letbarBODY, latex has an aneurysm (it binds bar to just {).



There is an alternate way of doing this, which is:



NewEnvirontestcglobalexpandafteredefcsname fooendcsnameBODY


This sort of works. The following thing works:
begintestchiendtestc, but this gives LaTeX an aneurysm: begintestcbf hiendtestc. (The error message is incomplete). I tried to debug it myself but I get hopelessly tangled up in packages that I don't understand. If you do it with tiny instead of bf you get the wildly different LaTeX error message: ! TeX capacity exceeded, sorry [input stack size=5000].



How can I squirrel away for later, not just BODY, but some sophisticated combination of BODY and other stuff, while having it robust to what's between the begin and end?



Edit: ok multiple solutions below







macros environments






share|improve this question









New contributor




Sébastien Loisel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Sébastien Loisel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 7 hours ago







Sébastien Loisel













New contributor




Sébastien Loisel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 10 hours ago









Sébastien LoiselSébastien Loisel

1284




1284




New contributor




Sébastien Loisel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Sébastien Loisel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Sébastien Loisel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1





    Why not use def? let does what it should do: use the the next token.

    – TeXnician
    10 hours ago











  • If you mean this: NewEnvirontestcglobalexpandafterdefcsname fooendcsnameBODY, I get: ! Undefined control sequence. foo ->BODY

    – Sébastien Loisel
    9 hours ago






  • 1





    I'm not sure why you don't like globalexpandafterletcsname barendcsnameBODY: it's entirely normal TeX.

    – Joseph Wright
    9 hours ago











  • I started writing a class file to prepare conference programmes using the same sort of "save text for later" features. github.com/scmbradley/schedule I have just realised that this was six years ago. My god. Six years!?

    – Seamus
    9 hours ago











  • You should definitely use let with BODY, def with anything whose definition doesn't change, and edef with things that do change (and you don't mind expanding them).

    – John Kormylo
    7 hours ago













  • 1





    Why not use def? let does what it should do: use the the next token.

    – TeXnician
    10 hours ago











  • If you mean this: NewEnvirontestcglobalexpandafterdefcsname fooendcsnameBODY, I get: ! Undefined control sequence. foo ->BODY

    – Sébastien Loisel
    9 hours ago






  • 1





    I'm not sure why you don't like globalexpandafterletcsname barendcsnameBODY: it's entirely normal TeX.

    – Joseph Wright
    9 hours ago











  • I started writing a class file to prepare conference programmes using the same sort of "save text for later" features. github.com/scmbradley/schedule I have just realised that this was six years ago. My god. Six years!?

    – Seamus
    9 hours ago











  • You should definitely use let with BODY, def with anything whose definition doesn't change, and edef with things that do change (and you don't mind expanding them).

    – John Kormylo
    7 hours ago








1




1





Why not use def? let does what it should do: use the the next token.

– TeXnician
10 hours ago





Why not use def? let does what it should do: use the the next token.

– TeXnician
10 hours ago













If you mean this: NewEnvirontestcglobalexpandafterdefcsname fooendcsnameBODY, I get: ! Undefined control sequence. foo ->BODY

– Sébastien Loisel
9 hours ago





If you mean this: NewEnvirontestcglobalexpandafterdefcsname fooendcsnameBODY, I get: ! Undefined control sequence. foo ->BODY

– Sébastien Loisel
9 hours ago




1




1





I'm not sure why you don't like globalexpandafterletcsname barendcsnameBODY: it's entirely normal TeX.

– Joseph Wright
9 hours ago





I'm not sure why you don't like globalexpandafterletcsname barendcsnameBODY: it's entirely normal TeX.

– Joseph Wright
9 hours ago













I started writing a class file to prepare conference programmes using the same sort of "save text for later" features. github.com/scmbradley/schedule I have just realised that this was six years ago. My god. Six years!?

– Seamus
9 hours ago





I started writing a class file to prepare conference programmes using the same sort of "save text for later" features. github.com/scmbradley/schedule I have just realised that this was six years ago. My god. Six years!?

– Seamus
9 hours ago













You should definitely use let with BODY, def with anything whose definition doesn't change, and edef with things that do change (and you don't mind expanding them).

– John Kormylo
7 hours ago






You should definitely use let with BODY, def with anything whose definition doesn't change, and edef with things that do change (and you don't mind expanding them).

– John Kormylo
7 hours ago











3 Answers
3






active

oldest

votes


















4














With unexpanded you can avoid worrying about protected@xdef.



documentclassarticle
usepackageenviron

NewEnvironexercise%
xdefsavedexercises%
unexpandedexpandaftersavedexercises%
noexpandbeginprintedexercise%
unexpandedexpandafterBODY%
noexpandendprintedexercise%
%

newcommandprintexercises%
savedexercises
gdefsavedexercises%

newcommandsavedexercises

newtheoremprintedexerciseExercise

begindocument

Here we talk about addition and show that $1+1=2$.

beginexercise
Compute $1+2$
endexercise

Here we talk about integrals.

beginexercise
Compute the following integrals:
beginitemize
item $displaystyleint_0^x e^-t^2,dt$

item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
enditemize
endexercise

Now we can print the exercises.

printexercises

enddocument


I used newtheorem just for the example.



enter image description here



With xparse released 2019-03-05 or later:



documentclassarticle
usepackagexparse

ExplSyntaxOn

NewDocumentEnvironmentexercise+b

tl_gput_right:Nn g_loisel_exercises_tl

beginprintedexercise
#1
endprintedexercise



NewDocumentCommandprintexercises

tl_use:N g_loisel_exercises_tl
tl_gclear:N g_loisel_exercises_tl


tl_new:N g_loisel_exercises_tl

ExplSyntaxOff

newtheoremprintedexerciseExercise

begindocument

Here we talk about addition and show that $1+1=2$.

beginexercise
Compute $1+2$
endexercise

Here we talk about integrals.

beginexercise
Compute the following integrals:
beginitemize
item $displaystyleint_0^x e^-t^2,dt$

item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
enditemize
endexercise

Now we can print the exercises.

printexercises

enddocument





share|improve this answer























  • Oh this is good

    – Sébastien Loisel
    7 hours ago


















6














If you want BODY to be saved then add stuff dynamically, you are likely best using two macros:



documentclassarticle
usepackageenviron
NewEnvirontestb%
globalexpandafterletcsname barendcsnameBODY
expandafterxdefcsname barplusendcsname%
expandafternoexpandcsname barendcsname
noexpandbf Hi
%

begindocument

begintestb
bfseries
Hi
endtestb
showbarplus
enddocument


If you want to avoid using BODY you could use xparse



documentclassarticle
usepackagexparse
NewDocumentEnvironmenttestb+bexpandaftergdefcsname barendcsname#1
begindocument

begintestb
bfseries
Hi
endtestb
showbar
enddocument





share|improve this answer

























  • This needs TL'19 or up-to-date MiKTeX: this is a new feature in xparse.

    – Joseph Wright
    9 hours ago











  • Ah, thanks for this. By the way, I just tried this: NewEnvirontestcexpandafterprotected@xdefcsname fooendcsnameBODY , and it seems to work. Is there any situation where this would break again?

    – Sébastien Loisel
    8 hours ago











  • @SébastienLoisel Well if you have anything that's not LaTeX2e robust, yes, but that is nowadays quite a low risk.

    – Joseph Wright
    8 hours ago











  • What's an example of something not LaTeX2e robust?

    – Sébastien Loisel
    8 hours ago











  • Nevermind, I tried with beginitemize...enditemize and it broke.

    – Sébastien Loisel
    7 hours ago


















0














A trick for triggering expansion is using romannumeral:



When due to romannumeral (La)TeX does gather together a sequence of digits trailed by a space as the number which it has to convert, expandable tokens get expanded.



When in the end a number is gathered together which is not positive, as the result of the conversion (La)TeX will not deliver any token at all.



Thus one can nicely (ab?)use romannumeral for triggering a lot of expansion-work and flipping-arguments-around-work as long as one ensures that in the end romannumeral will not find a positive number.



Here is a variant of egreg's answer which does with romannumeral and exchange instead of xdef and unexpanded.



documentclassarticle
usepackageenviron

newcommandexchange[2]#2#1

NewEnvironexercise%
expandaftergdefexpandaftersavedexercisesexpandafter%
romannumeral0expandafterexchangeexpandafterBODY%
exchange expandaftersavedexercises
beginprintedexercise%
%
endprintedexercise%
%

newcommandprintexercises%
savedexercises
gdefsavedexercises%

newcommandsavedexercises

newtheoremprintedexerciseExercise

begindocument

Here we talk about addition and show that $1+1=2$.

beginexercise
Compute $1+2$
endexercise

Here we talk about integrals.

beginexercise
Compute the following integrals:
beginitemize
item $displaystyleint_0^x e^-t^2,dt$

item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
enditemize
endexercise

Now we can print the exercises.

printexercises

enddocument


enter image description here



If you wish to wrap the name of the macro that is to be defined into csname..endcsname, i.e., if you wish to use csname savedexercisesendcsname rather than savedexercises, you can take advantage of the fact that (La)TeX does expand expandable tokens while due to csname gathering the name of a control sequence token and hereby searching for the matching endcsname:



documentclassarticle
usepackageenviron

newcommandexchange[2]#2#1

NewEnvironexercise%
expandaftergdefcsname savedexercisesexpandafterendcsnameexpandafter%
romannumeral0expandafterexchangeexpandafterBODY%
exchange expandafterexpandafterexpandaftercsname savedexercisesendcsname
beginprintedexercise%
%
endprintedexercise%
%

newcommandprintexercises%
csname savedexercisesendcsname
expandaftergdefcsname savedexercisesendcsname%

expandafternewcommandexpandaftercsname savedexercisesendcsname

newtheoremprintedexerciseExercise

begindocument

Here we talk about addition and show that $1+1=2$.

beginexercise
Compute $1+2$
endexercise

Here we talk about integrals.

beginexercise
Compute the following integrals:
beginitemize
item $displaystyleint_0^x e^-t^2,dt$

item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
enditemize
endexercise

Now we can print the exercises.

printexercises

enddocument


enter image description here



Be aware that with the approaches presented by now you cannot use printexercises for having exercises occur in arbitrary places. You can have exercises occur only in places of the document which in the source correspond to places behind the exercise-environments.



Perhaps an environment which does read its content under verbatim-catcode-régime for unexpanded-writing it to .aux-file in a way where from the .aux-file it gets read back under verbatim-catcode-régime also for defining a macro where scantokens will be applied to, and thus some sort of re-implementation of the label-ref-mechanism or of the tableofcontents-mechanism for verbatimized stuff might make it possible to make exercises printable throughout the entire document.



Implementing such a mechanism might be a nice challenge. But before taking that into consideration at all, exact information is needed on the intended usage and the desired "user-interface", i.e., what additional things you wish to be able to specify in which ways, etc, ...






share|improve this answer






















    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
    );



    );






    Sébastien Loisel is a new contributor. Be nice, and check out our Code of Conduct.









    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f480267%2fhow-to-robustly-store-bits-of-text-for-later-use%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    With unexpanded you can avoid worrying about protected@xdef.



    documentclassarticle
    usepackageenviron

    NewEnvironexercise%
    xdefsavedexercises%
    unexpandedexpandaftersavedexercises%
    noexpandbeginprintedexercise%
    unexpandedexpandafterBODY%
    noexpandendprintedexercise%
    %

    newcommandprintexercises%
    savedexercises
    gdefsavedexercises%

    newcommandsavedexercises

    newtheoremprintedexerciseExercise

    begindocument

    Here we talk about addition and show that $1+1=2$.

    beginexercise
    Compute $1+2$
    endexercise

    Here we talk about integrals.

    beginexercise
    Compute the following integrals:
    beginitemize
    item $displaystyleint_0^x e^-t^2,dt$

    item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
    enditemize
    endexercise

    Now we can print the exercises.

    printexercises

    enddocument


    I used newtheorem just for the example.



    enter image description here



    With xparse released 2019-03-05 or later:



    documentclassarticle
    usepackagexparse

    ExplSyntaxOn

    NewDocumentEnvironmentexercise+b

    tl_gput_right:Nn g_loisel_exercises_tl

    beginprintedexercise
    #1
    endprintedexercise



    NewDocumentCommandprintexercises

    tl_use:N g_loisel_exercises_tl
    tl_gclear:N g_loisel_exercises_tl


    tl_new:N g_loisel_exercises_tl

    ExplSyntaxOff

    newtheoremprintedexerciseExercise

    begindocument

    Here we talk about addition and show that $1+1=2$.

    beginexercise
    Compute $1+2$
    endexercise

    Here we talk about integrals.

    beginexercise
    Compute the following integrals:
    beginitemize
    item $displaystyleint_0^x e^-t^2,dt$

    item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
    enditemize
    endexercise

    Now we can print the exercises.

    printexercises

    enddocument





    share|improve this answer























    • Oh this is good

      – Sébastien Loisel
      7 hours ago















    4














    With unexpanded you can avoid worrying about protected@xdef.



    documentclassarticle
    usepackageenviron

    NewEnvironexercise%
    xdefsavedexercises%
    unexpandedexpandaftersavedexercises%
    noexpandbeginprintedexercise%
    unexpandedexpandafterBODY%
    noexpandendprintedexercise%
    %

    newcommandprintexercises%
    savedexercises
    gdefsavedexercises%

    newcommandsavedexercises

    newtheoremprintedexerciseExercise

    begindocument

    Here we talk about addition and show that $1+1=2$.

    beginexercise
    Compute $1+2$
    endexercise

    Here we talk about integrals.

    beginexercise
    Compute the following integrals:
    beginitemize
    item $displaystyleint_0^x e^-t^2,dt$

    item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
    enditemize
    endexercise

    Now we can print the exercises.

    printexercises

    enddocument


    I used newtheorem just for the example.



    enter image description here



    With xparse released 2019-03-05 or later:



    documentclassarticle
    usepackagexparse

    ExplSyntaxOn

    NewDocumentEnvironmentexercise+b

    tl_gput_right:Nn g_loisel_exercises_tl

    beginprintedexercise
    #1
    endprintedexercise



    NewDocumentCommandprintexercises

    tl_use:N g_loisel_exercises_tl
    tl_gclear:N g_loisel_exercises_tl


    tl_new:N g_loisel_exercises_tl

    ExplSyntaxOff

    newtheoremprintedexerciseExercise

    begindocument

    Here we talk about addition and show that $1+1=2$.

    beginexercise
    Compute $1+2$
    endexercise

    Here we talk about integrals.

    beginexercise
    Compute the following integrals:
    beginitemize
    item $displaystyleint_0^x e^-t^2,dt$

    item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
    enditemize
    endexercise

    Now we can print the exercises.

    printexercises

    enddocument





    share|improve this answer























    • Oh this is good

      – Sébastien Loisel
      7 hours ago













    4












    4








    4







    With unexpanded you can avoid worrying about protected@xdef.



    documentclassarticle
    usepackageenviron

    NewEnvironexercise%
    xdefsavedexercises%
    unexpandedexpandaftersavedexercises%
    noexpandbeginprintedexercise%
    unexpandedexpandafterBODY%
    noexpandendprintedexercise%
    %

    newcommandprintexercises%
    savedexercises
    gdefsavedexercises%

    newcommandsavedexercises

    newtheoremprintedexerciseExercise

    begindocument

    Here we talk about addition and show that $1+1=2$.

    beginexercise
    Compute $1+2$
    endexercise

    Here we talk about integrals.

    beginexercise
    Compute the following integrals:
    beginitemize
    item $displaystyleint_0^x e^-t^2,dt$

    item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
    enditemize
    endexercise

    Now we can print the exercises.

    printexercises

    enddocument


    I used newtheorem just for the example.



    enter image description here



    With xparse released 2019-03-05 or later:



    documentclassarticle
    usepackagexparse

    ExplSyntaxOn

    NewDocumentEnvironmentexercise+b

    tl_gput_right:Nn g_loisel_exercises_tl

    beginprintedexercise
    #1
    endprintedexercise



    NewDocumentCommandprintexercises

    tl_use:N g_loisel_exercises_tl
    tl_gclear:N g_loisel_exercises_tl


    tl_new:N g_loisel_exercises_tl

    ExplSyntaxOff

    newtheoremprintedexerciseExercise

    begindocument

    Here we talk about addition and show that $1+1=2$.

    beginexercise
    Compute $1+2$
    endexercise

    Here we talk about integrals.

    beginexercise
    Compute the following integrals:
    beginitemize
    item $displaystyleint_0^x e^-t^2,dt$

    item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
    enditemize
    endexercise

    Now we can print the exercises.

    printexercises

    enddocument





    share|improve this answer













    With unexpanded you can avoid worrying about protected@xdef.



    documentclassarticle
    usepackageenviron

    NewEnvironexercise%
    xdefsavedexercises%
    unexpandedexpandaftersavedexercises%
    noexpandbeginprintedexercise%
    unexpandedexpandafterBODY%
    noexpandendprintedexercise%
    %

    newcommandprintexercises%
    savedexercises
    gdefsavedexercises%

    newcommandsavedexercises

    newtheoremprintedexerciseExercise

    begindocument

    Here we talk about addition and show that $1+1=2$.

    beginexercise
    Compute $1+2$
    endexercise

    Here we talk about integrals.

    beginexercise
    Compute the following integrals:
    beginitemize
    item $displaystyleint_0^x e^-t^2,dt$

    item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
    enditemize
    endexercise

    Now we can print the exercises.

    printexercises

    enddocument


    I used newtheorem just for the example.



    enter image description here



    With xparse released 2019-03-05 or later:



    documentclassarticle
    usepackagexparse

    ExplSyntaxOn

    NewDocumentEnvironmentexercise+b

    tl_gput_right:Nn g_loisel_exercises_tl

    beginprintedexercise
    #1
    endprintedexercise



    NewDocumentCommandprintexercises

    tl_use:N g_loisel_exercises_tl
    tl_gclear:N g_loisel_exercises_tl


    tl_new:N g_loisel_exercises_tl

    ExplSyntaxOff

    newtheoremprintedexerciseExercise

    begindocument

    Here we talk about addition and show that $1+1=2$.

    beginexercise
    Compute $1+2$
    endexercise

    Here we talk about integrals.

    beginexercise
    Compute the following integrals:
    beginitemize
    item $displaystyleint_0^x e^-t^2,dt$

    item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
    enditemize
    endexercise

    Now we can print the exercises.

    printexercises

    enddocument






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 7 hours ago









    egregegreg

    727k8819233233




    727k8819233233












    • Oh this is good

      – Sébastien Loisel
      7 hours ago

















    • Oh this is good

      – Sébastien Loisel
      7 hours ago
















    Oh this is good

    – Sébastien Loisel
    7 hours ago





    Oh this is good

    – Sébastien Loisel
    7 hours ago











    6














    If you want BODY to be saved then add stuff dynamically, you are likely best using two macros:



    documentclassarticle
    usepackageenviron
    NewEnvirontestb%
    globalexpandafterletcsname barendcsnameBODY
    expandafterxdefcsname barplusendcsname%
    expandafternoexpandcsname barendcsname
    noexpandbf Hi
    %

    begindocument

    begintestb
    bfseries
    Hi
    endtestb
    showbarplus
    enddocument


    If you want to avoid using BODY you could use xparse



    documentclassarticle
    usepackagexparse
    NewDocumentEnvironmenttestb+bexpandaftergdefcsname barendcsname#1
    begindocument

    begintestb
    bfseries
    Hi
    endtestb
    showbar
    enddocument





    share|improve this answer

























    • This needs TL'19 or up-to-date MiKTeX: this is a new feature in xparse.

      – Joseph Wright
      9 hours ago











    • Ah, thanks for this. By the way, I just tried this: NewEnvirontestcexpandafterprotected@xdefcsname fooendcsnameBODY , and it seems to work. Is there any situation where this would break again?

      – Sébastien Loisel
      8 hours ago











    • @SébastienLoisel Well if you have anything that's not LaTeX2e robust, yes, but that is nowadays quite a low risk.

      – Joseph Wright
      8 hours ago











    • What's an example of something not LaTeX2e robust?

      – Sébastien Loisel
      8 hours ago











    • Nevermind, I tried with beginitemize...enditemize and it broke.

      – Sébastien Loisel
      7 hours ago















    6














    If you want BODY to be saved then add stuff dynamically, you are likely best using two macros:



    documentclassarticle
    usepackageenviron
    NewEnvirontestb%
    globalexpandafterletcsname barendcsnameBODY
    expandafterxdefcsname barplusendcsname%
    expandafternoexpandcsname barendcsname
    noexpandbf Hi
    %

    begindocument

    begintestb
    bfseries
    Hi
    endtestb
    showbarplus
    enddocument


    If you want to avoid using BODY you could use xparse



    documentclassarticle
    usepackagexparse
    NewDocumentEnvironmenttestb+bexpandaftergdefcsname barendcsname#1
    begindocument

    begintestb
    bfseries
    Hi
    endtestb
    showbar
    enddocument





    share|improve this answer

























    • This needs TL'19 or up-to-date MiKTeX: this is a new feature in xparse.

      – Joseph Wright
      9 hours ago











    • Ah, thanks for this. By the way, I just tried this: NewEnvirontestcexpandafterprotected@xdefcsname fooendcsnameBODY , and it seems to work. Is there any situation where this would break again?

      – Sébastien Loisel
      8 hours ago











    • @SébastienLoisel Well if you have anything that's not LaTeX2e robust, yes, but that is nowadays quite a low risk.

      – Joseph Wright
      8 hours ago











    • What's an example of something not LaTeX2e robust?

      – Sébastien Loisel
      8 hours ago











    • Nevermind, I tried with beginitemize...enditemize and it broke.

      – Sébastien Loisel
      7 hours ago













    6












    6








    6







    If you want BODY to be saved then add stuff dynamically, you are likely best using two macros:



    documentclassarticle
    usepackageenviron
    NewEnvirontestb%
    globalexpandafterletcsname barendcsnameBODY
    expandafterxdefcsname barplusendcsname%
    expandafternoexpandcsname barendcsname
    noexpandbf Hi
    %

    begindocument

    begintestb
    bfseries
    Hi
    endtestb
    showbarplus
    enddocument


    If you want to avoid using BODY you could use xparse



    documentclassarticle
    usepackagexparse
    NewDocumentEnvironmenttestb+bexpandaftergdefcsname barendcsname#1
    begindocument

    begintestb
    bfseries
    Hi
    endtestb
    showbar
    enddocument





    share|improve this answer















    If you want BODY to be saved then add stuff dynamically, you are likely best using two macros:



    documentclassarticle
    usepackageenviron
    NewEnvirontestb%
    globalexpandafterletcsname barendcsnameBODY
    expandafterxdefcsname barplusendcsname%
    expandafternoexpandcsname barendcsname
    noexpandbf Hi
    %

    begindocument

    begintestb
    bfseries
    Hi
    endtestb
    showbarplus
    enddocument


    If you want to avoid using BODY you could use xparse



    documentclassarticle
    usepackagexparse
    NewDocumentEnvironmenttestb+bexpandaftergdefcsname barendcsname#1
    begindocument

    begintestb
    bfseries
    Hi
    endtestb
    showbar
    enddocument






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 9 hours ago

























    answered 9 hours ago









    Joseph WrightJoseph Wright

    205k23561890




    205k23561890












    • This needs TL'19 or up-to-date MiKTeX: this is a new feature in xparse.

      – Joseph Wright
      9 hours ago











    • Ah, thanks for this. By the way, I just tried this: NewEnvirontestcexpandafterprotected@xdefcsname fooendcsnameBODY , and it seems to work. Is there any situation where this would break again?

      – Sébastien Loisel
      8 hours ago











    • @SébastienLoisel Well if you have anything that's not LaTeX2e robust, yes, but that is nowadays quite a low risk.

      – Joseph Wright
      8 hours ago











    • What's an example of something not LaTeX2e robust?

      – Sébastien Loisel
      8 hours ago











    • Nevermind, I tried with beginitemize...enditemize and it broke.

      – Sébastien Loisel
      7 hours ago

















    • This needs TL'19 or up-to-date MiKTeX: this is a new feature in xparse.

      – Joseph Wright
      9 hours ago











    • Ah, thanks for this. By the way, I just tried this: NewEnvirontestcexpandafterprotected@xdefcsname fooendcsnameBODY , and it seems to work. Is there any situation where this would break again?

      – Sébastien Loisel
      8 hours ago











    • @SébastienLoisel Well if you have anything that's not LaTeX2e robust, yes, but that is nowadays quite a low risk.

      – Joseph Wright
      8 hours ago











    • What's an example of something not LaTeX2e robust?

      – Sébastien Loisel
      8 hours ago











    • Nevermind, I tried with beginitemize...enditemize and it broke.

      – Sébastien Loisel
      7 hours ago
















    This needs TL'19 or up-to-date MiKTeX: this is a new feature in xparse.

    – Joseph Wright
    9 hours ago





    This needs TL'19 or up-to-date MiKTeX: this is a new feature in xparse.

    – Joseph Wright
    9 hours ago













    Ah, thanks for this. By the way, I just tried this: NewEnvirontestcexpandafterprotected@xdefcsname fooendcsnameBODY , and it seems to work. Is there any situation where this would break again?

    – Sébastien Loisel
    8 hours ago





    Ah, thanks for this. By the way, I just tried this: NewEnvirontestcexpandafterprotected@xdefcsname fooendcsnameBODY , and it seems to work. Is there any situation where this would break again?

    – Sébastien Loisel
    8 hours ago













    @SébastienLoisel Well if you have anything that's not LaTeX2e robust, yes, but that is nowadays quite a low risk.

    – Joseph Wright
    8 hours ago





    @SébastienLoisel Well if you have anything that's not LaTeX2e robust, yes, but that is nowadays quite a low risk.

    – Joseph Wright
    8 hours ago













    What's an example of something not LaTeX2e robust?

    – Sébastien Loisel
    8 hours ago





    What's an example of something not LaTeX2e robust?

    – Sébastien Loisel
    8 hours ago













    Nevermind, I tried with beginitemize...enditemize and it broke.

    – Sébastien Loisel
    7 hours ago





    Nevermind, I tried with beginitemize...enditemize and it broke.

    – Sébastien Loisel
    7 hours ago











    0














    A trick for triggering expansion is using romannumeral:



    When due to romannumeral (La)TeX does gather together a sequence of digits trailed by a space as the number which it has to convert, expandable tokens get expanded.



    When in the end a number is gathered together which is not positive, as the result of the conversion (La)TeX will not deliver any token at all.



    Thus one can nicely (ab?)use romannumeral for triggering a lot of expansion-work and flipping-arguments-around-work as long as one ensures that in the end romannumeral will not find a positive number.



    Here is a variant of egreg's answer which does with romannumeral and exchange instead of xdef and unexpanded.



    documentclassarticle
    usepackageenviron

    newcommandexchange[2]#2#1

    NewEnvironexercise%
    expandaftergdefexpandaftersavedexercisesexpandafter%
    romannumeral0expandafterexchangeexpandafterBODY%
    exchange expandaftersavedexercises
    beginprintedexercise%
    %
    endprintedexercise%
    %

    newcommandprintexercises%
    savedexercises
    gdefsavedexercises%

    newcommandsavedexercises

    newtheoremprintedexerciseExercise

    begindocument

    Here we talk about addition and show that $1+1=2$.

    beginexercise
    Compute $1+2$
    endexercise

    Here we talk about integrals.

    beginexercise
    Compute the following integrals:
    beginitemize
    item $displaystyleint_0^x e^-t^2,dt$

    item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
    enditemize
    endexercise

    Now we can print the exercises.

    printexercises

    enddocument


    enter image description here



    If you wish to wrap the name of the macro that is to be defined into csname..endcsname, i.e., if you wish to use csname savedexercisesendcsname rather than savedexercises, you can take advantage of the fact that (La)TeX does expand expandable tokens while due to csname gathering the name of a control sequence token and hereby searching for the matching endcsname:



    documentclassarticle
    usepackageenviron

    newcommandexchange[2]#2#1

    NewEnvironexercise%
    expandaftergdefcsname savedexercisesexpandafterendcsnameexpandafter%
    romannumeral0expandafterexchangeexpandafterBODY%
    exchange expandafterexpandafterexpandaftercsname savedexercisesendcsname
    beginprintedexercise%
    %
    endprintedexercise%
    %

    newcommandprintexercises%
    csname savedexercisesendcsname
    expandaftergdefcsname savedexercisesendcsname%

    expandafternewcommandexpandaftercsname savedexercisesendcsname

    newtheoremprintedexerciseExercise

    begindocument

    Here we talk about addition and show that $1+1=2$.

    beginexercise
    Compute $1+2$
    endexercise

    Here we talk about integrals.

    beginexercise
    Compute the following integrals:
    beginitemize
    item $displaystyleint_0^x e^-t^2,dt$

    item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
    enditemize
    endexercise

    Now we can print the exercises.

    printexercises

    enddocument


    enter image description here



    Be aware that with the approaches presented by now you cannot use printexercises for having exercises occur in arbitrary places. You can have exercises occur only in places of the document which in the source correspond to places behind the exercise-environments.



    Perhaps an environment which does read its content under verbatim-catcode-régime for unexpanded-writing it to .aux-file in a way where from the .aux-file it gets read back under verbatim-catcode-régime also for defining a macro where scantokens will be applied to, and thus some sort of re-implementation of the label-ref-mechanism or of the tableofcontents-mechanism for verbatimized stuff might make it possible to make exercises printable throughout the entire document.



    Implementing such a mechanism might be a nice challenge. But before taking that into consideration at all, exact information is needed on the intended usage and the desired "user-interface", i.e., what additional things you wish to be able to specify in which ways, etc, ...






    share|improve this answer



























      0














      A trick for triggering expansion is using romannumeral:



      When due to romannumeral (La)TeX does gather together a sequence of digits trailed by a space as the number which it has to convert, expandable tokens get expanded.



      When in the end a number is gathered together which is not positive, as the result of the conversion (La)TeX will not deliver any token at all.



      Thus one can nicely (ab?)use romannumeral for triggering a lot of expansion-work and flipping-arguments-around-work as long as one ensures that in the end romannumeral will not find a positive number.



      Here is a variant of egreg's answer which does with romannumeral and exchange instead of xdef and unexpanded.



      documentclassarticle
      usepackageenviron

      newcommandexchange[2]#2#1

      NewEnvironexercise%
      expandaftergdefexpandaftersavedexercisesexpandafter%
      romannumeral0expandafterexchangeexpandafterBODY%
      exchange expandaftersavedexercises
      beginprintedexercise%
      %
      endprintedexercise%
      %

      newcommandprintexercises%
      savedexercises
      gdefsavedexercises%

      newcommandsavedexercises

      newtheoremprintedexerciseExercise

      begindocument

      Here we talk about addition and show that $1+1=2$.

      beginexercise
      Compute $1+2$
      endexercise

      Here we talk about integrals.

      beginexercise
      Compute the following integrals:
      beginitemize
      item $displaystyleint_0^x e^-t^2,dt$

      item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
      enditemize
      endexercise

      Now we can print the exercises.

      printexercises

      enddocument


      enter image description here



      If you wish to wrap the name of the macro that is to be defined into csname..endcsname, i.e., if you wish to use csname savedexercisesendcsname rather than savedexercises, you can take advantage of the fact that (La)TeX does expand expandable tokens while due to csname gathering the name of a control sequence token and hereby searching for the matching endcsname:



      documentclassarticle
      usepackageenviron

      newcommandexchange[2]#2#1

      NewEnvironexercise%
      expandaftergdefcsname savedexercisesexpandafterendcsnameexpandafter%
      romannumeral0expandafterexchangeexpandafterBODY%
      exchange expandafterexpandafterexpandaftercsname savedexercisesendcsname
      beginprintedexercise%
      %
      endprintedexercise%
      %

      newcommandprintexercises%
      csname savedexercisesendcsname
      expandaftergdefcsname savedexercisesendcsname%

      expandafternewcommandexpandaftercsname savedexercisesendcsname

      newtheoremprintedexerciseExercise

      begindocument

      Here we talk about addition and show that $1+1=2$.

      beginexercise
      Compute $1+2$
      endexercise

      Here we talk about integrals.

      beginexercise
      Compute the following integrals:
      beginitemize
      item $displaystyleint_0^x e^-t^2,dt$

      item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
      enditemize
      endexercise

      Now we can print the exercises.

      printexercises

      enddocument


      enter image description here



      Be aware that with the approaches presented by now you cannot use printexercises for having exercises occur in arbitrary places. You can have exercises occur only in places of the document which in the source correspond to places behind the exercise-environments.



      Perhaps an environment which does read its content under verbatim-catcode-régime for unexpanded-writing it to .aux-file in a way where from the .aux-file it gets read back under verbatim-catcode-régime also for defining a macro where scantokens will be applied to, and thus some sort of re-implementation of the label-ref-mechanism or of the tableofcontents-mechanism for verbatimized stuff might make it possible to make exercises printable throughout the entire document.



      Implementing such a mechanism might be a nice challenge. But before taking that into consideration at all, exact information is needed on the intended usage and the desired "user-interface", i.e., what additional things you wish to be able to specify in which ways, etc, ...






      share|improve this answer

























        0












        0








        0







        A trick for triggering expansion is using romannumeral:



        When due to romannumeral (La)TeX does gather together a sequence of digits trailed by a space as the number which it has to convert, expandable tokens get expanded.



        When in the end a number is gathered together which is not positive, as the result of the conversion (La)TeX will not deliver any token at all.



        Thus one can nicely (ab?)use romannumeral for triggering a lot of expansion-work and flipping-arguments-around-work as long as one ensures that in the end romannumeral will not find a positive number.



        Here is a variant of egreg's answer which does with romannumeral and exchange instead of xdef and unexpanded.



        documentclassarticle
        usepackageenviron

        newcommandexchange[2]#2#1

        NewEnvironexercise%
        expandaftergdefexpandaftersavedexercisesexpandafter%
        romannumeral0expandafterexchangeexpandafterBODY%
        exchange expandaftersavedexercises
        beginprintedexercise%
        %
        endprintedexercise%
        %

        newcommandprintexercises%
        savedexercises
        gdefsavedexercises%

        newcommandsavedexercises

        newtheoremprintedexerciseExercise

        begindocument

        Here we talk about addition and show that $1+1=2$.

        beginexercise
        Compute $1+2$
        endexercise

        Here we talk about integrals.

        beginexercise
        Compute the following integrals:
        beginitemize
        item $displaystyleint_0^x e^-t^2,dt$

        item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
        enditemize
        endexercise

        Now we can print the exercises.

        printexercises

        enddocument


        enter image description here



        If you wish to wrap the name of the macro that is to be defined into csname..endcsname, i.e., if you wish to use csname savedexercisesendcsname rather than savedexercises, you can take advantage of the fact that (La)TeX does expand expandable tokens while due to csname gathering the name of a control sequence token and hereby searching for the matching endcsname:



        documentclassarticle
        usepackageenviron

        newcommandexchange[2]#2#1

        NewEnvironexercise%
        expandaftergdefcsname savedexercisesexpandafterendcsnameexpandafter%
        romannumeral0expandafterexchangeexpandafterBODY%
        exchange expandafterexpandafterexpandaftercsname savedexercisesendcsname
        beginprintedexercise%
        %
        endprintedexercise%
        %

        newcommandprintexercises%
        csname savedexercisesendcsname
        expandaftergdefcsname savedexercisesendcsname%

        expandafternewcommandexpandaftercsname savedexercisesendcsname

        newtheoremprintedexerciseExercise

        begindocument

        Here we talk about addition and show that $1+1=2$.

        beginexercise
        Compute $1+2$
        endexercise

        Here we talk about integrals.

        beginexercise
        Compute the following integrals:
        beginitemize
        item $displaystyleint_0^x e^-t^2,dt$

        item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
        enditemize
        endexercise

        Now we can print the exercises.

        printexercises

        enddocument


        enter image description here



        Be aware that with the approaches presented by now you cannot use printexercises for having exercises occur in arbitrary places. You can have exercises occur only in places of the document which in the source correspond to places behind the exercise-environments.



        Perhaps an environment which does read its content under verbatim-catcode-régime for unexpanded-writing it to .aux-file in a way where from the .aux-file it gets read back under verbatim-catcode-régime also for defining a macro where scantokens will be applied to, and thus some sort of re-implementation of the label-ref-mechanism or of the tableofcontents-mechanism for verbatimized stuff might make it possible to make exercises printable throughout the entire document.



        Implementing such a mechanism might be a nice challenge. But before taking that into consideration at all, exact information is needed on the intended usage and the desired "user-interface", i.e., what additional things you wish to be able to specify in which ways, etc, ...






        share|improve this answer













        A trick for triggering expansion is using romannumeral:



        When due to romannumeral (La)TeX does gather together a sequence of digits trailed by a space as the number which it has to convert, expandable tokens get expanded.



        When in the end a number is gathered together which is not positive, as the result of the conversion (La)TeX will not deliver any token at all.



        Thus one can nicely (ab?)use romannumeral for triggering a lot of expansion-work and flipping-arguments-around-work as long as one ensures that in the end romannumeral will not find a positive number.



        Here is a variant of egreg's answer which does with romannumeral and exchange instead of xdef and unexpanded.



        documentclassarticle
        usepackageenviron

        newcommandexchange[2]#2#1

        NewEnvironexercise%
        expandaftergdefexpandaftersavedexercisesexpandafter%
        romannumeral0expandafterexchangeexpandafterBODY%
        exchange expandaftersavedexercises
        beginprintedexercise%
        %
        endprintedexercise%
        %

        newcommandprintexercises%
        savedexercises
        gdefsavedexercises%

        newcommandsavedexercises

        newtheoremprintedexerciseExercise

        begindocument

        Here we talk about addition and show that $1+1=2$.

        beginexercise
        Compute $1+2$
        endexercise

        Here we talk about integrals.

        beginexercise
        Compute the following integrals:
        beginitemize
        item $displaystyleint_0^x e^-t^2,dt$

        item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
        enditemize
        endexercise

        Now we can print the exercises.

        printexercises

        enddocument


        enter image description here



        If you wish to wrap the name of the macro that is to be defined into csname..endcsname, i.e., if you wish to use csname savedexercisesendcsname rather than savedexercises, you can take advantage of the fact that (La)TeX does expand expandable tokens while due to csname gathering the name of a control sequence token and hereby searching for the matching endcsname:



        documentclassarticle
        usepackageenviron

        newcommandexchange[2]#2#1

        NewEnvironexercise%
        expandaftergdefcsname savedexercisesexpandafterendcsnameexpandafter%
        romannumeral0expandafterexchangeexpandafterBODY%
        exchange expandafterexpandafterexpandaftercsname savedexercisesendcsname
        beginprintedexercise%
        %
        endprintedexercise%
        %

        newcommandprintexercises%
        csname savedexercisesendcsname
        expandaftergdefcsname savedexercisesendcsname%

        expandafternewcommandexpandaftercsname savedexercisesendcsname

        newtheoremprintedexerciseExercise

        begindocument

        Here we talk about addition and show that $1+1=2$.

        beginexercise
        Compute $1+2$
        endexercise

        Here we talk about integrals.

        beginexercise
        Compute the following integrals:
        beginitemize
        item $displaystyleint_0^x e^-t^2,dt$

        item $displaystyleint_1^x frace^tt,dt$, for $t>0$.
        enditemize
        endexercise

        Now we can print the exercises.

        printexercises

        enddocument


        enter image description here



        Be aware that with the approaches presented by now you cannot use printexercises for having exercises occur in arbitrary places. You can have exercises occur only in places of the document which in the source correspond to places behind the exercise-environments.



        Perhaps an environment which does read its content under verbatim-catcode-régime for unexpanded-writing it to .aux-file in a way where from the .aux-file it gets read back under verbatim-catcode-régime also for defining a macro where scantokens will be applied to, and thus some sort of re-implementation of the label-ref-mechanism or of the tableofcontents-mechanism for verbatimized stuff might make it possible to make exercises printable throughout the entire document.



        Implementing such a mechanism might be a nice challenge. But before taking that into consideration at all, exact information is needed on the intended usage and the desired "user-interface", i.e., what additional things you wish to be able to specify in which ways, etc, ...







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 6 hours ago









        Ulrich DiezUlrich Diez

        5,460619




        5,460619




















            Sébastien Loisel is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            Sébastien Loisel is a new contributor. Be nice, and check out our Code of Conduct.












            Sébastien Loisel is a new contributor. Be nice, and check out our Code of Conduct.











            Sébastien Loisel 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.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f480267%2fhow-to-robustly-store-bits-of-text-for-later-use%23new-answer', 'question_page');

            );

            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







            Popular posts from this blog

            How to create a command for the “strange m” symbol in latex? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)How do you make your own symbol when Detexify fails?Writing bold small caps with mathpazo packageplus-minus symbol with parenthesis around the minus signGreek character in Beamer document titleHow to create dashed right arrow over symbol?Currency symbol: Turkish LiraDouble prec as a single symbol?Plus Sign Too Big; How to Call adfbullet?Is there a TeX macro for three-legged pi?How do I get my integral-like symbol to align like the integral?How to selectively substitute a letter with another symbol representing the same letterHow do I generate a less than symbol and vertical bar that are the same height?

            Category:Tremithousa Media in category "Tremithousa"Navigation menuUpload media34° 49′ 02.7″ N, 32° 26′ 37.32″ EOpenStreetMapGoogle EarthProximityramaReasonatorScholiaStatisticsWikiShootMe

            Dokschytsy (Steed) Kwelen | NawigatsjuunBelarus: Vitebsk Region, citypopulation.de