Automatic theorem listPrint all theoremsCode between beginenvironment and endenvironment stored in an arrayRemoving label from a stringAmsbook: Theoremstyles definition and remark identical?Lyx 2.0 Formatted references to theorem/lemma/claim environmentsReference to List Items in Theorem Environment with Label Type “Theorem”Referencing Definition by Name and Optional ArgumentHow can I apply a custom-defined theorem environment to nested theorems *only*?Customizing theorem listCite a theorem by its name and numberPlace theorem number *before* theorem label in page header?No new line for theorem in enumerateRedefine the theorem environment to save its contents

copy and scale one figure (wheel)

How could a planet have erratic days?

Pre-modern battle - command it, or fight in it?

Problem with TransformedDistribution

What is this called? Old film camera viewer?

Lowest total scrabble score

Basic combinatorial probability problem

How can I block email signup overlays or javascript popups in Safari?

Are Captain Marvel's powers affected by Thanos' actions in Avengers: Infinity War?

Is it possible to have a strip of cold climate in the middle of a planet?

What if a revenant (monster) gains fire resistance?

How to indicate a cut out for a product window

Biological Blimps: Propulsion

Melting point of aspirin, contradicting sources

What should you do when eye contact makes your subordinate uncomfortable?

Not using 's' for he/she/it

How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?

Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?

Loading commands from file

How much character growth crosses the line into breaking the character

Are the IPv6 address space and IPv4 address space completely disjoint?

2.8 Why are collections grayed out? How can I open them?

Did arcade monitors have same pixel aspect ratio as TV sets?

Terse Method to Swap Lowest for Highest?



Automatic theorem list


Print all theoremsCode between beginenvironment and endenvironment stored in an arrayRemoving label from a stringAmsbook: Theoremstyles definition and remark identical?Lyx 2.0 Formatted references to theorem/lemma/claim environmentsReference to List Items in Theorem Environment with Label Type “Theorem”Referencing Definition by Name and Optional ArgumentHow can I apply a custom-defined theorem environment to nested theorems *only*?Customizing theorem listCite a theorem by its name and numberPlace theorem number *before* theorem label in page header?No new line for theorem in enumerateRedefine the theorem environment to save its contents













2















In the accepted answer to Code between beginenvironment and endenvironment stored in an array it's shown how to create an automatic list of theorems using expl3 syntax. However the printtheorems command only works if it's called after the theorems.



Is there a way to call it before the theorems? I was thinking a possible way to do it would be to compile the .tex twice, the first time to get the theorems and the second one to display them (like the Table of Contents does with chapters, sections ecc..).



The code linked above and the corresponding output are:



documentclassarticle
usepackageamsmath
usepackageamsthm
usepackageenviron
usepackagexparse

ExplSyntaxOn

NewDocumentCommandnewtheoremxmomo

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
IfValueTF#2

newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2


IfValueTF#4

newtheorem#1@inner#3[#4]


newtheorem#1@inner#3

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1

% define a "grabbing" environment #1 with the usual features
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
exp_not:n ##1 % attribution
exp_not:V BODY % body



% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
seq_map_function:NN g_riccardo_theorems_seq riccardo_printtheorems:n
% end the group
group_end:


cs_new_protected:Nn riccardo_printtheorems:n

% just pass the argument in the form namenumberattributiontext
% to a four argument function
__riccardo_printtheorems:nnnn #1

cs_new_protected:Nn __riccardo_printtheorems:nnnn

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #1 @inner #2
tl_if_empty:nTF #3

% no attribution
begin#1@inner #4 end#1@inner


% attribution
begin#1@inner[#3] #4 end#1@inner



ExplSyntaxOff

newtheoremxtheoremTheorem[section]
newtheoremxlemma[theorem]Lemma

begindocument

sectionFirst test

begintheorem[Important]labelthm:important
This is a theorem about $log_a x$.
endtheorem

beginlemmalabellem:whatever
This is a lemma.
endlemma

begintheoremlabelthm:unimportant
This is another theorem
endtheorem

sectionSecond test

begintheoremlabelthm:soandso
Again a theorem.
endtheorem

sectionTheorems

printtheorems

enddocument


enter image description here



EDIT: MWE showing the problem



documentclassmemoir
usepackageamsmath, amsthm, hyperref, environ, xparse, showkeys

ExplSyntaxOn

NewDocumentCommandnewtheoremxmomo

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
IfValueTF#2

newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2


IfValueTF#4

newtheorem#1@inner#3[#4]


newtheorem#1@inner#3

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1
seq_gput_right:Nn g_riccardo_theorems_counters_seq #1

% define a "grabbing" environment #1 with the usual features
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
exp_not:n ##1 % attribution
exp_not:V BODY % body



% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl
iow_new:N g_riccardo_theorems_stream
seq_new:N g_riccardo_theorems_counters_seq

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
file_if_exist_input:n c_sys_jobname_str.thl
% end the group
group_end:
seq_map_inline:Nn g_riccardo_theorems_counters_seq

setcounter##1@inner0



cs_new_protected:Nn riccardo_theorems_save:

seq_map_function:NN g_riccardo_theorems_seq __riccardo_theorems_save:n

cs_new_protected:Nn __riccardo_theorems_save:n

iow_now:Nn g_riccardo_theorems_stream

savedtheorem #1



AtEndDocument

iow_open:Nn g_riccardo_theorems_stream c_sys_jobname_str.thl
riccardo_theorems_save:
iow_close:N g_riccardo_theorems_stream


NewDocumentCommandsavedtheoremmmmm

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #1 @inner #2
tl_if_empty:nTF #3

% no attribution
begin#1@inner #4 end#1@inner


% attribution
begin#1@inner[#3] #4 end#1@inner



ExplSyntaxOff

newtheoremxtheoremTheorem[chapter]
newtheoremxlemma[theorem]Lemma

begindocument

chapterTheorems

printtheorems

clearpage
sectionFirst theorem

begintheorem[Important] labelthm_1
This is a theorem about $log_a x$.
endtheorem
clearpage

This is a link to hyperref[thm_1]Theorem 1.
enddocument









share|improve this question
























  • Where did you get the idea that printtheorems can be used more than once?

    – egreg
    Feb 10 at 19:01











  • Oh, so the problem has nothing to do with the theorems belonging to different chapters, it's calling printtheorems twice that's causing the issue, right?

    – noibe
    Feb 10 at 19:07











  • Just tried removing the second printtheorems from the MWE I posted, but the theorem in the first chapter is still numbered incorrectly, so I guess different chapters are actually part of the problem.

    – noibe
    Feb 10 at 19:12











  • adding setcountertheorem@inner0 before the first theorem of every chapter seems to do the job.

    – noibe
    Feb 10 at 19:31












  • I fixed the issue with a new version, adding the resetting of the theorem numbers.

    – egreg
    Feb 10 at 20:58















2















In the accepted answer to Code between beginenvironment and endenvironment stored in an array it's shown how to create an automatic list of theorems using expl3 syntax. However the printtheorems command only works if it's called after the theorems.



Is there a way to call it before the theorems? I was thinking a possible way to do it would be to compile the .tex twice, the first time to get the theorems and the second one to display them (like the Table of Contents does with chapters, sections ecc..).



The code linked above and the corresponding output are:



documentclassarticle
usepackageamsmath
usepackageamsthm
usepackageenviron
usepackagexparse

ExplSyntaxOn

NewDocumentCommandnewtheoremxmomo

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
IfValueTF#2

newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2


IfValueTF#4

newtheorem#1@inner#3[#4]


newtheorem#1@inner#3

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1

