sobjornstad / TiddlyRemember

Embed Anki notes in your TiddlyWiki
https://sobjornstad.github.io/TiddlyRemember/
MIT License
65 stars 7 forks source link

Feature requrest: Convert KaTeX delimiters to MathJax delimiters #28

Closed grooveboxunited closed 3 years ago

grooveboxunited commented 4 years ago

In TiddlyWiki the official KaTeX plugin allows you to write Latex equations by surrounding them with the delimiter $$.

Anki uses the delimiters \( and \) to indicate MathJax, so I wonder if it would work to convert the delimiters on sync? This would mean that you could display math equations properly in both TW and Anki.

I have no idea if this is possible or appropriate, but I thought I'd suggest it.

sobjornstad commented 4 years ago

Comes back to #9. I am all for implementing math notation if I can get the plugin to work.

I had not seen this KaTeX plugin before. When I get a chance, I'll see if I can get it to work in my wiki (normally I wouldn't doubt, but the last two I tried for math notation broke, so...)

sobjornstad commented 3 years ago

The problem I'm running into right now is that TiddlyRemember renders the tiddlers to HTML while extracting the TR note contents, and this causes KaTeX rendering to be done, which isn't what we want since it removes the markup itself from the output stream.

I am not aware of a good way to disable just this parse rule, but have started a thread on the TW forums about it.

ldorigo commented 3 years ago

Hi, looked a bit into this because I would also like it to work. It looks like the output from KaTeX actually contains the original latex, see for instance:

<math>
  <semantics>
    <mtable rowspacing="0.24999999999999992em" columnalign="right left" columnspacing="0em">
      ...
    </mtable>
    <annotation encoding="application/x-tex">
    \begin{aligned}
    \frac{1}{2} &amp;= 2x \\
    x &amp;= \frac{1}{4}
    \end{aligned}
    </annotation>
  </semantics>
</math>

So if you extract what's inside the <annotation> element, you should get back the literal latex string (after converting html characters back to unicode)?

sobjornstad commented 3 years ago

@ldorigo, thanks, I think this is going to work. I went spelunking through the DOM before but evidently overlooked that <annotation> tag.

sobjornstad commented 3 years ago

Anyone care to try the version in the katex-sync branch to make sure this works the way you were expecting? You would just have to remove your existing TiddlyRemember plugin from Anki, download the contents of the anki-plugin/src folder on that branch, and copy them into a new folder in the addons folder in your Anki file location, then restart Anki and try a sync with some tiddlers containing KaTeX markup. No relevant changes to the TiddlyWiki plugin, so no need to update that.

grooveboxunited commented 3 years ago

I've tried to test the katex-sync branch but can't get Anki to load the plugin. I removed TiddlyRemember in Anki, downloaded the zip file for the katex-sync branch, then copied the contents of anki/plugin/src into a folder called "1234" in the Anki addons folder. I get this error when I start Anki.

I'm sure I'm doing something wrong, but I don't know what.

sobjornstad commented 3 years ago

@grooveboxunited Nope, this is my bad, I forgot there is a build step involved. Some of the files needed to produce a complete add-on aren't committed. I'll get you a proper build when I get home in a couple hours.

sobjornstad commented 3 years ago

@grooveboxunited OK, I pushed another commit to that branch, give it another try. This time I actually tested it, so I'm hopeful it will work this time. :)

grooveboxunited commented 3 years ago

@sobjornstad Thank you, that works as I expect for basic cards. There is, however, a problem that I should have anticipated - latex uses curly braces quite a bit so you get problems when using it with cloze cards.

For example:

<<remembercz "1"
    "$$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$ is known as the {quadratic formula}$$">>

The above example gives an error on sync, I think because of the nested curly braces. If a formula doesn't have nested braces, then any braces that appear in the formula get converted into clozes in Anki, which obviously breaks the formula and generates multiple nonsense cards. Here is an example of that:

<<remembercz "2"
    "$$[\begin{smallmatrix} a & b \\ c & d\end{smallmatrix}]$$ is an example of a {matrix}$$">>

This gets converted into:

\([\begin{{c1::smallmatrix}} a & b \\ c & d\end{{c2::smallmatrix}}]\) is an example of a {{c3::matrix}}

I don't know if it is possible for TiddlyRemember to ignore curly braces if they are contained within dollar signs?

sobjornstad commented 3 years ago

Thanks, I expect this can be fixed within TiddlyRemember's cloze parsing module (the dollar signs are long gone at that point, but there should be an HTML span we can key on, or simply the \( \) / \[ \] MathJaX delimiters). I'm trying to get the first draft of my book on TiddlyWiki out for review at the moment, so it may be a couple of weeks before I get a chance to take a closer look.

sobjornstad commented 3 years ago

Whoops, looks like I totally forgot about this, sorry about that. Ran across the half-finished changes on my hard drive. It will probably be a few more weeks, but starting in August I am on sabbatical working on open-source for a while, so I should definitely be able to get this in then!

sobjornstad commented 3 years ago

In the next version, it will be possible to escape curly braces that aren't cloze delimiters with backslashes, like \{ and \}. So you can say:

<<remembercz "1"
    "$$x = \frac\{-b \pm \sqrt\{b^2 - 4ac\}\}\{2a\}$$ is known as the {quadratic formula}.">>

If that's too much Leaning Toothpick Syndrome for your taste, you could write your own wrapper macro around remembercz that lets you use other characters of your choice in place of the braces by automatically escaping single braces and then converting the other characters to braces, for example:

\define latex-cloze(id, text, mode: "block", reference: "", sched: "", class: "")
  <$set name="mungedText" value={{{ [<__text__>search-replace:g[{],[\{]search-replace:g[}],[\}]search-replace:g[«],[{]search-replace:g[»],[}]] }}}>
    <$macrocall $name="remembercz" id=<<__id__>> text=<<mungedText>> mode=<<__mode__>> reference=<<__reference__>> sched=<<__sched__>> class=<<__class__>>/>
  </$set>
\end

<<latex-cloze "1"
    "$$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$ is known as the «quadratic formula».">>

I am however noticing that fractions don't appear to display correctly, so I'll still have to look into that:

corrupted display in cloze

sobjornstad commented 3 years ago

Fractions were broken because the escaped \ was being passed through to LaTeX and \{ was inhibiting \frac from accepting the argument. I've added a separate display span for clozes that replaces out the escape characters:

<span class="cloze-display"><$set name="unescape" value={{{ [<__text__>search-replace:g[\{],[{]search-replace:g[\}],[}]] }}}><<unescape>></$set></span>
<span class="cloze-text">$text$</span>

cloze-text is now display: none;'d out in CSS, so it'll only be read by the TiddlyRemember parser.