greghendershott / frog

Frog is a static blog generator implemented in Racket, targeting Bootstrap and able to use Pygments.
917 stars 96 forks source link

rendering math formulas #129

Closed WIZARDELF closed 9 years ago

WIZARDELF commented 9 years ago

it's useful to support rendering math formulas.

greghendershott commented 9 years ago

Can you help me understand some of the options for doing this?

Have you already tried using MathJax on your site?

jinwei233 commented 9 years ago

what options ? I can do sth if you need

soegaard commented 9 years ago

Feel free to steal what you need from:

https://github.com/soegaard/mathbook/blob/master/mathbook/defaults.rkt#L22 https://github.com/soegaard/mathbook/blob/master/mathbook/mathjax.rkt

2015-06-28 5:16 GMT+02:00 WeweTom notifications@github.com:

what options ? I can do sth if you need

— Reply to this email directly or view it on GitHub https://github.com/greghendershott/frog/issues/129#issuecomment-116187127 .

Jens Axel Søgaard

tfeb commented 9 years ago

I played with doing this with MathJax, and it works reasonably well. If no-one else has a better answer I can find the notes I made (but it's pretty much 'arrange for the MathJax stuff to be in the header'). The underlying problem is that I don't think there's a way of telling markdown processors that there's a bit of inline text that they should neither parse nor wrap in <pre>...</pre> tags (it is easy for block-level elements). That's a design limitation of markdown I think (I haven't looked at frog's parser to see if it has a solution).

greghendershott commented 9 years ago

Well frog uses a markdown parser I wrote. I could tweak that as necessary.

But to back up, can you give me example(s) of markdown source with MathJax annotations, that could be a problem?

The MathJax docs say:

The default math delimiters are $$...$$ and \[...\] for displayed mathematics, and \(...\) for in-line mathematics.

Do you mean for example that \( 2 * 3 * 4 \) might get parsed to "\( 2 <emph> 3 </emph> 4 \)"? Or that \(x^2)\) might get parsed as "footnote 2 for x"? And so on?

If so I could tweak the markdown parser to prevent that -- to take as literal the text between the MathJax delimiters.

soegaard commented 9 years ago

MathJax injects a little piece of JavaScript in the html-page. At load time it scans through the page and looks for [ ... ] and ( ... ) and replaces them with a rendering of the math formula.

The ... between delimeters need to be in standard LaTeX-format.

That is: It doesn't matter what you use to indicate math-start and math-end in the markdown.

/Jens Axel

2015-06-29 22:01 GMT+02:00 Greg Hendershott notifications@github.com:

Well frog uses a markdown parser I wrote https://github.com/greghendershott/markdown. I could tweak that as necessary.

But to back up, can you give me example(s) of markdown source with MathJax annotations, that could be a problem?

The MathJax docs say:

The default math delimiters are $$...$$ and [...] for displayed mathematics, and (...) for in-line mathematics.

Do you mean for example that ( 2 * 3 * 4 ) might get parsed to "( 2

3 4 )"? Or that (x^2)) might get parsed as "footnote 2 for x"? And so on? If so I could tweak the markdown parser to prevent that -- to take as literal the text between the MathJax delimiters. — Reply to this email directly or view it on GitHub https://github.com/greghendershott/frog/issues/129#issuecomment-116820312 .

Jens Axel Søgaard

tfeb commented 9 years ago

I am not sure if I have looked at what Frog's parser does, but things like $$x_y$$ tend to be problematic, as the underscore gets turned into some HTML.

greghendershott commented 9 years ago

Although some examples aren't a problem as I expected, some are:

#lang racket

(require markdown)

;; OK
(parse-markdown "\\(1 * 2 * 3\\)") ;'((p () "(1 * 2 * 3)"))
(parse-markdown "\\(1*2*3\\)")     ;'((p () "(1*2*3)"))
;; Oops
(parse-markdown "\\(1 *2* 3\\)")   ;'((p () "(1 " (em () "2") " 3)"))

;; Also, looks like it needs to be \\( \\) in the markdown source:
(parse-markdown "\\\\(1 * 2 * 3\\\\)") ;'((p () "\\(1 * 2 * 3\\)"))
soegaard commented 9 years ago

I thought you were going to modify the parser? Everything between ( and ) needs to be kept as-is.

2015-06-29 23:13 GMT+02:00 Greg Hendershott notifications@github.com:

Although some examples aren't a problem as I expected, some are:

lang racket

(require markdown)

;; OK (parse-markdown "(1 * 2 * 3)") ;'((p () "(1 * 2 * 3)")) (parse-markdown "(1_2_3)") ;'((p () "(1_2_3)")) ;; Oops (parse-markdown "(1 2 3)") ;'((p () "(1 " (em () "2") " 3)"))

;; Also, looks like it needs to be ( ) in the markdown source: (parse-markdown "\(1 * 2 * 3\)") ;'((p () "(1 * 2 * 3)"))

— Reply to this email directly or view it on GitHub https://github.com/greghendershott/frog/issues/129#issuecomment-116847839 .

Jens Axel Søgaard

greghendershott commented 9 years ago

I thought you were going to modify the parser?

I am. I was just sharing some examples of why it definitely will need to be modified.

Everything between \( and \) needs to be kept as-is.

Yes. (And also between \[ and \] delimiters, IIUC.)

Also I was sharing that I noticed that \( parses to (, and \[ to [, and so on. This already has a meaning in markdown, for example when you want to say that [] and () are not markdown syntax for links. So probably I'll need to require people to use \\( to get \( for MathJax to see, annoying as that may be.

tfeb commented 9 years ago

(I'm currently away from a machine where I can play with frog so this may be wrong or obvious.)

I think there are three things that matter about sections left as-is for MathJax:

I'm fine with having to type \\(...\\) and \\[...\\] as pairs: it doesn't really take significantly longer to type a double \ than a single one. It would kind of be a nice addition even without MathJax to have a way of saying 'leave this section be for something else to eat'.

greghendershott commented 9 years ago

Yes, within the MathJax delimiters, in the formula text itself, you can still use single \. The formula text will be parsed as literal text, not as markdown.

But I think I do need to use \\ on the delimiters themselves, because people may already be using for example \[foo\] to mean "I just want brackets, don't try to parse this as a markdown link".


After experimenting with using this, I found it somewhat finicky to have MathJax recognize the text in the HTML. I noticed that PHP-markdown emits the MathJax <script> directly. I decided to do that, too, in the markdown parser.

Once that update to the markdown package is live on the package server, I'll merge a change to frog itself (adding a widget, updating the README, and changing its info.rkt to require the newer markdown package version).

greghendershott commented 9 years ago

Although not yet merged to master, it's commit 4a0a156e

tfeb commented 9 years ago

Yes, I am fine with pretty much any delimiter: the thing I care about is that I can take a wodge of TeX and drop it into the bit between them without having to reformat it all (unless it contains `]' or something which, while strictly legal TeX, is not something I can ever see occurring in practice).

Thanks for these changes!