% define a "grabbing" environment #1 with the usual features
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
exp_not:n ##1 % attribution
exp_not:V BODY % body



% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
seq_map_function:NN g_riccardo_theorems_seq riccardo_printtheorems:n
% end the group
group_end:


cs_new_protected:Nn riccardo_printtheorems:n

% just pass the argument in the form namenumberattributiontext
% to a four argument function
__riccardo_printtheorems:nnnn #1

cs_new_protected:Nn __riccardo_printtheorems:nnnn

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #1 @inner #2
tl_if_empty:nTF #3

% no attribution
begin#1@inner #4 end#1@inner


% attribution
begin#1@inner[#3] #4 end#1@inner



ExplSyntaxOff

newtheoremxtheoremTheorem[section]
newtheoremxlemma[theorem]Lemma

begindocument

sectionFirst test

begintheorem[Important]labelthm:important
This is a theorem about $log_a x$.
endtheorem

beginlemmalabellem:whatever
This is a lemma.
endlemma

begintheoremlabelthm:unimportant
This is another theorem
endtheorem

sectionSecond test

begintheoremlabelthm:soandso
Again a theorem.
endtheorem

sectionTheorems

printtheorems

enddocument


enter image description here



EDIT: MWE showing the problem



documentclassmemoir
usepackageamsmath, amsthm, hyperref, environ, xparse, showkeys

ExplSyntaxOn

NewDocumentCommandnewtheoremxmomo

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
IfValueTF#2

newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2


IfValueTF#4

newtheorem#1@inner#3[#4]


newtheorem#1@inner#3

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1
seq_gput_right:Nn g_riccardo_theorems_counters_seq #1

% define a "grabbing" environment #1 with the usual features
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
exp_not:n ##1 % attribution
exp_not:V BODY % body



% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl
iow_new:N g_riccardo_theorems_stream
seq_new:N g_riccardo_theorems_counters_seq

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
file_if_exist_input:n c_sys_jobname_str.thl
% end the group
group_end:
seq_map_inline:Nn g_riccardo_theorems_counters_seq

setcounter##1@inner0



cs_new_protected:Nn riccardo_theorems_save:

seq_map_function:NN g_riccardo_theorems_seq __riccardo_theorems_save:n

cs_new_protected:Nn __riccardo_theorems_save:n

iow_now:Nn g_riccardo_theorems_stream

savedtheorem #1



AtEndDocument

iow_open:Nn g_riccardo_theorems_stream c_sys_jobname_str.thl
riccardo_theorems_save:
iow_close:N g_riccardo_theorems_stream


NewDocumentCommandsavedtheoremmmmm

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #1 @inner #2
tl_if_empty:nTF #3

% no attribution
begin#1@inner #4 end#1@inner


% attribution
begin#1@inner[#3] #4 end#1@inner



ExplSyntaxOff

newtheoremxtheoremTheorem[chapter]
newtheoremxlemma[theorem]Lemma

begindocument

chapterTheorems

printtheorems

clearpage
sectionFirst theorem

begintheorem[Important] labelthm_1
This is a theorem about $log_a x$.
endtheorem
clearpage

This is a link to hyperref[thm_1]Theorem 1.
enddocument









share|improve this question
























  • Where did you get the idea that printtheorems can be used more than once?

    – egreg
    Feb 10 at 19:01











  • Oh, so the problem has nothing to do with the theorems belonging to different chapters, it's calling printtheorems twice that's causing the issue, right?

    – noibe
    Feb 10 at 19:07











  • Just tried removing the second printtheorems from the MWE I posted, but the theorem in the first chapter is still numbered incorrectly, so I guess different chapters are actually part of the problem.

    – noibe
    Feb 10 at 19:12











  • adding setcountertheorem@inner0 before the first theorem of every chapter seems to do the job.

    – noibe
    Feb 10 at 19:31












  • I fixed the issue with a new version, adding the resetting of the theorem numbers.

    – egreg
    Feb 10 at 20:58













2












2








2


0






In the accepted answer to Code between beginenvironment and endenvironment stored in an array it's shown how to create an automatic list of theorems using expl3 syntax. However the printtheorems command only works if it's called after the theorems.



Is there a way to call it before the theorems? I was thinking a possible way to do it would be to compile the .tex twice, the first time to get the theorems and the second one to display them (like the Table of Contents does with chapters, sections ecc..).



The code linked above and the corresponding output are:



documentclassarticle
usepackageamsmath
usepackageamsthm
usepackageenviron
usepackagexparse

ExplSyntaxOn

NewDocumentCommandnewtheoremxmomo

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
IfValueTF#2

newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2


IfValueTF#4

newtheorem#1@inner#3[#4]


newtheorem#1@inner#3

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1

% define a "grabbing" environment #1 with the usual features
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
exp_not:n ##1 % attribution
exp_not:V BODY % body



% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
seq_map_function:NN g_riccardo_theorems_seq riccardo_printtheorems:n
% end the group
group_end:


cs_new_protected:Nn riccardo_printtheorems:n

% just pass the argument in the form namenumberattributiontext
% to a four argument function
__riccardo_printtheorems:nnnn #1

cs_new_protected:Nn __riccardo_printtheorems:nnnn

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #1 @inner #2
tl_if_empty:nTF #3

% no attribution
begin#1@inner #4 end#1@inner


% attribution
begin#1@inner[#3] #4 end#1@inner



ExplSyntaxOff

newtheoremxtheoremTheorem[section]
newtheoremxlemma[theorem]Lemma

begindocument

sectionFirst test

begintheorem[Important]labelthm:important
This is a theorem about $log_a x$.
endtheorem

beginlemmalabellem:whatever
This is a lemma.
endlemma

begintheoremlabelthm:unimportant
This is another theorem
endtheorem

sectionSecond test

begintheoremlabelthm:soandso
Again a theorem.
endtheorem

sectionTheorems

printtheorems

enddocument


enter image description here



EDIT: MWE showing the problem



documentclassmemoir
usepackageamsmath, amsthm, hyperref, environ, xparse, showkeys

ExplSyntaxOn

NewDocumentCommandnewtheoremxmomo

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
IfValueTF#2

newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2


IfValueTF#4

newtheorem#1@inner#3[#4]


newtheorem#1@inner#3

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1
seq_gput_right:Nn g_riccardo_theorems_counters_seq #1

% define a "grabbing" environment #1 with the usual features
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
exp_not:n ##1 % attribution
exp_not:V BODY % body



% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl
iow_new:N g_riccardo_theorems_stream
seq_new:N g_riccardo_theorems_counters_seq

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
file_if_exist_input:n c_sys_jobname_str.thl
% end the group
group_end:
seq_map_inline:Nn g_riccardo_theorems_counters_seq

setcounter##1@inner0



cs_new_protected:Nn riccardo_theorems_save:

seq_map_function:NN g_riccardo_theorems_seq __riccardo_theorems_save:n

cs_new_protected:Nn __riccardo_theorems_save:n

iow_now:Nn g_riccardo_theorems_stream

savedtheorem #1



AtEndDocument

iow_open:Nn g_riccardo_theorems_stream c_sys_jobname_str.thl
riccardo_theorems_save:
iow_close:N g_riccardo_theorems_stream


NewDocumentCommandsavedtheoremmmmm

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #1 @inner #2
tl_if_empty:nTF #3

% no attribution
begin#1@inner #4 end#1@inner


% attribution
begin#1@inner[#3] #4 end#1@inner



ExplSyntaxOff

newtheoremxtheoremTheorem[chapter]
newtheoremxlemma[theorem]Lemma

begindocument

