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

Does Doodling or Improvising on the Piano Have Any Benefits?

Redundant comparison & "if" before assignment

Loading commands from file

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

What does routing an IP address mean?

Strong empirical falsification of quantum mechanics based on vacuum energy density

Yosemite Fire Rings - What to Expect?

Why electric field inside a cavity of a non-conducting sphere not zero?

Why is it that I can sometimes guess the next note?

Symbol used to indicate indivisibility

Terse Method to Swap Lowest for Highest?

How much character growth crosses the line into breaking the character

Multiplicative persistence

Closed-form expression for certain product

Biological Blimps: Propulsion

Rising and falling intonation

Count the occurrence of each unique word in the file

Non-trope happy ending?

Is it better practice to read straight from sheet music rather than memorize it?

Basic combinatorial probability problem

Why should universal income be universal?

Added a new user on Ubuntu, set password not working?

The screen of my macbook suddenly broken down how can I do to recover

Pre-mixing cryogenic fuels and using only one fuel tank



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 5 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

acmart: Multiple authors: all with same affiliation, one author an additional affiliationHow to Write Names of Multiple Authors with Shared Affiliation in ACM 2017 Template?Multiple authors with different primary affiliation, but same additional affiliationSame affiliation for all authors without extra packagesIOS-Book-Article.cls: one author with multiple affiliationacmart: Shared Author AffiliationMultiple authors with different primary affiliation, but same additional affiliationAuthor affiliation with only 1 authorAdding Multiple Authors with Different Affiliation in LaTeX ArticleLaTeX: Multiple authors stays on same lineHow to Label Multiple Authors with Same DescriptionHow to make two authors use the same affiliationTwo authors with same affiliation on finished front page

How to write “ä” and other umlauts and accented letters in bibliography?Accents in BibTeXSorting references with special characters alphabeticallyUse ae ligature in bibliographyEastern European nameInverted circumflex in BibTexBibTex, non-ascii initials and nameptr fproblems with accent in LatexHow to add a Ø to my bibliography from Jabref?References without accentsTroubles when trying to cite St“omer-Verlet in ”title" field of a bib entryComprehensive list of accented charactersHow to type the letter “i” with two dots (diaeresis) in math mode?Problem with glossary text and accented lettersSpecial character in bibliographyAccented letters, Unicode and LaTeX accentsHow to stop natbib from modifying bibliography styleCitation of a paper with non-standard characters by BibtexWrite accented characters to file using writeHow to group the bibliography alphabetically, if some surnames start with “accented” characters?How can I automatically capitalize significant words in my bibliography?

How to force a table into page width?How to make table spanning textwidthMaking table width fit into text widthTable with tabular, column type p grabbing available spaceMaking widths of tables equal to width of textblocklatex tabular width the same as the textwidthMake tabular span textwidth exactlySet width of table to be the whole line or a given portion of itIs the tabu package obsolete?What is the difference between tabular, tabular* and tabularx environments?How can i set the width of a table?Making table width fit into text widthTabular and grid typesettingMake table use page widthTable exceeds page widthtable width automatically to page widthHow to shrink table to page widthForce table column to be zero widthTable width exceeds the page widthTable extending beyond the page widthforce longtable to text width