Problems related to custom AMS theorem module

Rudi Gaelzer rgaelzer at
Fri Jul 23 16:14:20 UTC 2021

This question is still related to the recent thread:[1]

I thought I got it right, but several unexpected problems came up and I'd like to have your 

What I want to do is the following: I'm preparing a series of lectures that will employ 
theorem-like environments.
Several lectures are related to the same subject and so I want the enumeration of 
numbered things (equations, figures, theorems, examples, definitions, etc) in subsequent 
lectures to continue from the previous ones.

In order to guarantee the correct sequence, I'm loading in the preamble the totcount 
package.  For each counter employed in a given lecture, the totcount package  creates a 
new counter (equation at totc, figure at totc, etc), which contains the last value of the 
The values of these counters are then written in a specific file (call it, which 
can be read in the subsequent lecture.  Finally, by using 
\setcounter{<counter>}{<last value>}
I can guarantee the desired continuation of the enumeration of things.

For the native LaTeX counters (equation, figure, etc), I've been including all the necessary 
commands in an ERT with the layout InPreamble.
For the native counters, this works all right because the counters are defined with the 
class and class packages, which are read before user-specific definitions incuded with the 
InPreamble layout.

However, for the theorem-like environments, the implementation is more involved.  The 
problems that are coming up are:

1. Say a given environment like theorem is only introduced from the second lecture.  In 
this case, the file will not contain the last value of the counter thm at totc, 
because there was no theorem in the first lecture.  Hence, I cannot issue a 
\setcounter{thm}{<last value>} because in this case the number of the first theorem gets 
increased for each LyX/LaTeX compilation.
This complicates the implementation of the custom theorem-bytpe module I've been 
tinkering with in my previous thread.  Somehow I need to introduce instructions to read 
the file and find out if the counter thm was previously employed.  Only if there 
is a last value of thm at totc that I can \setvalue the counter.  
I'm not sure what will be the best way to implement this.  Several posts I've been browsing 
through suggest the use of TeX natives like \newread, \openin \read, etc, and then use 
some package (like xstring) that identifies specific strings in each line read.  Seems too 
much trouble for me.

2. Then I decided to dumb down my approach.  I will only use \setvalue for those counters 
that I know are employed in the previous lecture, as I was doing so far with the native 
LaTeX counters.
The problem now is that with the layout InPreamble, those instructions will be written 
before the ams-theorem module defines the thm counters.  That is going to give errors.
It seems that as of now there is no way to choose the ordering of the instructions 
contained in the layout InPreamble inside the document's preamble.  This is discussed in 
this thread:[2]

Therefore, the only solution that I can imagine is to write the necessary instructions in the 
user-defined preamble accessible in 
Document Settings -> LaTeX Preamble
This way, I guarantee the correct ordering of the instructions.

Anyway, I just wanted to vent out my frustration that I was not smart enough to somehow 
automatize this process.  This is an admittedly very specific problem, and I don't know if 
you think this can be of interest to the larger community.

Rudi Gaelzer
Institute of Physics
Federal University of Rio Grande do Sul
Registered linux user # 153741

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the lyx-users mailing list