1

I took the code from this thread and modified it a little to fit my personal needs. Everything works fine until I add a second theorem environment, which in the MWE I'm posting is the lemma environment.

I've been trying to fix this for quite sometime but without success. Here's the code:

\documentclass{book}
\usepackage{amsthm, chngcntr, environ, etoolbox, hyperref, showkeys, xparse}
% ---------------------------
\hypersetup{colorlinks, linkcolor=[RGB]{117, 17, 17}, citecolor=[RGB]{0, 0, 0}}
%
\ExplSyntaxOn
%
\NewDocumentCommand{\newtheoremx}{momo}
{
  \IfValueTF{#2}
  {
    \newtheorem{#1@inner}[#2@inner]{#3}
    \theoremstyle{linked}
    \newtheorem{#1@innerlinked}[#2@inner]{#3}
    \prop_gput:Nnn \g_riccardo_theorems_prop { #1 } { #2 }
  }
  {
    \IfValueTF{#4}
    {
      \newtheorem{#1@inner}{#3}[#4]
      \theoremstyle{linked}
      \newtheorem{#1@innerlinked}{#3}[#4]
    }
    {
      \newtheorem{#1@inner}{#3}
      \theoremstyle{linked}
      \newtheorem{#1@innerlinked}{#3}
    }
    \prop_gput:Nnn \g_riccardo_theorems_prop { #1 } { #1 }
    \seq_gput_right:Nn \g_riccardo_theorems_counters_seq { #1 }
  }
  \NewEnviron{#1}[1][]
  {
    \tl_if_empty:nTF { ##1 }
    { \begin{#1@inner} }
    { \begin{#1@inner}[##1] }
    \label{#3\thechapter.\arabic{#1@inner}}
    \tl_gset:Nx \g__riccardo_theorems_number_tl { \use:c {@currentlabel} }
    \BODY
    \end{#1@inner}
    \seq_gput_right:Nx \g_riccardo_theorems_seq
    {
      { #1 }
      { \g__riccardo_theorems_number_tl }
      { \exp_not:n { ##1 } }
      { \exp_not:V \BODY }
      { \thechapter }
    }
  }
  \AtBeginEnvironment{#1}{\ifnum\arabic{#1@inner}=0 \begin{noeqnumber}\printtheorems\end{noeqnumber} \fi\clearpage}
  \counterwithin{equation}{#1@inner}
  \counterwithin{footnote}{#1@inner}
  \theoremstyle{mytheorem}
}
%
\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

\NewDocumentCommand{\printtheorems}{}
 {
  \group_begin:
  \cs_set_eq:NN \label \use_none:n
  \file_if_exist_input:n { \c_sys_jobname_str.thm }
  \group_end:
  \seq_map_inline:Nn \g_riccardo_theorems_counters_seq
   {
    \setcounter{##1@inner}{0}
   }
   \newpage
 }
%
\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.thm }
  \riccardo_theorems_save:
  \iow_close:N \g_riccardo_theorems_stream
 }
%
\NewDocumentCommand{\savedtheorem}{mmmmm}
{
  \cs_set:cpn { the \prop_item:Nn \g_riccardo_theorems_prop {#1} @innerlinked } { #2 }
  \int_compare:nNnT {\thechapter} = {#5} {
    \tl_if_empty:nTF { #3 } 
    {
      \begin{#1@innerlinked} #4 \end{#1@innerlinked}
    }
    {
      \begin{#1@innerlinked}[#3] #4 \end{#1@innerlinked}
    }
  }  
} 
%
\ExplSyntaxOff
% ---------------------------
\newtheoremstyle{linked}{0pt}{10pt}{\itshape}{}{\bfseries}{\space}{0pt}{\hyperref[\thmname{#1}\thmnumber{#2}]{\thmname{#1}~\thmnumber{#2}}~\thmnote{\normalfont(#3)}}
\newtheoremstyle{mytheorem}{0pt}{10pt}{\itshape}{}{\bfseries}{\space}{0pt}{\thmname{#1}~\thmnumber{#2}~\thmnote{\normalfont(#3)}}
\theoremstyle{mytheorem}
%
\newtheoremx{thm}{Theorem}[chapter]
\newtheoremx{lemma}[thm]{Lemma}
%
\renewenvironment{proof}{\pushQED{\qed}\trivlist\item[\hskip\labelsep\hspace{7pt}\textit{Proof.}]\mbox{}\vspace{2pt}\\}{\popQED}
\newenvironment{noeqnumber}{\renewenvironment{equation}{\[}{\]}}{}
%
\counterwithout{equation}{chapter}
\counterwithout{footnote}{chapter}
% --------------------------------
\begin{document}
%
\chapter{A test chapter}
\begin{thm}
A very important theorem è.
\end{thm}

\begin{lemma}
A very important lemma.
\end{lemma}

\begin{thm}
Another theorem.
\end{thm}
%
\end{document}

I think the main problem is that lemma@inner doesn't get recognised as a counter for the lemma environment, which causes the code to fail.

noibe
  • 2,084
  • You seem to be using the code from the question rather than the code from the solution, which could well be your problem. As the code in the accepted answer already correctly deals with multiple lemmas and theorems wouldn't it be a better starting point? –  Apr 28 '19 at 02:42
  • I tried using both versions of the code posted in the solution, but neither of them compile. – noibe Apr 28 '19 at 12:19

0 Answers0