chapterTheorems

printtheorems

clearpage
sectionFirst theorem

begintheorem[Important] labelthm_1
This is a theorem about $log_a x$.
endtheorem
clearpage

This is a link to hyperref[thm_1]Theorem 1.
enddocument









share|improve this question
















In the accepted answer to Code between beginenvironment and endenvironment stored in an array it's shown how to create an automatic list of theorems using expl3 syntax. However the printtheorems command only works if it's called after the theorems.



Is there a way to call it before the theorems? I was thinking a possible way to do it would be to compile the .tex twice, the first time to get the theorems and the second one to display them (like the Table of Contents does with chapters, sections ecc..).



The code linked above and the corresponding output are:



documentclassarticle
usepackageamsmath
usepackageamsthm
usepackageenviron
usepackagexparse

ExplSyntaxOn

NewDocumentCommandnewtheoremxmomo

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
IfValueTF#2

newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2


IfValueTF#4

newtheorem#1@inner#3[#4]


newtheorem#1@inner#3

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1

% define a "grabbing" environment #1 with the usual features
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
exp_not:n ##1 % attribution
exp_not:V BODY % body



% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
seq_map_function:NN g_riccardo_theorems_seq riccardo_printtheorems:n
% end the group
group_end:


cs_new_protected:Nn riccardo_printtheorems:n

% just pass the argument in the form namenumberattributiontext
% to a four argument function
__riccardo_printtheorems:nnnn #1

cs_new_protected:Nn __riccardo_printtheorems:nnnn

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #1 @inner #2
tl_if_empty:nTF #3

% no attribution
begin#1@inner #4 end#1@inner


% attribution
begin#1@inner[#3] #4 end#1@inner



ExplSyntaxOff

newtheoremxtheoremTheorem[section]
newtheoremxlemma[theorem]Lemma

begindocument

sectionFirst test

begintheorem[Important]labelthm:important
This is a theorem about $log_a x$.
endtheorem

beginlemmalabellem:whatever
This is a lemma.
endlemma

begintheoremlabelthm:unimportant
This is another theorem
endtheorem

sectionSecond test

begintheoremlabelthm:soandso
Again a theorem.
endtheorem

sectionTheorems

printtheorems

enddocument


enter image description here



EDIT: MWE showing the problem



documentclassmemoir
usepackageamsmath, amsthm, hyperref, environ, xparse, showkeys

ExplSyntaxOn

NewDocumentCommandnewtheoremxmomo

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
IfValueTF#2

newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2


IfValueTF#4

newtheorem#1@inner#3[#4]


newtheorem#1@inner#3

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1
seq_gput_right:Nn g_riccardo_theorems_counters_seq #1

% define a "grabbing" environment #1 with the usual features
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
exp_not:n ##1 % attribution
exp_not:V BODY % body



% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl
iow_new:N g_riccardo_theorems_stream
seq_new:N g_riccardo_theorems_counters_seq

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
file_if_exist_input:n c_sys_jobname_str.thl
% end the group
group_end:
seq_map_inline:Nn g_riccardo_theorems_counters_seq

setcounter##1@inner0



cs_new_protected:Nn riccardo_theorems_save:

seq_map_function:NN g_riccardo_theorems_seq __riccardo_theorems_save:n

cs_new_protected:Nn __riccardo_theorems_save:n

iow_now:Nn g_riccardo_theorems_stream

savedtheorem #1



AtEndDocument

iow_open:Nn g_riccardo_theorems_stream c_sys_jobname_str.thl
riccardo_theorems_save:
iow_close:N g_riccardo_theorems_stream


NewDocumentCommandsavedtheoremmmmm

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #1 @inner #2
tl_if_empty:nTF #3

% no attribution
begin#1@inner #4 end#1@inner


% attribution
begin#1@inner[#3] #4 end#1@inner



ExplSyntaxOff

newtheoremxtheoremTheorem[chapter]
newtheoremxlemma[theorem]Lemma

begindocument

chapterTheorems

printtheorems

clearpage
sectionFirst theorem

begintheorem[Important] labelthm_1
This is a theorem about $log_a x$.
endtheorem
clearpage

This is a link to hyperref[thm_1]Theorem 1.
enddocument






macros theorems expl3 amsthm xparse






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 17 at 0:01







noibe

















asked Feb 9 at 18:11









noibenoibe

529113




529113












  • Where did you get the idea that printtheorems can be used more than once?

    – egreg
    Feb 10 at 19:01











  • Oh, so the problem has nothing to do with the theorems belonging to different chapters, it's calling printtheorems twice that's causing the issue, right?

    – noibe
    Feb 10 at 19:07











  • Just tried removing the second printtheorems from the MWE I posted, but the theorem in the first chapter is still numbered incorrectly, so I guess different chapters are actually part of the problem.

    – noibe
    Feb 10 at 19:12











  • adding setcountertheorem@inner0 before the first theorem of every chapter seems to do the job.

    – noibe
    Feb 10 at 19:31












  • I fixed the issue with a new version, adding the resetting of the theorem numbers.

    – egreg
    Feb 10 at 20:58

















  • Where did you get the idea that printtheorems can be used more than once?

    – egreg
    Feb 10 at 19:01











  • Oh, so the problem has nothing to do with the theorems belonging to different chapters, it's calling printtheorems twice that's causing the issue, right?

    – noibe
    Feb 10 at 19:07











  • Just tried removing the second printtheorems from the MWE I posted, but the theorem in the first chapter is still numbered incorrectly, so I guess different chapters are actually part of the problem.

    – noibe
    Feb 10 at 19:12











  • adding setcountertheorem@inner0 before the first theorem of every chapter seems to do the job.

    – noibe
    Feb 10 at 19:31












  • I fixed the issue with a new version, adding the resetting of the theorem numbers.

    – egreg
    Feb 10 at 20:58
















Where did you get the idea that printtheorems can be used more than once?

– egreg
Feb 10 at 19:01





Where did you get the idea that printtheorems can be used more than once?

– egreg
Feb 10 at 19:01













Oh, so the problem has nothing to do with the theorems belonging to different chapters, it's calling printtheorems twice that's causing the issue, right?

– noibe
Feb 10 at 19:07





Oh, so the problem has nothing to do with the theorems belonging to different chapters, it's calling printtheorems twice that's causing the issue, right?

– noibe
Feb 10 at 19:07













Just tried removing the second printtheorems from the MWE I posted, but the theorem in the first chapter is still numbered incorrectly, so I guess different chapters are actually part of the problem.

– noibe
Feb 10 at 19:12





Just tried removing the second printtheorems from the MWE I posted, but the theorem in the first chapter is still numbered incorrectly, so I guess different chapters are actually part of the problem.

– noibe
Feb 10 at 19:12













adding setcountertheorem@inner0 before the first theorem of every chapter seems to do the job.

– noibe
Feb 10 at 19:31






adding setcountertheorem@inner0 before the first theorem of every chapter seems to do the job.

– noibe
Feb 10 at 19:31














I fixed the issue with a new version, adding the resetting of the theorem numbers.

– egreg
Feb 10 at 20:58





I fixed the issue with a new version, adding the resetting of the theorem numbers.

– egreg
Feb 10 at 20:58










1 Answer
1






active

oldest

votes


















5














The theorems can be saved in a sequence as before; at end document they can be written to an auxiliary file and printtheorems inputs it (if it exists).



documentclassarticle
usepackageamsmath
usepackageamsthm
usepackagexparse,environ

ExplSyntaxOn

NewDocumentCommandnewtheoremxsmomo

IfBooleanTF#1

