mdbook-mathpunc

Crates.iomdbook-mathpunc
lib.rsmdbook-mathpunc
version0.2.0
sourcesrc
created_at2023-11-20 16:15:56.375778
updated_at2023-12-24 15:37:09.871617
descriptionAn mdbook preprocessor that prevents line breaks between inline math blocks and punctuation marks when using katex.
homepagehttps://github.com/yannickseurin/mdbook-mathpunc
repositoryhttps://github.com/yannickseurin/mdbook-mathpunc
max_upload_size
id1042485
size61,965
Yannick Seurin (yannickseurin)

documentation

https://docs.rs/mdbook-mathpunc

README

mdbook-mathpunc

Crates.io GitHub License

An mdBook preprocessor preventing line breaks between inline math blocks and punctuation marks when using katex.

Installation

Assuming you have mdBook and mdbook-katex installed, install the crate with

$ cargo install mdbook-mathpunc

Then add it as a preprocessor to your book.toml:

[preprocessor.mathpunc]
before = ["katex"]

The before = ["katex"] line ensures that mathpunc is run before the katex preprocessor.

Implementation

This is very basic: the preprocessor simply replaces all occurrences of $p, where p is zero or one closing parenthesis followed by one of the five punctuation marks , . ; : ) (possibly with zero or more white spaces between the dollar sign and p) by p$, except if the dollar sign is escaped with a backslash. If p is : or ):, it adds negative space \!\! before the colon since it is rendered with extra white space in math mode (for example, when writing $a$:, one does not expect any space between a and :, as would be the case when transforming it in $a:$). It does not handle other punctuation marks such as ? or ! as it is uncommon to have a math block followed by these marks. It uses the fancy-regex crate to do this.

Note that this might have unwanted side-effects in case some inline equation starts with a punctuation mark, such as $,a$ which will be replaced by ,$a$.

TODO

Currently the preprocessor only handles the default delimiter for inline math, namely $. The mdbook-katex preprocessor allows to define custom delimiters for inline math, e.g. \( ... \). It would be nice to handle custom delimiters as well here.

Commit count: 11

cargo fmt