lzanini / mdbook-katex

A preprocessor for mdBook, rendering LaTex equations to HTML at build time.
MIT License
195 stars 34 forks source link

Support many custom delimiters #101

Closed ayoubelmhamdi closed 8 months ago

ayoubelmhamdi commented 9 months ago

ChatGPT sometimes outputs math formulas as \[x=2\] and sometimes as $x=2$. Is there any way to extend the custom delimiter to be like:

[preprocessor.katex]
after = ["links"]
block-delimiter = [{ left = "\\[", right = "\\]" }, { left = "$", right = "$" }]
inline-delimiter = [{ left = "\\(", right = "\\)" }, { left = "$$", right = "$$" }]

test:

$E=\frac{1}{n} \sum_{i=1}^{n} (y_i - w_i)^2$ (4)

$$E=\frac{1}{n} \sum_{i=1}^{n} (y_i - w_i)^2$$ (5)

\(E=\frac{1}{n} \sum_{i=1}^{n} (y_i - w_i)^2\) (5)

\[E=\frac{1}{n} \sum_{i=1}^{n} (y_i - w_i)^2\] (6)

image

SichangHe commented 9 months ago

Why don't you ask ChatGPT to do that?

ayoubelmhamdi commented 9 months ago

Not every GPT is powerful, and when we have a complex or lengthy prompt, the GPTmay not follow all instructions correctly, but the programming language (like Rust) generates text very consistently

ayoubelmhamdi commented 9 months ago

My temporary solution is to calculate the dominance between the two situations.

set -e

file="file.md"

left="$(grep -o -e '\\(\(\([^\]*\)[^)]\+\))' -e '\\\[\(\([^\]*\)[^]]\+\)]' "$file" | wc -l)"
dollar="$(grep -o '\$\+\([^$]\+\)\$\+' "$file" | wc -l)"

if [ "$dollar" -gt "$left" ];then
  cat book_dollar.toml > book.toml
else
  cat book_left.toml > book.toml
fi
SichangHe commented 8 months ago

Hello @ayoubelmhamdi, was your problem solved?

I do not plan to support multiple different delimiters because 1. it would complicate the code in a way that I do not find worthy and 2. it would encourage people to commit crimes on using many inconsistent delimiters.

However, if you would like to implement this feature, here is where you need to look at:

https://github.com/lzanini/mdbook-katex/blob/5302e9611b40e8248fa6c5d73fc48c5805258921/src/scan.rs#L121-L129

Hope this helps :D.

ayoubelmhamdi commented 8 months ago

thinks @SichangHe.

I think we don't have many delimiters. Is it acceptable to set a fixed number of delimiters that is already supported, so that it automatically handles these default delimiters without requiring the user to specify which delimiter to use?

This approach would eliminate the need for users to think about which delimiter to use.

SichangHe commented 8 months ago

I did not understand what you mean by "set a fixed number of delimiters that is already supported". Do you mean that we hardcode several delimiter pairs? Also, the default delimiters are dollar signs.

ayoubelmhamdi commented 8 months ago

@SichangHe I mean, we should not accept any delimiters from the user. The user should not worry about delimiters because there is a list of supported delimiters.

[preprocessor.katex]
after = ["links"]
block-delimiter = None # not supported anymore
inline-delimiter = None # not supported anymore

mdbook-katex supports only a list of delimiters, like

for the left [ "\\[",  "$", "<math>"]
for the right [ "\\]",  "$", "</math>"]

If you think there is another delimiter that is very useful, you could add it to the list. What do you think?

SichangHe commented 8 months ago

Sorry for the delay. I am not a fan of this change because either users will lose some customizability or we will have to deal with all the weird delimiters people use/don't use.

I think for your original problem, it would be much easier to write a script to replace delimiters in your source file to unify all delimiters.