如何自动重复所有的定理、命题、定义?

如何自动重复所有的定理、命题、定义?

假设你有一堆定理、命题、定义和引理,比如

定义 1. 一个群被称为是…
引理 2. 设…则…
证明 2. […]
定理 3. 如果…则…
证明 3. […]

我想要做的是有一个像这样的命令\allResults(或几个类似的命令\allTheorems,在文档末尾返回定义、定理、引理(按正确的顺序)的陈述(不带证明),如下所示\allDefs\allLemmas)

定义 1. 一个群被称为是…
引理 2. 设…则…
定理 3. 如果…则…


我尝试按照以下答案进行操作问题。我不明白 egreg 的第一个代码。我非常喜欢 Andrew 的代码,但我无法根据自己的情况对其进行更改。

因此,我尝试了 egreg 的第二段代码,通过创建\allprops\alltheoremes,但出现了错误(见下文),就好像我创建了两次一样......当我只创建\allprops(没有\alltheoremes)时,它运行良好。

\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}

我收到类似这样的错误

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

我的问题是:如何创建类似\allResults(或多个类似\allTheorems,,\allDefs\allLemmas通过更改我的代码,或者使用新的更好的想法(例如 egreg 或 Andrew 的答案)这里)?

非常感谢!

答案1

原因是(正如现在定义的)\vedran_...restatable每个类定理环境都必须有一个单独的宏。

我现在时间不够,但我会尝试为两种以上的环境提供更为复杂的方法。

结果

\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}

更新

通用代码生成\AddDeclaredTheorems{...}将动态生成所有相关的宏和环境。

\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}

**下次更新--使用\AllThingiesInOrderOfAppearance命令

\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}

在此处输入图片描述

相关内容