1

I'd like to know how packages like cleveref actually work. As far as I know, \ref gives back the value of the counter that was incremented through \refstepcounter. However, it can only give the value of the counter. Nothing about the name or other info about the environment is known. So I'm wondering how cleveref works, and I'd like to create a reference command with similar functions myself.

Mico
  • 506,678
OrthoPole
  • 113
  • To learn how a software package's code really works, nothing beats studying the code itself. What have you done so far? – Mico Mar 24 '23 at 06:36
  • Reading TeX code is quite difficult though, start with package writing - Where do I start LaTeX programming? - TeX - LaTeX Stack Exchange if you want. In this case if you want to write some TeX code you would actually need to learn anyway (so answers would need to explain how the TeX code work instead of just giving a high level architectural design overview), which would be a massive effort and I think a bit too much to ask of people. – user202729 Mar 24 '23 at 06:49
  • (for reference the source code file cleveref.dtx is >10000 lines. Also you can compile it to get a documentation of the implementation which may give some insight.) – user202729 Mar 24 '23 at 06:52
  • I was assuming that the OP has a reasonably solid understanding of the fundamentals of TeX and LaTeX programming. Please advise if this assumption is invalid. – Mico Mar 24 '23 at 07:27
  • I suggest you find the file cleveref.dtx in your TeX distribution and compile it twice in LaTeX. The output file, cleveref.pdf, will contain a new section, numbered "16 Implementation". This section, more than 200 pages long [!], contains all implementation details of the package. Please let us know if you have questions about some of the material in section 16. – Mico Mar 24 '23 at 09:53
  • However, this kind of packages usually work by redefining the LaTeX internals, in this case cleveref redefines \label to add two lines to the aux file, one for use with \ref and one with information about the type for use with \cref. This is possible because LaTeX has only a very limited concept of scope or namespaces, so each package can redefine commands from other packages, built-in commands or user commands without any issue. – Marijn Mar 25 '23 at 15:48
  • Actually, it seems that my knowledge about latex programming is very limited. In fact, I want to build a system to manage the theorem environments myself, but I wasn't aware of the difficulties of realizing this function. I will learn more about latex programming. Thanks a lot. – OrthoPole Mar 29 '23 at 06:20

0 Answers0