printthm_nonumber:nn #2 #4


printthm_number:nnnn #2 #3 #4 #5



cs_new_protected:Nn printthm_nonumber:nn

newtheorem*#1@inner#2
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

*
#1 % name
% number
tl_if_novalue:nF ##1 [##1] % attribution
exp_not:V BODY % body




cs_new_protected:Nn printthm_number:nnnn

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
tl_if_novalue:nTF #2

tl_if_novalue:nTF #4

newtheorem#1@inner#3


newtheorem#1@inner#3[#4]

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1
seq_gput_right:Nn g_riccardo_theorems_counters_seq #1


newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2

% define a "grabbing" environment #1 with the usual features
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
IfValueT##1 [exp_not:n ##1 ] % attribution
exp_not:V BODY % body




% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl
iow_new:N g_riccardo_theorems_stream
seq_new:N g_riccardo_theorems_counters_seq

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
file_if_exist_input:n c_sys_jobname_str.thl
% end the group
group_end:
seq_map_inline:Nn g_riccardo_theorems_counters_seq

setcounter##1@inner0



cs_new_protected:Nn riccardo_theorems_save:

seq_map_function:NN g_riccardo_theorems_seq __riccardo_theorems_save:n

cs_new_protected:Nn __riccardo_theorems_save:n

iow_now:Nn g_riccardo_theorems_stream

savedtheorem #1


AtEndDocument

iow_open:Nn g_riccardo_theorems_stream c_sys_jobname_str.thl
riccardo_theorems_save:
iow_close:N g_riccardo_theorems_stream


NewDocumentCommandsavedtheoremsmmom

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
IfBooleanTF#1

IfNoValueTF#4
begin#2@inner#5end#2@inner
begin#2@inner[#4]#5end#2@inner


cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #2 @inner #3
IfNoValueTF#4

% no attribution
begin#2@inner #5 end#2@inner


% attribution
begin#2@inner[#4] #5 end#2@inner




ExplSyntaxOff

newtheoremx*theorem*Theorem
newtheoremxtheoremTheorem[section]
newtheoremxlemma[theorem]Lemma

begindocument

sectionTheorems

printtheorems

sectionFirst test

begintheorem*
This is not numbered.
endtheorem*

begintheorem[Important]labelthm:important
This is a theorem about $log_a x$.
endtheorem

beginlemmalabellem:whatever
This is a lemma.
endlemma

begintheoremlabelthm:unimportant
This is another theorem
endtheorem

sectionSecond test

begintheoremlabelthm:soandso
Again a theorem.
endtheorem

enddocument


enter image description here



The following version requires xparse released 2019-03-05 or later.



documentclassarticle
usepackageamsmath
usepackageamsthm
usepackagexparse

ExplSyntaxOn

NewDocumentCommandnewtheoremxsmomo

IfBooleanTF#1

printthm_nonumber:nn #2 #4


printthm_number:nnnn #2 #3 #4 #5



cs_new_protected:Nn printthm_nonumber:nn

newtheorem*#1@inner#2
NewDocumentEnvironment#1 o +b

% start the inner environment (without or with optional argument)
IfNoValueTF ##1
begin#1@inner
begin#1@inner[##1]
% typeset the statement
##2
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

*
#1 % name
% number
tl_if_novalue:nF ##1 [##1] % attribution
exp_not:n ##2 % body





cs_new_protected:Nn printthm_number:nnnn

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
tl_if_novalue:nTF #2

tl_if_novalue:nTF #4

newtheorem#1@inner#3


newtheorem#1@inner#3[#4]

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1
seq_gput_right:Nn g_riccardo_theorems_counters_seq #1


newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2

% define a "grabbing" environment #1 with the usual features
NewDocumentEnvironment#1 o +b

% start the inner environment (without or with optional argument)
IfNoValueTF##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
##2
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
IfValueT##1 [exp_not:n ##1 ] % attribution
exp_not:n ##2 % body




% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl
iow_new:N g_riccardo_theorems_stream
seq_new:N g_riccardo_theorems_counters_seq

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
file_if_exist_input:n c_sys_jobname_str.thl
% end the group
group_end:
seq_map_inline:Nn g_riccardo_theorems_counters_seq

setcounter##1@inner0



cs_new_protected:Nn riccardo_theorems_save:

seq_map_function:NN g_riccardo_theorems_seq __riccardo_theorems_save:n

cs_new_protected:Nn __riccardo_theorems_save:n

iow_now:Nn g_riccardo_theorems_stream

savedtheorem #1


AtEndDocument

iow_open:Nn g_riccardo_theorems_stream c_sys_jobname_str.thl
riccardo_theorems_save:
iow_close:N g_riccardo_theorems_stream


NewDocumentCommandsavedtheoremsmmom

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
IfBooleanTF#1

IfNoValueTF#4
begin#2@inner#5end#2@inner
begin#2@inner[#4]#5end#2@inner


cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #2 @inner #3
IfNoValueTF#4

% no attribution
begin#2@inner #5 end#2@inner


% attribution
begin#2@inner[#4] #5 end#2@inner




ExplSyntaxOff

newtheoremx*theorem*Theorem
newtheoremxtheoremTheorem[section]
newtheoremxlemma[theorem]Lemma

begindocument

sectionTheorems

printtheorems

sectionFirst test

begintheorem*
This is not numbered.
endtheorem*

begintheorem[Important]labelthm:important
This is a theorem about $log_a x$.
endtheorem

beginlemmalabellem:whatever
This is a lemma.
endlemma

begintheoremlabelthm:unimportant
This is another theorem
endtheorem

sectionSecond test

begintheoremlabelthm:soandso
Again a theorem.
endtheorem

enddocument





share|improve this answer

























  • I just found out that the theorem numbering gets messed up when the theorems belong to different chapters. I just added a MWE showing the problem to the original question. Would it help to divide the theorems printed into chapters, so that printtheorems only prints the theorems relative to the current chapter? That would be the desired behaviour anyways, so if it also fixes the numbering problem it'd kill two birds with one stone.

    – noibe
    Feb 10 at 16:54












  • I'm sorry to bother you again, I may have found another inconvenience. When the theorems are numbered within a chapter, nullifying the label isn't effective and hyperref links (in general) to the wrong page. I tried removing the string label (if present) from line 52, so that it won't be written as part of the body in the .thl, but without ay success. MWE provided in the original post.

    – noibe
    Feb 17 at 0:09











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%2f474089%2fautomatic-theorem-list%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














The theorems can be saved in a sequence as before; at end document they can be written to an auxiliary file and printtheorems inputs it (if it exists).



documentclassarticle
usepackageamsmath
usepackageamsthm
usepackagexparse,environ

ExplSyntaxOn

NewDocumentCommandnewtheoremxsmomo

IfBooleanTF#1

printthm_nonumber:nn #2 #4


printthm_number:nnnn #2 #3 #4 #5



cs_new_protected:Nn printthm_nonumber:nn

newtheorem*#1@inner#2
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

*
#1 % name
% number
tl_if_novalue:nF ##1 [##1] % attribution
exp_not:V BODY % body




cs_new_protected:Nn printthm_number:nnnn

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
tl_if_novalue:nTF #2

tl_if_novalue:nTF #4

newtheorem#1@inner#3


newtheorem#1@inner#3[#4]

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1
seq_gput_right:Nn g_riccardo_theorems_counters_seq #1


newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2

% define a "grabbing" environment #1 with the usual features
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
IfValueT##1 [exp_not:n ##1 ] % attribution
exp_not:V BODY % body




% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl
iow_new:N g_riccardo_theorems_stream
seq_new:N g_riccardo_theorems_counters_seq

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
file_if_exist_input:n c_sys_jobname_str.thl
% end the group
group_end:
seq_map_inline:Nn g_riccardo_theorems_counters_seq

setcounter##1@inner0



cs_new_protected:Nn riccardo_theorems_save:

seq_map_function:NN g_riccardo_theorems_seq __riccardo_theorems_save:n

cs_new_protected:Nn __riccardo_theorems_save:n

iow_now:Nn g_riccardo_theorems_stream

savedtheorem #1


AtEndDocument

iow_open:Nn g_riccardo_theorems_stream c_sys_jobname_str.thl
riccardo_theorems_save:
iow_close:N g_riccardo_theorems_stream


NewDocumentCommandsavedtheoremsmmom

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
IfBooleanTF#1

IfNoValueTF#4
begin#2@inner#5end#2@inner
begin#2@inner[#4]#5end#2@inner


cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #2 @inner #3
IfNoValueTF#4

% no attribution
begin#2@inner #5 end#2@inner


% attribution
begin#2@inner[#4] #5 end#2@inner




ExplSyntaxOff

newtheoremx*theorem*Theorem
newtheoremxtheoremTheorem[section]
newtheoremxlemma[theorem]Lemma

begindocument

sectionTheorems

printtheorems

sectionFirst test

begintheorem*
This is not numbered.
endtheorem*

begintheorem[Important]labelthm:important
This is a theorem about $log_a x$.
endtheorem

beginlemmalabellem:whatever
This is a lemma.
endlemma

begintheoremlabelthm:unimportant
This is another theorem
endtheorem

sectionSecond test

begintheoremlabelthm:soandso
Again a theorem.
endtheorem

enddocument


enter image description here



The following version requires xparse released 2019-03-05 or later.



documentclassarticle
usepackageamsmath
usepackageamsthm
usepackagexparse

ExplSyntaxOn

NewDocumentCommandnewtheoremxsmomo

IfBooleanTF#1

printthm_nonumber:nn #2 #4


printthm_number:nnnn #2 #3 #4 #5



cs_new_protected:Nn printthm_nonumber:nn

newtheorem*#1@inner#2
NewDocumentEnvironment#1 o +b

% start the inner environment (without or with optional argument)
IfNoValueTF ##1
begin#1@inner
begin#1@inner[##1]
% typeset the statement
##2
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

*
#1 % name
% number
tl_if_novalue:nF ##1 [##1] % attribution
exp_not:n ##2 % body





cs_new_protected:Nn printthm_number:nnnn

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
tl_if_novalue:nTF #2

tl_if_novalue:nTF #4

newtheorem#1@inner#3


newtheorem#1@inner#3[#4]

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1
seq_gput_right:Nn g_riccardo_theorems_counters_seq #1


newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2

% define a "grabbing" environment #1 with the usual features
NewDocumentEnvironment#1 o +b

% start the inner environment (without or with optional argument)
IfNoValueTF##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
##2
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
IfValueT##1 [exp_not:n ##1 ] % attribution
exp_not:n ##2 % body




% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl
iow_new:N g_riccardo_theorems_stream
seq_new:N g_riccardo_theorems_counters_seq

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
file_if_exist_input:n c_sys_jobname_str.thl
% end the group
group_end:
seq_map_inline:Nn g_riccardo_theorems_counters_seq

setcounter##1@inner0



cs_new_protected:Nn riccardo_theorems_save:

seq_map_function:NN g_riccardo_theorems_seq __riccardo_theorems_save:n

cs_new_protected:Nn __riccardo_theorems_save:n

iow_now:Nn g_riccardo_theorems_stream

savedtheorem #1


AtEndDocument

iow_open:Nn g_riccardo_theorems_stream c_sys_jobname_str.thl
riccardo_theorems_save:
iow_close:N g_riccardo_theorems_stream


NewDocumentCommandsavedtheoremsmmom

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
IfBooleanTF#1

IfNoValueTF#4
begin#2@inner#5end#2@inner
begin#2@inner[#4]#5end#2@inner


cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #2 @inner #3
IfNoValueTF#4

% no attribution
begin#2@inner #5 end#2@inner


% attribution
begin#2@inner[#4] #5 end#2@inner




ExplSyntaxOff

newtheoremx*theorem*Theorem
newtheoremxtheoremTheorem[section]
newtheoremxlemma[theorem]Lemma

begindocument

sectionTheorems

printtheorems

sectionFirst test

begintheorem*
This is not numbered.
endtheorem*

begintheorem[Important]labelthm:important
This is a theorem about $log_a x$.
endtheorem

beginlemmalabellem:whatever
This is a lemma.
endlemma

begintheoremlabelthm:unimportant
This is another theorem
endtheorem

sectionSecond test

begintheoremlabelthm:soandso
Again a theorem.
endtheorem

enddocument





share|improve this answer

























  • I just found out that the theorem numbering gets messed up when the theorems belong to different chapters. I just added a MWE showing the problem to the original question. Would it help to divide the theorems printed into chapters, so that printtheorems only prints the theorems relative to the current chapter? That would be the desired behaviour anyways, so if it also fixes the numbering problem it'd kill two birds with one stone.

    – noibe
    Feb 10 at 16:54












  • I'm sorry to bother you again, I may have found another inconvenience. When the theorems are numbered within a chapter, nullifying the label isn't effective and hyperref links (in general) to the wrong page. I tried removing the string label (if present) from line 52, so that it won't be written as part of the body in the .thl, but without ay success. MWE provided in the original post.

    – noibe
    Feb 17 at 0:09
















5














The theorems can be saved in a sequence as before; at end document they can be written to an auxiliary file and printtheorems inputs it (if it exists).



documentclassarticle
usepackageamsmath
usepackageamsthm
usepackagexparse,environ

ExplSyntaxOn

NewDocumentCommandnewtheoremxsmomo

IfBooleanTF#1

printthm_nonumber:nn #2 #4


printthm_number:nnnn #2 #3 #4 #5



cs_new_protected:Nn printthm_nonumber:nn

newtheorem*#1@inner#2
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

*
#1 % name
% number
tl_if_novalue:nF ##1 [##1] % attribution
exp_not:V BODY % body




cs_new_protected:Nn printthm_number:nnnn

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
tl_if_novalue:nTF #2

tl_if_novalue:nTF #4

newtheorem#1@inner#3


newtheorem#1@inner#3[#4]

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1
seq_gput_right:Nn g_riccardo_theorems_counters_seq #1


newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2

% define a "grabbing" environment #1 with the usual features
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
IfValueT##1 [exp_not:n ##1 ] % attribution
exp_not:V BODY % body




% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl
iow_new:N g_riccardo_theorems_stream
seq_new:N g_riccardo_theorems_counters_seq

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
file_if_exist_input:n c_sys_jobname_str.thl
% end the group
group_end:
seq_map_inline:Nn g_riccardo_theorems_counters_seq

setcounter##1@inner0



cs_new_protected:Nn riccardo_theorems_save:

seq_map_function:NN g_riccardo_theorems_seq __riccardo_theorems_save:n

cs_new_protected:Nn __riccardo_theorems_save:n

iow_now:Nn g_riccardo_theorems_stream

savedtheorem #1


AtEndDocument

iow_open:Nn g_riccardo_theorems_stream c_sys_jobname_str.thl
riccardo_theorems_save:
iow_close:N g_riccardo_theorems_stream


NewDocumentCommandsavedtheoremsmmom

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
IfBooleanTF#1

IfNoValueTF#4
begin#2@inner#5end#2@inner
begin#2@inner[#4]#5end#2@inner


cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #2 @inner #3
IfNoValueTF#4

% no attribution
begin#2@inner #5 end#2@inner


% attribution
begin#2@inner[#4] #5 end#2@inner




ExplSyntaxOff

newtheoremx*theorem*Theorem
newtheoremxtheoremTheorem[section]
newtheoremxlemma[theorem]Lemma

begindocument

sectionTheorems

printtheorems

sectionFirst test

begintheorem*
This is not numbered.
endtheorem*

begintheorem[Important]labelthm:important
This is a theorem about $log_a x$.
endtheorem

beginlemmalabellem:whatever
This is a lemma.
endlemma

begintheoremlabelthm:unimportant
This is another theorem
endtheorem

sectionSecond test

begintheoremlabelthm:soandso
Again a theorem.
endtheorem

enddocument


enter image description here



The following version requires xparse released 2019-03-05 or later.



documentclassarticle
usepackageamsmath
usepackageamsthm
usepackagexparse

ExplSyntaxOn

NewDocumentCommandnewtheoremxsmomo

IfBooleanTF#1

printthm_nonumber:nn #2 #4


printthm_number:nnnn #2 #3 #4 #5



cs_new_protected:Nn printthm_nonumber:nn

newtheorem*#1@inner#2
NewDocumentEnvironment#1 o +b

% start the inner environment (without or with optional argument)
IfNoValueTF ##1
begin#1@inner
begin#1@inner[##1]
% typeset the statement
##2
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

*
#1 % name
% number
tl_if_novalue:nF ##1 [##1] % attribution
exp_not:n ##2 % body





cs_new_protected:Nn printthm_number:nnnn

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
tl_if_novalue:nTF #2

tl_if_novalue:nTF #4

newtheorem#1@inner#3


newtheorem#1@inner#3[#4]

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1
seq_gput_right:Nn g_riccardo_theorems_counters_seq #1


newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2

% define a "grabbing" environment #1 with the usual features
NewDocumentEnvironment#1 o +b

% start the inner environment (without or with optional argument)
IfNoValueTF##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
##2
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
IfValueT##1 [exp_not:n ##1 ] % attribution
exp_not:n ##2 % body




% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl
iow_new:N g_riccardo_theorems_stream
seq_new:N g_riccardo_theorems_counters_seq

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
file_if_exist_input:n c_sys_jobname_str.thl
% end the group
group_end:
seq_map_inline:Nn g_riccardo_theorems_counters_seq

setcounter##1@inner0



cs_new_protected:Nn riccardo_theorems_save:

seq_map_function:NN g_riccardo_theorems_seq __riccardo_theorems_save:n

cs_new_protected:Nn __riccardo_theorems_save:n

iow_now:Nn g_riccardo_theorems_stream

savedtheorem #1


AtEndDocument

iow_open:Nn g_riccardo_theorems_stream c_sys_jobname_str.thl
riccardo_theorems_save:
iow_close:N g_riccardo_theorems_stream


NewDocumentCommandsavedtheoremsmmom

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
IfBooleanTF#1

IfNoValueTF#4
begin#2@inner#5end#2@inner
begin#2@inner[#4]#5end#2@inner


cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #2 @inner #3
IfNoValueTF#4

% no attribution
begin#2@inner #5 end#2@inner


% attribution
begin#2@inner[#4] #5 end#2@inner




ExplSyntaxOff

newtheoremx*theorem*Theorem
newtheoremxtheoremTheorem[section]
newtheoremxlemma[theorem]Lemma

begindocument

sectionTheorems

printtheorems

sectionFirst test

begintheorem*
This is not numbered.
endtheorem*

begintheorem[Important]labelthm:important
This is a theorem about $log_a x$.
endtheorem

beginlemmalabellem:whatever
This is a lemma.
endlemma

begintheoremlabelthm:unimportant
This is another theorem
endtheorem

sectionSecond test

begintheoremlabelthm:soandso
Again a theorem.
endtheorem

enddocument





share|improve this answer

























  • I just found out that the theorem numbering gets messed up when the theorems belong to different chapters. I just added a MWE showing the problem to the original question. Would it help to divide the theorems printed into chapters, so that printtheorems only prints the theorems relative to the current chapter? That would be the desired behaviour anyways, so if it also fixes the numbering problem it'd kill two birds with one stone.

    – noibe
    Feb 10 at 16:54












  • I'm sorry to bother you again, I may have found another inconvenience. When the theorems are numbered within a chapter, nullifying the label isn't effective and hyperref links (in general) to the wrong page. I tried removing the string label (if present) from line 52, so that it won't be written as part of the body in the .thl, but without ay success. MWE provided in the original post.

    – noibe
    Feb 17 at 0:09














5












5








5







The theorems can be saved in a sequence as before; at end document they can be written to an auxiliary file and printtheorems inputs it (if it exists).



documentclassarticle
usepackageamsmath
usepackageamsthm
usepackagexparse,environ

ExplSyntaxOn

NewDocumentCommandnewtheoremxsmomo

IfBooleanTF#1

printthm_nonumber:nn #2 #4


printthm_number:nnnn #2 #3 #4 #5



cs_new_protected:Nn printthm_nonumber:nn

newtheorem*#1@inner#2
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

*
#1 % name
% number
tl_if_novalue:nF ##1 [##1] % attribution
exp_not:V BODY % body




cs_new_protected:Nn printthm_number:nnnn

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
tl_if_novalue:nTF #2

tl_if_novalue:nTF #4

newtheorem#1@inner#3


newtheorem#1@inner#3[#4]

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1
seq_gput_right:Nn g_riccardo_theorems_counters_seq #1


newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2

% define a "grabbing" environment #1 with the usual features
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
IfValueT##1 [exp_not:n ##1 ] % attribution
exp_not:V BODY % body




% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl
iow_new:N g_riccardo_theorems_stream
seq_new:N g_riccardo_theorems_counters_seq

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
file_if_exist_input:n c_sys_jobname_str.thl
% end the group
group_end:
seq_map_inline:Nn g_riccardo_theorems_counters_seq

setcounter##1@inner0



cs_new_protected:Nn riccardo_theorems_save:

seq_map_function:NN g_riccardo_theorems_seq __riccardo_theorems_save:n

cs_new_protected:Nn __riccardo_theorems_save:n

iow_now:Nn g_riccardo_theorems_stream

savedtheorem #1


AtEndDocument

iow_open:Nn g_riccardo_theorems_stream c_sys_jobname_str.thl
riccardo_theorems_save:
iow_close:N g_riccardo_theorems_stream


NewDocumentCommandsavedtheoremsmmom

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
IfBooleanTF#1

IfNoValueTF#4
begin#2@inner#5end#2@inner
begin#2@inner[#4]#5end#2@inner


cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #2 @inner #3
IfNoValueTF#4

% no attribution
begin#2@inner #5 end#2@inner


% attribution
begin#2@inner[#4] #5 end#2@inner




ExplSyntaxOff

newtheoremx*theorem*Theorem
newtheoremxtheoremTheorem[section]
newtheoremxlemma[theorem]Lemma

begindocument

sectionTheorems

printtheorems

sectionFirst test

begintheorem*
This is not numbered.
endtheorem*

begintheorem[Important]labelthm:important
This is a theorem about $log_a x$.
endtheorem

beginlemmalabellem:whatever
This is a lemma.
endlemma

begintheoremlabelthm:unimportant
This is another theorem
endtheorem

sectionSecond test

begintheoremlabelthm:soandso
Again a theorem.
endtheorem

enddocument


enter image description here



