How to access MathJax extensions (like siunitx) from Rmarkdown via Pandoc & Knitr? - rstudio

Working with Rmarkdown in Rstudio, using pandoc and knitr, I am targetting PDF output via LaTeX and HTML output with MathJax. I would like to use some of the MathJax extensions that are available, to allow richer LaTeX for the PDF target. Specifically, I am trying to use the siunitx extension right now, although I am also interested in others (e.g. physics).
Using siunitx works fine with LaTeX for PDF output, but I've had a hard time getting it working with HTML output.
Here an example Rmarkdown file:
---
title: "siunitx test"
author: "chriss"
date: "June 13, 2017"
output:
html_document:
mathjax: https://cdn.rawgit.com/mathjax/MathJax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML
number_sections: yes
pdf_document:
keep_tex: yes
latex_engine: xelatex
number_sections: yes
header-includes: \usepackage{siunitx}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# The Problem
I would like to be able to use `siunitx` latex macros from `Rmarkdown`,
targetting PDF output via latex and html with MathJax. It should get me proper
formatting of things like $\SI{120}{\W\per\square\m}$ and $\SI{0.8}{\A\per\W}$,
as long as I put them in a latex math environment, so that MathJax picks them
up.
The PDF output is OK when I add the `header-includes: \usepackage{siunitx}` to
the `YAML` header, but how can I access the MathJax `siunitx` extension via the
knitr -> pandoc -> mathjax/html route?
Check: is MathJax working in general: $\frac{1}{r^2}$
This knits fine to PDF, but the $\SI{}{}$ are output verbatim and hilighted red in the HTML output, and in RStudio. I'm having pandoc get MathJax from rawgit.org, since the default of cdn.mathjax.org is soon-to-be defunct, and it seems, no longer has a Contrib path with the extensions.
I have tried adding MathJax's $\require{siunitx}$ with variations on the path to the siunitx extension, to no avail. This causes the HTML to look for the siunitx extension, but apparently in the wrong place: https://cdn.rawgit.com/mathjax/MathJax/2.7.1/extensions/TeX/siunitx.js?V=2.7.1, which is a 404.
If I remove the \require{} and remove the part of the output HTML file that loads MathJax dynamically (labelled <!-- dynamically load mathjax for compatibility with self-contained -->), and manually add:
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {inlineMath: [["$","$"],["\\(","\\)"]]},
errorSettings: {message: undefined},
TeX: { extensions: ["[burnpanck]/siunitx/unpacked/siunitx.js"] }
};
MathJax.Ajax.config.path['burnpanck'] =
'https://rawgit.com/burnpanck/MathJax-third-party-extensions/master';
</script>
<script type="text/javascript"
src="https://cdn.rawgit.com/mathjax/MathJax/2.7.1/latest.js?config=TeX-AMS-
MML_HTMLorMML"></script>
To header of the HTML file, then it briefly pops up a complaint about some issue with siunitx.js but produces correct output (this is a modified version of the header from the example for the siunitx MathJax extension, from here )
This suggests that I could modify the HTML template for pandoc to reflect those changes, and things would basically work.
However, the following questions remain:
Is changing the HTML template in this way the proper way to fix the HTML output? Are these the URLs that are intended to be used now that cdn.mathjax.org is going down, or are there better ones that I should use instead?
Why do I still get the warning about siunitx.js?
What would need to be done to have Rstudio understand the siunitx content in its preview? Is there already a way to enable this (e.g. convince it to use siunitx extension, assuming it's built on MathJax), or would this be a feature request..?
Indeed, it would be nice if there was an easy way to access the MathJax extensions out-of-the-box, without having to go to the trouble of editing templates and the like, with proper handling in the Rstudio GUI. I can imagine there may be Rstudio users who would benefit from the extra functionality but don't want to / aren't able to jump through these kind of hoops to access it.
UPDATE The warning message I see when loading the 'working' HTML about siunitx.js seems to be a general issue with the current version of siunitx.js, due to changes to the MathJax CDN, see issue raised here: https://github.com/burnpanck/MathJax-third-party-extensions/issues/5

I'm using includes in_header to solve the problem.
---
title: "doku1"
output:
html_document:
includes:
in_header: header.html
pdf_document:
keep_tex: yes
latex_engine: pdflatex
number_sections: no
header-includes: \usepackage{mhchem, siunitx}
---
header.html looks like this
<script type="text/x-mathjax-config">
MathJax.Ajax.config.path["mhchem"] = "https://cdnjs.cloudflare.com/ajax/libs/mathjax-mhchem/3.3.2";
MathJax.Ajax.config.path['myExt'] = 'https://rawgit.com/burnpanck/MathJax-third-party-extensions/master';
MathJax.Hub.Config({
TeX: { extensions: ["AMSmath.js","AMSsymbols.js","[myExt]/siunitx/unpacked/siunitx.js","[mhchem]/mhchem.js", "color.js"] }
});
</script>
It works, but is rather slow.
John

Related

Is there a built-in markdown flavor for Firefox?

I like to use markdown languages like GitHub markdown and ASCIIDoc to provide lightweight formatting to text documents. The tags in HTML are too heavy and render the original text almost unreadable.
The problem is when I send documents to other users. They can't be bothered with installing a markdown plugin. I would like to use a markdown flavor that will render predictably in web browsers. That way I can send a URL for my document and the recipient will see the formatted text.
Is there a standard markdown language built into Firefox?
Thanks,
(PS: this is a serious question. Pedants please restrain yourselves.)
Unfortunately, at the moment, there are no major web browsers that natively support parsing and rendering markdown.
However, there are a few solutions.
Render the markdown to html and send the html document. Most renderers automatically include Stylesheets that make the html look good, or you can edit the output or templates yourself.
Get the recepent to install a extension that will render the markdown. I quickly found something by googling firefox markdown extension.
I hope this solved your problem.

How do I put HTML, JS and CSS in a Github page .md file?

I'm writing a post for my Github-hosted site. I'm writing to my-first-post.md.
I have Javascript in script tags, CSS in style tags, and HTML in divs and spans.
When I paste the code in the .md and run jekyll serve thru Ruby, all I get is a page with the raw HTML.
What am I doing wrong?
"HTML is a publishing format; Markdown is a writing format." --daringfireball
Markdown is supposed to be simple.
You shouldn't be putting javascript into markdown unless you have a good reason to be doing so and even then I don't think every markdown converter will process javascript.
Ideally, your markdown should be confined to this list and maybe some simple HTML.
Any CSS or javascript should be included as part of your theme.
Edit, explaining jekyll filestructure:
So your Jekyll site should have a file structure that looks something like this. In very broad terms, your CSS and javascript should be in the default.html file located in the _layouts folder. You'll probably notice that the file appears to be a regular HTML file, more or less. The blog post--which is the markdown after it has been converted to HTML--will insert itself into the layout at the {{ content }} tag.
Also, the CSS and javascript doesn't have to be in default.html. It can also be in external files that you link to from default.html
Use MDX. It can do a lot. But if you want to use something more appropriate for the task at hand, I'd suggest looking into Astro . You can find more on these websites. For astro you can install the CLI and write markdown in less than 2 minutes. And also, it's very fast and integrates with a lot of frameworks.

CKEditor - have it return markdown syntax instead of HTML

I'm working on a CMS platform and I am planning to use CKEditor as it seems to offer everything I need.
One thing that is a bit of a bother to me is that I want my content to be in markdown format instead of html and while I found a BBCode extension for this, I couldn't figure out how it can be remade to support markdown.
I tried to find an editor that does markdown out of the box, but the ones I found are way too simple for what I need and CKEditor has the benefit of having a plugin system to adjust perfectly for me.
CKEditor now has a Markdown addon that does this exact thing. The addon project is hosted on github.
Screenshots:
See also: Integrated Markdown WYSIWYG text editor (2012)
Using Markdown instead of HTML is a very bad idea for several reasons:
Markdown has no spec, so every library works differently in details. The output which you'll produce using CKEditor may give a different (even totally wrong) result when transformed to HTML by your back-end. For example - escaping image's title and link texts - you won't be able to ensure that the text user inserted does not break the output.
Not all HTML can be transformed to Markdown.
There are plenty of tricky cases which are totally correct in HTML, but cannot be done in Markdown.
Markdown has fewer features than HTML, so you'd lose some content which users produced.
You actually gain nothing by using Markdown instead of HTML.
I am a CKEditor core developer, so I know it very well. I tried to implement a Markdown writer for CKEditor and very quickly I found that it's completely pointless. I don't say that it's not possible, because it is, but only a limited stability can be achieved - too low for anything I would personally want to use in production.

Do not process HTML as Markdown but keep definition lists

I like the definition list syntax in Pandoc. I also have some raw HTML that is put into my document.
I don't want the HTML to be parsed as Markdown. I tried --strict to force Pandoc to ignore the HTML. This works but unfortunately this turns off the definition list feature. I could use HTML to write my DL but that is tedious. (The HTML comes from another source and should be left untouched.)
Are there any configuration options that give me more granularity over this?
(see raw html section in the manual and the definition list section. They both talk about how --strict disables them.)
In the development version of pandoc, you can enable and disable pandoc extensions piecemeal, so for example you can specify -t markdown-markdown_in_html_blocks to disable the extension for parsing markdown inside HTML blocks.
You'd need to install from source, which requires the Haskell Platform. Instructions are here, or you can wait for the next release.

Integrated Markdown WYSIWYG text editor

In looking for a straightforward WYSIWYG editor for Markdown code, I am not finding a comparible UI to that of CkEditor, TinyMCE, ect.
Specifically, the Markdown "WYSIWYG" editors that are often recommended (such as posts like this ) are not pure WYSIWYG editors in the sense that users either still write raw Markdown ( MarkItUp ) or go to the other extreme of having in-line editing without standard controls ( Hallo ).
I need something in-between.
I'm looking for a Markdown editor that looks and functions like a stripped down CkEditor text box, and that accepts and outputs Markdown. There should be a toolbar with a minimum set of formatting options (B, I, U, lists, ect), and the text entry area should show the converted Markdown, not the raw code. There should be a Source button that will allow users to edit the raw Markdown, but even that is optional. Ex:
I get the reason for Markdown/wiki, ect - the security it offers. I don't mind entering raw code like here at SE, but my users are not geeks and do not find this enjoyable. They don't want to see * * * ___ and spaces mixed in with their text. They are used to "Word" style editing, and are most productive in that environment.
So - is there a truly integrated WYSIWYG editor for Markdown? I'm writing in a PHP, so something that I can invoke with a class would be perfect.
Sept 23, 2015 Update
CKEditor now has a Markdown addon that does this exact thing. The addon project is hosted on github.
Screenshots:
Apr 13, 2015 Update
Someone professing to develop CKEditor says that the appearance of CommonMark is a game changer, and we could possibly see a proper markup interface for CKEditor (read comments for the full story).
Feb 6, 2015 Update
CKEditor now comes with a plugin that outputs (and accepts as input) BBCode.
Demo: http://ckeditor.com/demo#bbcode
SimpleMDE, a newcomer, may be the answer. I've been searching for something just like this for a month now. I'm surprised that this does not show up higher in search results. I had to go through a notice on lepture/editor to find this.
I was researching in this subject the other day and I haven't found any decent WYSIWYG editor with Markdown output. In fact, first you have to have to create a WYSIWG Markdown editor is WYSIWG HTML editor and there are just few of them that are usable on the market.
There's a chance that you'll be able to create dataProcessor for CKEditor that will change HTML editor into Markdown editor. We've got a plugin for BBCode that works like this (check out http://nightly-v4.ckeditor.com/3737/samples/bbcode.html).
All you have to do is implement this interface http://nightly-v4.ckeditor.com/ckeditor_api/#!/api/CKEDITOR.dataProcessor. If you check BBCode plugin's code you'll see some hacks and tricks, because unfortunately current CKEditor's architecture isn't ready (yet) to create such a data processor. However, I believe that if you want to provide only few styling options you should be able to implement Markdown support pretty quickly.
EDIT Sept 23, 2015
CKEditor now has a Markdown addon that does this exact thing. The addon project is hosted on github.
Screenshots:
As posted in my Feb 6, 2015 update, CKEditor now includes plugins that allow BBCode input and output.
A demo is available here: http://ckeditor.com/demo#bbcode
EDIT Apr 13, 2015:
Someone professing to develop CKEditor says that the appearance of CommonMark is a game changer, and we could possibly see a proper markup interface for CKEditor (read comments for the full story).
Pen is a new (active as of 2014) WYSIWYG editor that outputs Markdown.
It's not perfect—I've had issues with pasting HTML there—but it works.
Edit: Sorry! It doesn't output Markdown. Walery Strauch pointed out in the comments that the Markdown formatting signs I saw were actually CSS pseudo element rules:
Still, I'll leave it here as an option since some people upvoted this answer and it may have been of use to somebody.
I implemented a very simple editor that allows the contents of a <textarea> that contains Markdown to be edited in a WYSIWYG fashion.
I used Hallo. I don't think its web site makes it obvious that it is not itself a Markdown WYSIWYG editor but the demo does forge the path to one.
Hallo allows WYSIWYG editing of the HTML inside a <div>. I used javascript to hide any <textarea> blocks that have a specific wysiwyg CSS class, replacing it with a <div> and copying the contents of the <textarea> into the <div>. The copying runs through Showdown which produces HTML from Markdown.
Another Javascript routine reacts every time the <div> contents change. It copies the contents back into the (now hidden) <textarea>. The content is run through to-markdown to convert from HTML to Markdown.
If the <textarea> is a field in a <form>, then the edited Markdown will be sent to the server when that form is submitted.
The inspiration for this comes from the Hallo Markdown Example, specifically the editor.js file. I used that as a basis for my own script along with hallo.js, showdown.js and to-markdown.js.
My script, wysiwyg.js is a derivative of editor.js from the Hallo Markdown Example. Some points to note:
I use this in a Rails application (not that that matters)
It runs on ready and also on page:load, the latter because Rails uses Turbolinks
It runs on ajaxComplete because I use Ajax for form error reports
There are other dependencies: JQueryUI and Rangy (Rails users can avail of the gems jquery-ui-rails and rangy-rails).
Also Font Awesome is used for the tool bar icons. The version of hallo.js used by the demo is out of date (it uses an old version of Font Awesome) - use hallo.js from GitHub instead.
You only need to add CSS class='wysiwyg' to any <textarea> to enable WYSIWYG on it. The <textarea> should conain Markdown formatted text.
I would expect that wysiwyg.js could be easily adapted to use another editor if you don't like Hallo as long as it works on the HTML in a <div>. There are quite a few to choose from but not all are as lightweight as Hallo.
A smilar piece of work that I found is markdown-html-form. It uses the same Showdown and to-markdown.
I am also looking for a markdown editor that is described at the top of this thread
Have you seen "markdown tools": http://md-wysiwyg.sourceforge.net/
Demo: http://md-wysiwyg.sourceforge.net/cgi-bin/cgi_wysiwyg_test.py/
That seems pretty close to what we are looking for, it does a reasonable job of taking your WYSIWYG rich text and outputting markdown. However it failed on an encoding exception when I pasted in some rich text from a google doc.
I found two which currently seem to be quite popular, active and free but haven't been mentioned yet.
The first one is Toast UI Editor. It supports both modes (WYSIWYG and raw). It has official react and vuejs wrappers.
The second one is ProseMirror which calls itself "a toolkit for building rich-text editors on the web" and it seems to be very flexible and extensive. It also supports both modes.
I'm pretty sure solid unofficial react / vue / angular wrappers for it can be found as well.

Resources