How to robustly store bits of text for later use Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)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

What does it mean that physics no longer uses mechanical models to describe phenomena?

What does 丫 mean? 丫是什么意思?

macOS: Name for app shortcut screen found by pinching with thumb and three fingers

Girl Hackers - Logic Puzzle

What order were files/directories output in dir?

How much damage would a cupful of neutron star matter do to the Earth?

Does the Mueller report show a conspiracy between Russia and the Trump Campaign?

Most bit efficient text communication method?

Do I really need to have a message in a novel to appeal to readers?

Semigroups with no morphisms between them

How does the math work when buying airline miles?

How do living politicians protect their readily obtainable signatures from misuse?

Is there public access to the Meteor Crater in Arizona?

What does Turing mean by this statement?

AppleTVs create a chatty alternate WiFi network

What do you call the main part of a joke?

What is the difference between a "ranged attack" and a "ranged weapon attack"?

How many time has Arya actually used Needle?

Do wooden building fires get hotter than 600°C?

Why can't I install Tomboy in Ubuntu Mate 19.04?

Misunderstanding of Sylow theory

Time evolution of a Gaussian wave packet, why convert to k-space?

Why weren't discrete x86 CPUs ever used in game hardware?

The test team as an enemy of development? And how can this be avoided?



How to robustly store bits of text for later use



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)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



















  • 1





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

    – TeXnician
    Mar 19 at 13:02











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

    – Sébastien Loisel
    Mar 19 at 13:09






  • 1





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

    – Joseph Wright
    Mar 19 at 13:31











  • 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
    Mar 19 at 13:47











  • 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
    Mar 19 at 16:05
















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



















  • 1





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

    – TeXnician
    Mar 19 at 13:02











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

    – Sébastien Loisel
    Mar 19 at 13:09






  • 1





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

    – Joseph Wright
    Mar 19 at 13:31











  • 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
    Mar 19 at 13:47











  • 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
    Mar 19 at 16:05














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
















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















share|improve this question













share|improve this question




share|improve this question








edited Mar 19 at 15:40







Sébastien Loisel

















asked Mar 19 at 12:59









Sébastien LoiselSébastien Loisel

1284




1284







  • 1





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

    – TeXnician
    Mar 19 at 13:02











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

    – Sébastien Loisel
    Mar 19 at 13:09






  • 1





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

    – Joseph Wright
    Mar 19 at 13:31











  • 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
    Mar 19 at 13:47











  • 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
    Mar 19 at 16:05













  • 1





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

    – TeXnician
    Mar 19 at 13:02











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

    – Sébastien Loisel
    Mar 19 at 13:09






  • 1





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

    – Joseph Wright
    Mar 19 at 13:31











  • 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
    Mar 19 at 13:47











  • 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
    Mar 19 at 16:05








1




1





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

– TeXnician
Mar 19 at 13:02





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

– TeXnician
Mar 19 at 13:02













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

– Sébastien Loisel
Mar 19 at 13:09





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

– Sébastien Loisel
Mar 19 at 13:09




1




1





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

– Joseph Wright
Mar 19 at 13:31





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

– Joseph Wright
Mar 19 at 13:31













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
Mar 19 at 13:47





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
Mar 19 at 13:47













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
Mar 19 at 16:05






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
Mar 19 at 16:05











3 Answers
3






active

oldest

votes


