The following version requires xparse released 2019-03-05 or later.



documentclassarticle
usepackageamsmath
usepackageamsthm
usepackagexparse

ExplSyntaxOn

NewDocumentCommandnewtheoremxsmomo

IfBooleanTF#1

printthm_nonumber:nn #2 #4


printthm_number:nnnn #2 #3 #4 #5



cs_new_protected:Nn printthm_nonumber:nn

newtheorem*#1@inner#2
NewDocumentEnvironment#1 o +b

% start the inner environment (without or with optional argument)
IfNoValueTF ##1
begin#1@inner
begin#1@inner[##1]
% typeset the statement
##2
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

*
#1 % name
% number
tl_if_novalue:nF ##1 [##1] % attribution
exp_not:n ##2 % body





cs_new_protected:Nn printthm_number:nnnn

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
tl_if_novalue:nTF #2

tl_if_novalue:nTF #4

newtheorem#1@inner#3


newtheorem#1@inner#3[#4]

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1
seq_gput_right:Nn g_riccardo_theorems_counters_seq #1


newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2

% define a "grabbing" environment #1 with the usual features
NewDocumentEnvironment#1 o +b

% start the inner environment (without or with optional argument)
IfNoValueTF##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
##2
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
IfValueT##1 [exp_not:n ##1 ] % attribution
exp_not:n ##2 % body




% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl
iow_new:N g_riccardo_theorems_stream
seq_new:N g_riccardo_theorems_counters_seq

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
file_if_exist_input:n c_sys_jobname_str.thl
% end the group
group_end:
seq_map_inline:Nn g_riccardo_theorems_counters_seq

setcounter##1@inner0



cs_new_protected:Nn riccardo_theorems_save:

seq_map_function:NN g_riccardo_theorems_seq __riccardo_theorems_save:n

cs_new_protected:Nn __riccardo_theorems_save:n

iow_now:Nn g_riccardo_theorems_stream

savedtheorem #1


AtEndDocument

iow_open:Nn g_riccardo_theorems_stream c_sys_jobname_str.thl
riccardo_theorems_save:
iow_close:N g_riccardo_theorems_stream


NewDocumentCommandsavedtheoremsmmom

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
IfBooleanTF#1

IfNoValueTF#4
begin#2@inner#5end#2@inner
begin#2@inner[#4]#5end#2@inner


cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #2 @inner #3
IfNoValueTF#4

% no attribution
begin#2@inner #5 end#2@inner


% attribution
begin#2@inner[#4] #5 end#2@inner




ExplSyntaxOff

newtheoremx*theorem*Theorem
newtheoremxtheoremTheorem[section]
newtheoremxlemma[theorem]Lemma

begindocument

sectionTheorems

printtheorems

sectionFirst test

begintheorem*
This is not numbered.
endtheorem*

begintheorem[Important]labelthm:important
This is a theorem about $log_a x$.
endtheorem

beginlemmalabellem:whatever
This is a lemma.
endlemma

begintheoremlabelthm:unimportant
This is another theorem
endtheorem

sectionSecond test

begintheoremlabelthm:soandso
Again a theorem.
endtheorem

enddocument





share|improve this answer















The theorems can be saved in a sequence as before; at end document they can be written to an auxiliary file and printtheorems inputs it (if it exists).



documentclassarticle
usepackageamsmath
usepackageamsthm
usepackagexparse,environ

ExplSyntaxOn

NewDocumentCommandnewtheoremxsmomo

IfBooleanTF#1

printthm_nonumber:nn #2 #4


printthm_number:nnnn #2 #3 #4 #5



cs_new_protected:Nn printthm_nonumber:nn

newtheorem*#1@inner#2
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

*
#1 % name
% number
tl_if_novalue:nF ##1 [##1] % attribution
exp_not:V BODY % body




cs_new_protected:Nn printthm_number:nnnn

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
tl_if_novalue:nTF #2

tl_if_novalue:nTF #4

newtheorem#1@inner#3


newtheorem#1@inner#3[#4]

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1
seq_gput_right:Nn g_riccardo_theorems_counters_seq #1


newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2

% define a "grabbing" environment #1 with the usual features
NewEnviron#1[1][]

% start the inner environment (without or with optional argument)
tl_if_empty:nTF ##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
BODY
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
IfValueT##1 [exp_not:n ##1 ] % attribution
exp_not:V BODY % body




% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl
iow_new:N g_riccardo_theorems_stream
seq_new:N g_riccardo_theorems_counters_seq

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
file_if_exist_input:n c_sys_jobname_str.thl
% end the group
group_end:
seq_map_inline:Nn g_riccardo_theorems_counters_seq

setcounter##1@inner0



cs_new_protected:Nn riccardo_theorems_save:

seq_map_function:NN g_riccardo_theorems_seq __riccardo_theorems_save:n

cs_new_protected:Nn __riccardo_theorems_save:n

iow_now:Nn g_riccardo_theorems_stream

savedtheorem #1


AtEndDocument

iow_open:Nn g_riccardo_theorems_stream c_sys_jobname_str.thl
riccardo_theorems_save:
iow_close:N g_riccardo_theorems_stream


NewDocumentCommandsavedtheoremsmmom

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
IfBooleanTF#1

IfNoValueTF#4
begin#2@inner#5end#2@inner
begin#2@inner[#4]#5end#2@inner


cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #2 @inner #3
IfNoValueTF#4

% no attribution
begin#2@inner #5 end#2@inner


% attribution
begin#2@inner[#4] #5 end#2@inner




ExplSyntaxOff

newtheoremx*theorem*Theorem
newtheoremxtheoremTheorem[section]
newtheoremxlemma[theorem]Lemma

begindocument

sectionTheorems

printtheorems

sectionFirst test

begintheorem*
This is not numbered.
endtheorem*

begintheorem[Important]labelthm:important
This is a theorem about $log_a x$.
endtheorem

beginlemmalabellem:whatever
This is a lemma.
endlemma

begintheoremlabelthm:unimportant
This is another theorem
endtheorem

sectionSecond test

begintheoremlabelthm:soandso
Again a theorem.
endtheorem

enddocument


enter image description here



The following version requires xparse released 2019-03-05 or later.



documentclassarticle
usepackageamsmath
usepackageamsthm
usepackagexparse

ExplSyntaxOn

NewDocumentCommandnewtheoremxsmomo

IfBooleanTF#1

printthm_nonumber:nn #2 #4


printthm_number:nnnn #2 #3 #4 #5



cs_new_protected:Nn printthm_nonumber:nn

newtheorem*#1@inner#2
NewDocumentEnvironment#1 o +b

% start the inner environment (without or with optional argument)
IfNoValueTF ##1
begin#1@inner
begin#1@inner[##1]
% typeset the statement
##2
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

*
#1 % name
% number
tl_if_novalue:nF ##1 [##1] % attribution
exp_not:n ##2 % body





cs_new_protected:Nn printthm_number:nnnn

% * Let's duplicate the working of newtheorem; newtheoremx should
% be used for theorems that need to be listed
% * newtheoremxtheoremTheorem will actually do
% newtheoremtheorem@innerTheorem (honoring the usual optional arguments)
% * We also need a property list to store along with the theorem
% the one which it is subordinate to
tl_if_novalue:nTF #2

tl_if_novalue:nTF #4

newtheorem#1@inner#3


newtheorem#1@inner#3[#4]

% #1 is not subordinate, store the name itself
prop_gput:Nnn g_riccardo_theorems_prop #1 #1
seq_gput_right:Nn g_riccardo_theorems_counters_seq #1


