#mdbook #katex #pre-processor #line-break #punctuation

bin+lib mdbook-mathpunc

An mdbook preprocessor that prevents line breaks between inline math blocks and punctuation marks when using katex

2 unstable releases

0.2.0 Dec 24, 2023
0.1.0 Nov 20, 2023

#1043 in Math

MIT license

18KB
150 lines

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.

Dependencies

~12–23MB
~346K SLoC