5














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
    Mar 19 at 15:40


















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
    Mar 19 at 13:34











  • 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
    Mar 19 at 14:42











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

    – Joseph Wright
    Mar 19 at 14:43











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

    – Sébastien Loisel
    Mar 19 at 15:04











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

    – Sébastien Loisel
    Mar 19 at 15:14


















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



    );













    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









    5














    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
      Mar 19 at 15:40















    5














    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
      Mar 19 at 15:40













    5












    5








    5







    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 Mar 19 at 15:34









    egregegreg

    735k8919343259




    735k8919343259












    • Oh this is good

      – Sébastien Loisel
      Mar 19 at 15:40

















    • Oh this is good

      – Sébastien Loisel
      Mar 19 at 15:40
















    Oh this is good

    – Sébastien Loisel
    Mar 19 at 15:40





    Oh this is good

    – Sébastien Loisel
    Mar 19 at 15:40











    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
      Mar 19 at 13:34











    • 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
      Mar 19 at 14:42











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

      – Joseph Wright
      Mar 19 at 14:43











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

      – Sébastien Loisel
      Mar 19 at 15:04











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

      – Sébastien Loisel
      Mar 19 at 15:14















    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
      Mar 19 at 13:34











    • 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
      Mar 19 at 14:42











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

      – Joseph Wright
      Mar 19 at 14:43











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

      – Sébastien Loisel
      Mar 19 at 15:04











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

      – Sébastien Loisel
      Mar 19 at 15:14













    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 Mar 19 at 13:40

























    answered Mar 19 at 13:34









    Joseph WrightJoseph Wright

    206k23566895




    206k23566895












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

      – Joseph Wright
      Mar 19 at 13:34











    • 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
      Mar 19 at 14:42











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

      – Joseph Wright
      Mar 19 at 14:43











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

      – Sébastien Loisel
      Mar 19 at 15:04











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

      – Sébastien Loisel
      Mar 19 at 15:14

















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

      – Joseph Wright
      Mar 19 at 13:34











    • 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
      Mar 19 at 14:42











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

      – Joseph Wright
      Mar 19 at 14:43











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

      – Sébastien Loisel
      Mar 19 at 15:04











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

      – Sébastien Loisel
      Mar 19 at 15:14
















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

    – Joseph Wright
    Mar 19 at 13:34





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

    – Joseph Wright
    Mar 19 at 13:34













    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
    Mar 19 at 14:42





    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
    Mar 19 at 14:42













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

    – Joseph Wright
    Mar 19 at 14:43





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

    – Joseph Wright
    Mar 19 at 14:43













    What's an example of something not LaTeX2e robust?

    – Sébastien Loisel
    Mar 19 at 15:04





    What's an example of something not LaTeX2e robust?

    – Sébastien Loisel
    Mar 19 at 15:04













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

    – Sébastien Loisel
    Mar 19 at 15:14





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

    – Sébastien Loisel
    Mar 19 at 15:14











    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 Mar 19 at 17:01









        Ulrich DiezUlrich Diez

        5,725620




        5,725620



























            draft saved

            draft discarded
















































            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

            Masuk log Menu navigasi

            Identifying “long and narrow” polygons in with PostGISlength and width of polygonWhy postgis st_overlaps reports Qgis' “avoid intersections” generated polygon as overlapping with others?Adjusting polygons to boundary and filling holesDrawing polygons with fixed area?How to remove spikes in Polygons with PostGISDeleting sliver polygons after difference operation in QGIS?Snapping boundaries in PostGISSplit polygon into parts adding attributes based on underlying polygon in QGISSplitting overlap between polygons and assign to nearest polygon using PostGIS?Expanding polygons and clipping at midpoint?Removing Intersection of Buffers in Same Layers

            Старые Смолеговицы Содержание История | География | Демография | Достопримечательности | Примечания | НавигацияHGЯOLHGЯOL41 206 832 01641 606 406 141Административно-территориальное деление Ленинградской области«Переписная оброчная книга Водской пятины 1500 года», С. 793«Карта Ингерманландии: Ивангорода, Яма, Копорья, Нотеборга», по материалам 1676 г.«Генеральная карта провинции Ингерманландии» Э. Белинга и А. Андерсина, 1704 г., составлена по материалам 1678 г.«Географический чертёж над Ижорскою землей со своими городами» Адриана Шонбека 1705 г.Новая и достоверная всей Ингерманландии ланткарта. Грав. А. Ростовцев. СПб., 1727 г.Топографическая карта Санкт-Петербургской губернии. 5-и верстка. Шуберт. 1834 г.Описание Санкт-Петербургской губернии по уездам и станамСпецкарта западной части России Ф. Ф. Шуберта. 1844 г.Алфавитный список селений по уездам и станам С.-Петербургской губернииСписки населённых мест Российской Империи, составленные и издаваемые центральным статистическим комитетом министерства внутренних дел. XXXVII. Санкт-Петербургская губерния. По состоянию на 1862 год. СПб. 1864. С. 203Материалы по статистике народного хозяйства в С.-Петербургской губернии. Вып. IX. Частновладельческое хозяйство в Ямбургском уезде. СПб, 1888, С. 146, С. 2, 7, 54Положение о гербе муниципального образования Курское сельское поселениеСправочник истории административно-территориального деления Ленинградской области.Топографическая карта Ленинградской области, квадрат О-35-23-В (Хотыницы), 1930 г.АрхивированоАдминистративно-территориальное деление Ленинградской области. — Л., 1933, С. 27, 198АрхивированоАдминистративно-экономический справочник по Ленинградской области. — Л., 1936, с. 219АрхивированоАдминистративно-территориальное деление Ленинградской области. — Л., 1966, с. 175АрхивированоАдминистративно-территориальное деление Ленинградской области. — Лениздат, 1973, С. 180АрхивированоАдминистративно-территориальное деление Ленинградской области. — Лениздат, 1990, ISBN 5-289-00612-5, С. 38АрхивированоАдминистративно-территориальное деление Ленинградской области. — СПб., 2007, с. 60АрхивированоКоряков Юрий База данных «Этно-языковой состав населённых пунктов России». Ленинградская область.Административно-территориальное деление Ленинградской области. — СПб, 1997, ISBN 5-86153-055-6, С. 41АрхивированоКультовый комплекс Старые Смолеговицы // Электронная энциклопедия ЭрмитажаПроблемы выявления, изучения и сохранения культовых комплексов с каменными крестами: по материалам работ 2016-2017 гг. в Ленинградской области