newtheorem#1@inner[#2@inner]#3
% #1 is subordinate to #2
prop_gput:Nnn g_riccardo_theorems_prop #1 #2

% define a "grabbing" environment #1 with the usual features
NewDocumentEnvironment#1 o +b

% start the inner environment (without or with optional argument)
IfNoValueTF##1
begin#1@inner
begin#1@inner[##1]
% save the statement number
tl_gset:Nx g__riccardo_theorems_number_tl use:c @currentlabel
% typeset the statement
##2
% end the inner environment
end#1@inner
% store the statement in a sequence variable, actually as
% four arguments as shown
seq_gput_right:Nx g_riccardo_theorems_seq

#1 % name
g__riccardo_theorems_number_tl % number
IfValueT##1 [exp_not:n ##1 ] % attribution
exp_not:n ##2 % body




% allocate the needed variables
prop_new:N g_riccardo_theorems_prop
seq_new:N g_riccardo_theorems_seq
tl_new:N g__riccardo_theorems_number_tl
iow_new:N g_riccardo_theorems_stream
seq_new:N g_riccardo_theorems_counters_seq

% print the stored theorems
NewDocumentCommandprinttheorems

% we need a group where nullifying the action of label
group_begin:
cs_set_eq:NN label use_none:n
% map the sequence, passing each item to the function that prints a theorem
file_if_exist_input:n c_sys_jobname_str.thl
% end the group
group_end:
seq_map_inline:Nn g_riccardo_theorems_counters_seq

setcounter##1@inner0



cs_new_protected:Nn riccardo_theorems_save:

seq_map_function:NN g_riccardo_theorems_seq __riccardo_theorems_save:n

cs_new_protected:Nn __riccardo_theorems_save:n

iow_now:Nn g_riccardo_theorems_stream

savedtheorem #1


AtEndDocument

iow_open:Nn g_riccardo_theorems_stream c_sys_jobname_str.thl
riccardo_theorems_save:
iow_close:N g_riccardo_theorems_stream


NewDocumentCommandsavedtheoremsmmom

% redefine the<statement>@inner to yield the stored number
% we use the property list to use the correct counter
% (for instance, in case of "lemma", <statement> will be "theorem"
IfBooleanTF#1

IfNoValueTF#4
begin#2@inner#5end#2@inner
begin#2@inner[#4]#5end#2@inner


cs_set:cpn the prop_item:Nn g_riccardo_theorems_prop #2 @inner #3
IfNoValueTF#4

% no attribution
begin#2@inner #5 end#2@inner


% attribution
begin#2@inner[#4] #5 end#2@inner




ExplSyntaxOff

newtheoremx*theorem*Theorem
newtheoremxtheoremTheorem[section]
newtheoremxlemma[theorem]Lemma

begindocument

sectionTheorems

printtheorems

sectionFirst test

begintheorem*
This is not numbered.
endtheorem*

begintheorem[Important]labelthm:important
This is a theorem about $log_a x$.
endtheorem

beginlemmalabellem:whatever
This is a lemma.
endlemma

begintheoremlabelthm:unimportant
This is another theorem
endtheorem

sectionSecond test

begintheoremlabelthm:soandso
Again a theorem.
endtheorem

enddocument






share|improve this answer














share|improve this answer



share|improve this answer








edited 4 mins ago

























answered Feb 9 at 18:56









egregegreg

728k8819243235




728k8819243235












  • I just found out that the theorem numbering gets messed up when the theorems belong to different chapters. I just added a MWE showing the problem to the original question. Would it help to divide the theorems printed into chapters, so that printtheorems only prints the theorems relative to the current chapter? That would be the desired behaviour anyways, so if it also fixes the numbering problem it'd kill two birds with one stone.

    – noibe
    Feb 10 at 16:54












  • I'm sorry to bother you again, I may have found another inconvenience. When the theorems are numbered within a chapter, nullifying the label isn't effective and hyperref links (in general) to the wrong page. I tried removing the string label (if present) from line 52, so that it won't be written as part of the body in the .thl, but without ay success. MWE provided in the original post.

    – noibe
    Feb 17 at 0:09


















  • I just found out that the theorem numbering gets messed up when the theorems belong to different chapters. I just added a MWE showing the problem to the original question. Would it help to divide the theorems printed into chapters, so that printtheorems only prints the theorems relative to the current chapter? That would be the desired behaviour anyways, so if it also fixes the numbering problem it'd kill two birds with one stone.

    – noibe
    Feb 10 at 16:54












  • I'm sorry to bother you again, I may have found another inconvenience. When the theorems are numbered within a chapter, nullifying the label isn't effective and hyperref links (in general) to the wrong page. I tried removing the string label (if present) from line 52, so that it won't be written as part of the body in the .thl, but without ay success. MWE provided in the original post.

    – noibe
    Feb 17 at 0:09

















I just found out that the theorem numbering gets messed up when the theorems belong to different chapters. I just added a MWE showing the problem to the original question. Would it help to divide the theorems printed into chapters, so that printtheorems only prints the theorems relative to the current chapter? That would be the desired behaviour anyways, so if it also fixes the numbering problem it'd kill two birds with one stone.

– noibe
Feb 10 at 16:54






I just found out that the theorem numbering gets messed up when the theorems belong to different chapters. I just added a MWE showing the problem to the original question. Would it help to divide the theorems printed into chapters, so that printtheorems only prints the theorems relative to the current chapter? That would be the desired behaviour anyways, so if it also fixes the numbering problem it'd kill two birds with one stone.

– noibe
Feb 10 at 16:54














I'm sorry to bother you again, I may have found another inconvenience. When the theorems are numbered within a chapter, nullifying the label isn't effective and hyperref links (in general) to the wrong page. I tried removing the string label (if present) from line 52, so that it won't be written as part of the body in the .thl, but without ay success. MWE provided in the original post.

– noibe
Feb 17 at 0:09






I'm sorry to bother you again, I may have found another inconvenience. When the theorems are numbered within a chapter, nullifying the label isn't effective and hyperref links (in general) to the wrong page. I tried removing the string label (if present) from line 52, so that it won't be written as part of the body in the .thl, but without ay success. MWE provided in the original post.

– noibe
Feb 17 at 0:09


















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%2f474089%2fautomatic-theorem-list%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

Isabella Eugénie Boyer Biographie | Références | Menu de navigationmodifiermodifier le codeComparator to Compute the Relative Value of a U.S. Dollar Amount – 1774 to Present.

Join wedge with single bond in chemfigHow to make only one part of double bond bold with chemfig?Crossing bonds in chemfigjoining atoms in chemfig. Two adjacent molculesHow do I selectively change bond length in chemfig?Ugly bond joints in chemfigchemfig: reaction above arrowUsing the mhchem and chemfig packages in conjunctionBonding to specific element letter using chemfigResonance hybrids in chemfigScale chemfig molecule in beamer with tikzWhy does this chemfig bond with a hook start in the middle of the atom?

Are small insurances worth itIs insurance worth it if you can afford to replace the item? If not, when is it?Is accident insurance worth it for my kids who play sportsIs insuring property for more than it is worth allowed?At what point does it become worth it to file an insurance claim?Are wage loss insurance programs worth the cost compared to having an emergency fund?When is an event worth insuring against?Is insurance worth it if you can afford to replace the item? If not, when is it?FHA loan just commenced : Any way to get any of the up-front mortgage insurance back?Which types of insurances do I need to buy?Should I carry less renter's insurance if I can self-insure?Mortgage Adviser Signed Me Up For Multiple Home and Life Insurances (UK)Why many travel insurances don't cover country of nationality?