5

Suppose that you have a bunch of theorems, propositions, definitions and lemmas, like

Definition 1. A group is said to be …
Lemma 2. Let … Then …
Proof 2. […]
Theorem 3. If … then …
Proof 3. […]

What I would like to do is to have a command like \allResults (or several like \allTheorems, \allDefs, \allLemmas) that returns at the end of the document the statements (without proofs) of the definitions, theorems, lemmas (in the correct order), like this:

Definition 1. A group is said to be …
Lemma 2. Let … Then …
Theorem 3. If … then …


What I tried is to follow the answers to this question. I didn't understand egreg's first code. I liked Andrew's code very much, but I wasn't able to change it for my situation.

Therefore I tried egreg's second code, by creating \allprops and \alltheoremes, but I got errors (see below), as if I created twice something... When I only create \allprops (without \alltheoremes), it works well.

\documentclass[a4paper]{article}
\usepackage{thmtools}
\usepackage{thm-restate}
\usepackage{xparse}

\declaretheorem[
  name=Proposition,
  numberwithin=section,
]{thmprop}

\ExplSyntaxOn
\NewDocumentEnvironment{prop}{o}
 {
  \int_gincr:N \g_vedran_prop_int
  \IfNoValueTF{#1}
   {
    \vedran_restatable:x { \int_to_Alph:n { \g_vedran_prop_int } }
   }
   {
    \vedran_restatable:nx { #1 } { \int_to_Alph:n { \g_vedran_prop_int }     }
   }
 }
 {
  \endrestatable
  \seq_gput_right:Nx \g_vedran_prop_seq
   {
    prop \int_to_Alph:n { \g_vedran_prop_int }
   }
 }

\NewDocumentCommand{\allprops}{}
 {
  \seq_map_inline:Nn \g_vedran_prop_seq
   {
    \use:c { ##1 } *
   }
 }

\int_new:N \g_vedran_prop_int
\seq_new:N \g_vedran_prop_seq

\cs_new_protected:Npn \vedran_restatable:n #1
 {
  \restatable{thmprop}{prop#1}
 }
\cs_generate_variant:Nn \vedran_restatable:n { x }
\cs_new_protected:Npn \vedran_restatable:nn #1 #2
 {
  \restatable[#1]{thmprop}{prop#2}
 }
\cs_generate_variant:Nn \vedran_restatable:nn { nx }
\ExplSyntaxOff


\declaretheorem[
  name=Theoreme,
  numberwithin=section,
]{thmtheoreme}

\ExplSyntaxOn
\NewDocumentEnvironment{theoreme}{o}
 {
  \int_gincr:N \g_vedran_theoreme_int
  \IfNoValueTF{#1}
   {
    \vedran_restatable:x { \int_to_Alph:n { \g_vedran_theoreme_int } }
   }
   {
    \vedran_restatable:nx { #1 } { \int_to_Alph:n { \g_vedran_theoreme_int } }
   }
 }
 {
  \endrestatable
  \seq_gput_right:Nx \g_vedran_theoreme_seq
   {
    theoreme \int_to_Alph:n { \g_vedran_theoreme_int }
   }
 }

\NewDocumentCommand{\alltheoremes}{}
 {
  \seq_map_inline:Nn \g_vedran_theoreme_seq
   {
    \use:c { ##1 } *
   }
 }

\int_new:N \g_vedran_theoreme_int
\seq_new:N \g_vedran_theoreme_seq

\cs_new_protected:Npn \vedran_restatable:n #1
 {
  \restatable{thmtheoreme}{theoreme#1}
 }
\cs_generate_variant:Nn \vedran_restatable:n { x }
\cs_new_protected:Npn \vedran_restatable:nn #1 #2
 {
  \restatable[#1]{thmtheoreme}{theoreme#2}
 }
\cs_generate_variant:Nn \vedran_restatable:nn { nx }
\ExplSyntaxOff

\begin{document}
\section{Originals}
Some text.

\begin{prop}[Euler]
This is the first prop.
\end{prop}

Some text.

\begin{prop}
This is the second prop.
\end{prop}

Some text.

\section{All together}
\allprops

\end{document}

I get errors like

LaTeX error: "kernel/command-already-defined"
!
! Control sequence \vedran_restatable:n already defined.

My question is: how is it possible to create commands like \allResults (or several like \allTheorems, \allDefs, \allLemmas), either by changing my code, or by using a new and better idea (e.g. egreg's or Andrew's answers here)?

Thank you very much!

  • 1
    You have copied the definitions of \vedran_restatable more than once – user31729 Dec 28 '16 at 13:05
  • @ChristianHupfer : thank you for your comment! Does it mean that I can't use some kind of "restatable" to solve my problem (since I have several environments Theorem, Definition, Proposition, Lemma, to deal with)? – Watson Dec 28 '16 at 13:08
  • Basically, as is setup by egreg you need a ....restatable macro for each theorem type. This can be done in loop, most likely, but requires more work to do. (I am short of time, right now) – user31729 Dec 28 '16 at 13:11
5

The reason is that (as it is defined right now) there must be an individual \vedran_...restatable macro for each of the theorem-like environment.

I am short of time right now, but I'll try to provide a more sophisticated approach for more than just two environments.

result

\documentclass[a4paper]{article}

\usepackage{thmtools}
\usepackage{thm-restate}
\usepackage{xparse}

\declaretheorem[
  name=Proposition,
  numberwithin=section,
]{thmprop}

\ExplSyntaxOn
\NewDocumentEnvironment{prop}{o}
 {
  \int_gincr:N \g_vedran_prop_int
  \IfNoValueTF{#1}
   {
    \vedran_restatable:x { \int_to_Alph:n { \g_vedran_prop_int } }
   }
   {
    \vedran_restatable:nx { #1 } { \int_to_Alph:n { \g_vedran_prop_int } }
   }
 }
 {
  \endrestatable
  \seq_gput_right:Nx \g_vedran_prop_seq
   {
    prop \int_to_Alph:n { \g_vedran_prop_int }
   }
 }

\NewDocumentCommand{\allprops}{}
 {
  \seq_map_inline:Nn \g_vedran_prop_seq
   {
    \use:c { ##1 } *
   }
 }

\int_new:N \g_vedran_prop_int
\seq_new:N \g_vedran_prop_seq

\cs_new_protected:Npn \vedran_restatable:n #1
 {
  \restatable{thmprop}{prop#1}
 }
\cs_generate_variant:Nn \vedran_restatable:n { x }
\cs_new_protected:Npn \vedran_restatable:nn #1 #2
 {
  \restatable[#1]{thmprop}{prop#2}
 }
\cs_generate_variant:Nn \vedran_restatable:nn { nx }
\ExplSyntaxOff







\declaretheorem[
  name=Theoreme,
  numberwithin=section,
]{thmtheoreme}

\ExplSyntaxOn
\NewDocumentEnvironment{theoreme}{o}
 {
  \int_gincr:N \g_vedran_theoreme_int
  \IfNoValueTF{#1}
   {
    \vedran_theo_restatable:x { \int_to_Alph:n { \g_vedran_theoreme_int } }
   }
   {
    \vedran_theo_restatable:nx { #1 } { \int_to_Alph:n { \g_vedran_theoreme_int } }
   }
 }
 {
  \endrestatable
  \seq_gput_right:Nx \g_vedran_theoreme_seq
   {
    theoreme \int_to_Alph:n { \g_vedran_theoreme_int }
   }
 }

\NewDocumentCommand{\alltheoremes}{}
 {
  \seq_map_inline:Nn \g_vedran_theoreme_seq
   {
    \use:c { ##1 } *
   }
 }

\int_new:N \g_vedran_theoreme_int
\seq_new:N \g_vedran_theoreme_seq

\cs_new_protected:Npn \vedran_theo_restatable:n #1
 {
  \restatable{thmtheoreme}{theoreme#1}
 }
\cs_generate_variant:Nn \vedran_theo_restatable:n { x }
\cs_new_protected:Npn \vedran_theo_restatable:nn #1 #2
 {
  \restatable[#1]{thmtheoreme}{theoreme#2}
 }
\cs_generate_variant:Nn \vedran_theo_restatable:nn { nx }
\ExplSyntaxOff







\begin{document}

\section{Originals}

Some text.

\begin{prop}[Euler]
This is the first prop.
\end{prop}

Some text.

\begin{prop}
This is the second prop.
\end{prop}

Some text.

\begin{theoreme}
foo foo
\end{theoreme}

\section{All together}

\alltheoremes

\allprops

\end{document}

Update

Generic code generation with \AddDeclaredTheorems{...} will produce all relevant macros and environments on the fly.

\documentclass[a4paper]{article}

\usepackage{thmtools}
\usepackage{thm-restate}
\usepackage{xparse}

\declaretheorem[
  name=Proposition,
  numberwithin=section,
]{thmprop}

\declaretheorem[
  name=Theoreme,
  numberwithin=section,
]{thmtheoreme}

\declaretheorem[
  name=Example,
  numberwithin=section,
]{thmexample}


\ExplSyntaxOn


\seq_new:N \g_vedran_declaredtheorem_seq

\cs_generate_variant:Nn \cs_generate_variant:Nn { cn } % Does this work????
\cs_generate_variant:Nn \int_to_Alph:n {c}

\NewDocumentCommand{\AddDeclaredTheorems}{m}{%
  \clist_set:Nn \l_tmpa_clist {#1}
  % Loop over elements in the list and add them to the list of declared theorems
  \clist_map_inline:Nn \l_tmpa_clist {
    \seq_gput_right:Nn \g_vedran_declaredtheorem_seq {##1}
  }
  \seq_gremove_duplicates:N \g_vedran_declaredtheorem_seq
  \seq_map_inline:Nn \g_vedran_declaredtheorem_seq {%
    \int_if_exist:cF { g_vedran_##1_int } {
      \int_new:c {g_vedran_##1_int}
    }
    \seq_if_exist:cTF { g_vedran_##1_seq }{%
      \seq_gclear:c { g_vedran_##1_seq }
    }{% No, does not exist, define it
      \seq_new:c { g_vedran_##1_seq }
    }
    \vedran_generate_restatable_noopt:n {##1}
    \vedran_generate_restatable_opt:n {##1}
    %Build the variants
    \cs_generate_variant:cn { vedran_##1_restatable:n } { x }
    \cs_generate_variant:cn { vedran_##1_restatable:nn } { nx }
    \vedran_generate_wrapper_env:n {##1}
    \vedran_generate_allthingies:n {##1}
  }% End of \seq_map_inline
}

\cs_new:Nn \vedran_generate_restatable_noopt:n {%
  \cs_if_exist:cF { vedran_#1_restatable:n } {
    \cs_new_protected:cn {vedran_#1_restatable:n}{%
      \typeout{Using vedran_#1_restatable:x}%
      \restatable{thm#1}{#1##1}
    }
  }
}

\cs_new:Nn \vedran_generate_restatable_opt:n {
  \cs_if_exist:cF { vedran_#1_restatable:nn } {
    \cs_new_protected:cn {vedran_#1_restatable:nn }{%
      \restatable[##1]{thm#1}{#1##2}
    }
  }
}


\cs_new:Nn \vedran_generate_wrapper_env:n {%
  \cs_if_exist:cF {#1} {
    \NewDocumentEnvironment{#1}{o}
    {
      \int_gincr:c { g_vedran_#1_int }
      \IfNoValueTF{##1}
      {
        \use:c{vedran_#1_restatable:x} { \int_to_Alph:c { g_vedran_#1_int } }
      }
      {
        \use:c{vedran_#1_restatable:nx} { ##1 } { \int_to_Alph:c { g_vedran_#1_int } }
      }
    }
    {
      \endrestatable
      \seq_gput_right:cx  {g_vedran_#1_seq} {#1\int_to_Alph:c { g_vedran_#1_int }}
    }
  }
}

\cs_new:Nn \vedran_generate_allthingies:n {
  \cs_new:cpn {all#1s} 
  {
    \seq_map_inline:cn {g_vedran_#1_seq}
    {
      \use:c { ####1 } *
    }
  }
}

\ExplSyntaxOff

\begin{document}

\AddDeclaredTheorems{prop,theoreme,example}

\section{Originals}

Some text.

\begin{prop}[Euler]
This is the first prop.
\end{prop}

\begin{example}
  $E=mc^2$
\end{example}

Some text.

\begin{prop}
This is the second prop.
\end{prop}

Some text.

\begin{theoreme}
foo foo
\end{theoreme}

\section{All together}

\alltheoremes

\allprops

\allexamples

\end{document}

**Next Update -- with \AllThingiesInOrderOfAppearance command

\documentclass[a4paper]{article}

\usepackage{thmtools}
\usepackage{thm-restate}
\usepackage{xparse}

\declaretheorem[
  name=Proposition,
  numberwithin=section,
]{thmprop}

\declaretheorem[
  name=Theoreme,
  numberwithin=section,
]{thmtheoreme}

\declaretheorem[
  name=Example,
  numberwithin=section,
]{thmexample}


\ExplSyntaxOn


\seq_new:N \g_vedran_declaredtheorem_seq

\seq_new:N \g_vedran_ordered_seq 

\cs_generate_variant:Nn \cs_generate_variant:Nn { cn } % Does this work????
\cs_generate_variant:Nn \int_to_Alph:n {c}

\NewDocumentCommand{\AddDeclaredTheorems}{m}{%
  \clist_set:Nn \l_tmpa_clist {#1}
  % Loop over elements in the list and add them to the list of declared theorems
  \clist_map_inline:Nn \l_tmpa_clist {
    \seq_gput_right:Nn \g_vedran_declaredtheorem_seq {##1}
  }
  \seq_gremove_duplicates:N \g_vedran_declaredtheorem_seq
  \seq_map_inline:Nn \g_vedran_declaredtheorem_seq {%
    \int_if_exist:cF { g_vedran_##1_int } {
      \int_new:c {g_vedran_##1_int}
    }
    \seq_if_exist:cTF { g_vedran_##1_seq }{%
      \seq_gclear:c { g_vedran_##1_seq }
    }{% No, does not exist, define it
      \seq_new:c { g_vedran_##1_seq }
    }
    \vedran_generate_restatable_noopt:n {##1}
    \vedran_generate_restatable_opt:n {##1}
    %Build the variants
    \cs_generate_variant:cn { vedran_##1_restatable:n } { x }
    \cs_generate_variant:cn { vedran_##1_restatable:nn } { nx }
    \vedran_generate_wrapper_env:n {##1}
    \vedran_generate_allthingies:n {##1}
  }% End of \seq_map_inline
}

\cs_new:Nn \vedran_generate_restatable_noopt:n {%
  \cs_if_exist:cF { vedran_#1_restatable:n } {
    \cs_new_protected:cn {vedran_#1_restatable:n}{%
      \restatable{thm#1}{#1##1}
    }
  }
}

\cs_new:Nn \vedran_generate_restatable_opt:n {
  \cs_if_exist:cF { vedran_#1_restatable:nn } {
    \cs_new_protected:cn {vedran_#1_restatable:nn }{%
      \restatable[##1]{thm#1}{#1##2}
    }
  }
}


\cs_new:Nn \vedran_generate_wrapper_env:n {%
  \cs_if_exist:cF {#1} {
    \NewDocumentEnvironment{#1}{o}
    {
      \int_gincr:c { g_vedran_#1_int }
      \IfNoValueTF{##1}
      {
        \use:c{vedran_#1_restatable:x} { \int_to_Alph:c { g_vedran_#1_int } }
      }
      {
        \use:c{vedran_#1_restatable:nx} { ##1 } { \int_to_Alph:c { g_vedran_#1_int } }
      }
    }
    {
      \endrestatable
      \seq_gput_right:cx  {g_vedran_#1_seq} {#1\int_to_Alph:c { g_vedran_#1_int }}
      \seq_gput_right:Nx  \g_vedran_ordered_seq {#1\int_to_Alph:c { g_vedran_#1_int }}
    }
  }
}

\NewDocumentCommand{\AllThingiesInOrderOfAppearance}{}{%
  \seq_map_inline:Nn \g_vedran_ordered_seq {%
    \use:c {##1} *
  }
}

\cs_new:Nn \vedran_generate_allthingies:n {
  \cs_new:cpn {all#1s} 
  {
    \seq_map_inline:cn {g_vedran_#1_seq}
    {
      \use:c { ####1 } *
    }
  }
}

\ExplSyntaxOff

\begin{document}

\AddDeclaredTheorems{prop,theoreme,example}

\section{Originals}

Some text.

\begin{prop}[Euler]
This is the first prop.
\end{prop}

\begin{example}
  $E=mc^2$
\end{example}

Some text.

\begin{prop}
This is the second prop.
\end{prop}

Some text.

\begin{theoreme}
foo foo
\end{theoreme}

\section{All together}

% All in order
\AllThingiesInOrderOfAppearance


% Individual ones in a row

Now individual groups:

\alltheoremes

\allprops

\allexamples

\end{document}

enter image description here

  • Thank you very much! It will be very useful, I think. However, we get here first all the theorems, and then all the propositions. Do you know if it is possible to create some kind of \allResults which gives the theorems and propositions in the same order as in the original section? – Watson Dec 28 '16 at 13:12
  • 1
    @Watson: Should be possible, again, later on... – user31729 Dec 28 '16 at 13:12
  • Is it possible to have in the example above Theorem 1.3 instead of Theorem 1.1. (using \usepackage{amsthm}and \theoremstyle{plain} makes no sense here)? Many thanks! – Watson Dec 29 '16 at 23:27
  • 1
    @Watson: I'll try during the next days. I had very busy days... – user31729 Jan 24 '17 at 22:11
  • 1
    @Watson: Well... yes... I will update later on (no, not today ;-)) with a better checking whether the wrapper environment has been defined already. I am a little bit unsatisfied with the way I've done it yet (it's quick and dirty ;-)) – user31729 Jan 28 '17 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.