home
themmj.dev

latex-demos

Repo: latex-demos

intro

Sometimes you just want to find a solution for something that isn't really even problem you are having. Mostly just to have an excuse to try something fun.

In this case, the basic thought was "Wouldn't it be nice to have some form of context in a LaTex document which can expand as you go along?" This could be used to track established concepts in a paper you are writing, or world building details known to the reader of a novel. Maybe even character traits as the character develops. That way you can check what has been established. With the addition of some form of assertion that can event prevent accidentally relying on a concept or world detail that has not been established yet. Even catching these cases when restructuring.

requirements

building blocks

LaTex has variables, or rather control sequences. That's a good start. Control sequences can be defined on the fly with \csname and \endcsname. Declaring multiple control sequences which only differ in the last number (e.g. arr0, arr1, arr2 etc.) although crude, resembles an array!

Additionally LaTex has support for range based loops. Variables, arrays, loops - it's all coming together.

Using the stringstrings and xfp package, LaTex is even able to compute a crude hash of a string: h = (strlen(s) * 67 + wordcnt(s)) % limit

hash map

With the building blocks described above, a simple hash map is fairly easy to implement. Two arrays (for keys and values respectively) the size of the map and it's basically done. For simplicity, each hash map has a set size of 569. Any form of resizing would have increased the complexity tenfold.

The hash map of this project uses open addressing with linear probing. Although not optimal in high load scenarios, it's by far the simplest to implement with the available building blocks.

context

The context functionality takes full leverage of hash maps. A global hash map called contextcategories is used to track all knows contexts. This can be used to dump all contexts at once.

A new context should be created at the beginning of a document. This will add an entry with it's name into contextcategories and create a new hash map with the name of the context. Adding to, removing from, reading from and printing the context simply forwards the request to the underlying hash map.

One additional feature the context has is assertions. \contextRequire{context_name}{required_content} can be used to assert the presence of certain content in the context. If this is not available, it will cause a package error.

summary

LaTex is very powerful. Leveraging this power however, requires a change in mindset from traditional programming as well as a deeper understanding of the token evaluation. Overall a very enjoyable project.