I am writing a short report with OpTeX, and I have noticed a troublesome behavior which I cannot easily change: each section resets the counter for displayed equations. For this reason I have several equations numbered (1), (2), (3) in the same document, and it is unduly difficult to reference them correctly. A minimal example, to be compiled with the optex command, follows.

Is it possible to keep an incremental counter for all displayed equations? I know that the default behavior is written in the documentation, but writing long papers in this way is a nightmare (in my opinion).

\sec One

$$ a+b=c \eqmark $$

\sec Two

$$ d+e=f \eqmark $$

  • As always it might probably be best to provide a full but minimal example that others can copy and test as is. Especially as there probably aren't super many users who are familiar wih optex.
    – daleif
    Commented Dec 14, 2023 at 13:48

1 Answer 1


You need to prevent \_secx (ran at the start of every section) from resetting \_dnum (the equation number counter). The easiest way of doing this is to copy the original OpTeX \_secx but remove the \_dnum=0; the more general solution is to remove \_dnum=0 from whatever \_secx currently is.

\margins/1 a5 (2,2,1,1)in


\sec One
$$ a = 1 \eqmark $$

\sec Two
$$ b = 2 \eqmark $$

% % Option 1
    token.set_macro("_secx", token.get_macro("_secx"):gsub("._dnum =0", ""))

% % Option 2
% \_def\_secx{\_seccx \_seccnum=0 \_tnum=0 \_fnum=0 \_resetABCDE}

\sec Three
$$ c = 3 \eqmark $$

\sec Four
$$ d = 4 \eqmark $$



  • 5
    Really nice trick for patching commands! I've never thought about using lua pattern matching for that.
    – Udi Fogiel
    Commented Dec 14, 2023 at 14:13
  • Very nice answer, thank you!
    – Siminore
    Commented Dec 14, 2023 at 14:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .