MathJax processing
Brett,
I’m having trouble using Marked to preview blog posts that include equations. My blog is set up to use MathJax, and I’ve fiddled with the Markdown processor it uses (PHP Markdown Extra, because that was one of the best back in the old days and it still works) to use \( \)
for inline equations and\[ \]
for display equations.
I’ve tried to configure Marked to work the same way, but it’s inconsistent. I have it set to use the TeX-AMS_CHTML settings and the following Advanced Configuration:
{
tex2jax: {
inlineMath: [["\(","\)"]],
displayMath: [["\[","\]"]],
processEscapes: false
},
}
(I forgot what processEscapes means, but I get the same results whether it’s true or false.)
With this input,
Exponential growth can be represented this way:
\[ y = A e^{Cx} \]
where \(A\) and \(C\) are constants. The base of the exponential function doesn't matter. We could just as easily express this growth using base-10 logs,
\[ y = A \cdot {10}^{Dx} \]
where
\[ D = C \log_{10} \]
Any base can be used to represent exponential growth, it's just a matter of scaling.
Data that are thought to display exponential growth are commonly plotted on "semilog" graphs, in which the one of the scales is logarithmic and the other is linear. To see why, let's take the base-10 logarithm of both sides of the second equation.
\[ \log_{10} y = \log_{10} \left( A \cdot 10^{Dx} \right) = \log_{10} A + D x \]
This means that if we plot \(\log_{10} y\) vs \(x\), we'll get a straight line with slope \(D\) and intercept \(\log_{10} A\). This is the same as plotting \(y\) on a base-10 log scale and \(x\) on a linear scale.
I get the output in the attached screenshot.
As you can see, one of the display equations is processed, but the others aren’t. A couple of the inline equations are processed correctly, but the others are unprocessed and have messed up the formatting of the nearby text.
If I delete the Advanced Configuration and double all the backslashes, all the equations process correctly, but I really don’t want to write that way, as my LaTeX habits are firmly ingrained. Is there something I’m missing in Advanced Configuration that will get all the equations to process?
// Configuration (do not edit) Marked 2 985 (SETApp)
macOS Version: 10.14.6
additionalMathJaxConfig:
allowPageBreakInCode: YES
bookTxtIsLeanpub: NO
summaryMdIsGitBook: NO
codeIsPoetry: NO
codeCanWrap: YES
collapsibleHeadlines: NO
convertGithubCheckboxes: YES
convertYAMLToMMD: NO
defaultMathJaxConfig: TeX-AMS_CHTML
defaultPreviewStyle: swiss
defaultProcessor: MultiMarkdown
enableMiniMap: NO
excludeBlockQuotesFromCounts: NO
excludeFootnotesFromCounts: NO
excludeVerbatimFromCounts: NO
excludeCaptionsFromCounts: NO
folderPreviewExtensionsArray: md, markdown, txt, mdown, html, opml
githubNewlines: YES
h1IsPageBreak: NO
h2IsPageBreak: NO
hrIsPageBreak: NO
includeMathJax: YES
includeKatex: NO
limitTextWidth: NO
minimalJavaScript: NO
outlineModeExtensions:
permissionGranted: NO
printHeaderBottomLeft:
printHeaderBottomRight:
printHeaderFirstPage: NO
printFooterFirstPage: NO
printHeaderTopLeft:
printHeaderTopRight:
processHTMLFiles: YES
removeMetadataForPreview: NO
removeYAMLHeaders: NO
scrollToEdit: YES
shouldDetectCritic: YES
shouldHighlightMarkupErrors: NO
shouldLoadCritic: NO
syntaxHighlight: NO
syntaxHighlightOnlyLang: NO
useCustomMarkdownProcessor: NO
customMarkdownProcessor:
useCustomPreprocessor: NO
customPreprocessor:
useFirstH1AsPrintTitle: YES
-
20200403-103408.png 72.4 KB
Comments are currently closed for this discussion. You can start a new one.
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
Support Staff 1 Posted by Brett on 04 Apr, 2020 02:59 PM
Remove your advanced configuration entirely and see what results you
get.
-Brett
2 Posted by drdrang on 04 Apr, 2020 03:39 PM
I get more or less what I expect. The single backslashes cause the brackets and parentheses to appear literally, as shown in the attached screenshot. The only things I didn't expect are:
The caret gets interpreted as exponent even when the rest of the equation is treated as regular text. Is that a MulitMarkdown feature I've never used?
The italic weirdness in the first two lines of the final paragraph.
Support Staff 3 Posted by Brett on 04 Apr, 2020 04:06 PM
Using your settings (with no advanced config) I get this:
MMD processor, AMS_CHTML_full. Are you sure you have Mathjax enabled? And that you have an up-to-date version of Marked?
-Brett
Support Staff 4 Posted by Brett on 04 Apr, 2020 04:07 PM
BTW, yes, the caret does create superscript text, but MMD should be ignoring anything within what it considers math blocks.
Support Staff 5 Posted by Brett on 05 Apr, 2020 09:16 AM
In case that image embed is too small to read, attaching a full size screenshot here. Curious if you've discovered anything on your end to explain the difference?
6 Posted by drdrang on 07 Apr, 2020 01:49 AM
I have no explanation. I do have MathJax selected in the Style preference pane (you can see includeMathJax: YES in the configuration section of my first post), and I've relaunched both Marked and Drafts (and rebooted the computer). I've also tried it with the same text in a file.
I get better results using Discount instead of MMD, as you can see in the attached screenshot. Still not right, though.
I'm really surprised to hear that it's rendering for you with and empty Advanced Configuration. Single backslashes in front of parentheses and brackets shouldn't work in a stock MathJax 2 installation, should they?
Support Staff 7 Posted by Brett on 07 Apr, 2020 01:58 AM
The base config for Mathjax implements most "standard" configuration options preferred by LaTeX folks, to the best of my knowledge. It's honestly all Greek to me. It also automatically converts $ and $$ delineators for consistency between implementations.
It really appears no Mathjax processing is happening at all for you in MMD mode, which makes me wonder if there's a script error. Could you attach a full sample document to test with?
- Brett
8 Posted by drdrang on 07 Apr, 2020 02:13 AM
Honestly, it's weirder to me that you're getting proper rendering with no advanced configuration than that I'm getting partial rendering with advanced configuration.
I've attached a document, but it's the same text I included inline with my first support email in this thread. I don't mind sending the full blog post, but it's full of image references that clutter up the document and have nothing to do with the math renderin problem.
By the way, you are using MathJax 2 in Marked, aren't you? It's been a while since I wrote a configuration file for that, but I think they still have the docs up for it. When I first wrote you, I thought you might see something obvious and have a quick answer, but if the answer is deep in MathJaxland, I can dig through the docs myself, as long as I know which version of the docs to pull up.
Support Staff 9 Posted by Brett on 07 Apr, 2020 12:32 PM
Ok, so using your file I get the results you're getting, whereas my
version with your text copy/pasted in is still rendering fine. Now I
have to look into that mystery. It doesn't appear to be an encoding or
"gremlin" issue of any kind. Have you tried copy/pasting into a new file
at all? Not a step I would normally recommend, but something weird is
going on there.
And yes, it's MathJax 2. The implementation of 3 has been too much
effort for few relevant benefits at this point.
-Brett
Support Staff 10 Posted by Brett on 07 Apr, 2020 01:24 PM
Oh, I see. In the email version of the example text that I received for this ticket, all of the surrounding delimiters were double escaped (they are not in the actual code above). In your file, it's all single backslashes. Now I understand your question, I think.
I'm not sure how it works in other systems, but MMD requires that the [] and () surrounding equations be double-backslashed to work. Single backslashes within the equation should work fine, as long as the delimiters are correct. Is that problematic?
Support Staff 11 Posted by Brett on 07 Apr, 2020 01:26 PM
My test file with proper escaping attached.
12 Posted by drdrang on 07 Apr, 2020 02:37 PM
> Is that problematic?
In a word, yes.
As I said in my original email, LaTeX expects single slashes and I have years of habit built in to writing that way, so I set up my blog to handle single slashes, too. I was hoping to do the same with Marked, but it’s looking less likely.
My best bet, assuming I don’t just abandon the idea altogether, might be to write a preprocessor that doubles the appropriate backslashes instead of trying to wrestle with MathJax. I’ll let you know how it works out.
Support Staff 13 Posted by Brett on 07 Apr, 2020 02:59 PM
Yes, or setting up PHP MultiMarkdown Extra as a custom processor might told yield results closer to your blog. The issue (I think) is that MMD eats the single escapes before getting to the Mathjax processing.
- Brett
14 Posted by drdrang on 07 Apr, 2020 04:31 PM
Well, just to close this off, I made a one-line Perl preprocessor
```
#!/usr/bin/perl -p
s/([^\\]|^)\\([\[\]\(\)])/$1\\\\$2/g;
```
I know there are situations in which it will fail, but it should be fairly reliable. Regexes get big when every character you care about is a metacharacter.
The problem with my tweaked PHP Markdown Extra is that the one I use for the blog is meant to be used with MathJax 3, and I’m not sure I still have a copy of the version that worked with MathJax 2.
Support Staff 15 Posted by Brett on 07 Apr, 2020 04:42 PM
I hope that generally works out. Glad to hear there's a reasonable solution.
- Brett