Mathjax being parsed with Jekyll - jekyll

I have been trying for a while now to get MathJax to render properly using Jekyll.
I have tried several parsers (regular, kramdown and redcarpet) but none seem to do what I want. I have also tried all the solution mentioned here: Using MathJax with Jekyll
The problem is that I am using rules to write down some equations. I have created a minimal working example using plain html, to make sure that my code is actually valid. You can see that here: http://call-cc.be/files/example.html
I just want to display that exact code (well, ill change it once it works) on my jekyll-generated website.
However, the parser is reading through the mathjax and complaining about liquid tags not being closed:
Configuration file: /Users/m1dnight/vcs/cdetroye.github.io/_config.yml
Source: /Users/m1dnight/vcs/cdetroye.github.io
Destination: /Users/m1dnight/vcs/cdetroye.github.io/_site
Incremental build: disabled. Enable with --incremental
Generating...
Liquid Exception: Liquid syntax error: Variable '{{#1}' was not properly terminated with regexp: /\}\}/ in /Users/m1dnight/vcs/cdetroye.github.io/_posts/2015-11-11-stlc.md
jekyll 3.0.1 | Error: Liquid syntax error: Variable '{{#1}' was not properly terminated with regexp: /\}\}/
In addition to the solutions I tried mentioned above, I have tried putting the code in {%raw%} tags and that just rendered the actual mathjax code, including some <p> here and there.
Another solution I tried was this plugin I found, but again, to no avail. https://gist.github.com/jessykate/834610
The full post I am trying to render is shown below. Watch out, is rather lenghty.
---
layout: post
title: Work goddamnit
---
Foo bar bar
<div>
$$
\newcommand{\lolli}{\multimap}
\newcommand{\tensor}{\otimes}
\newcommand{\with}{\&}
\newcommand{\all}[2]{\forall {#1}.\;{#2}}
\newcommand{\fix}[2]{\mu {#1}.\;{#2}}
\newcommand{\letv}[3]{\mathsf{let}\,{#1} = {#2}\;\mathsf{in}\;{#3}}
\newcommand{\Fun}[2]{\Lambda {#1}.\;{#2}}
\newcommand{\fun}[2]{\lambda {#1}.\;{#2}}
\newcommand{\unit}{\left(\right)}
\newcommand{\letunit}[2]{\letv{\unit}{#1}{#2}}
\newcommand{\pair}[2]{\left({#1},{#2}\right)}
\newcommand{\letpair}[4]{\letv{\pair{#1}{#2}}{#3}{#4}}
\newcommand{\unita}{\left[\right]}
\newcommand{\paira}[2]{\left[{#1}, {#2}\right]}
\newcommand{\fst}[1]{\mathsf{fst}\,{#1}}
\newcommand{\snd}[1]{\mathsf{snd}\,{#1}}
\newcommand{\inl}[1]{\mathsf{inl}\,{#1}}
\newcommand{\inr}[1]{\mathsf{inr}\,{#1}}
\newcommand{\case}[5]{\mathsf{case}({#1}, \inl{#2} \to {#3}, \inr{#4} \to {#5})}
\newcommand{\abort}[1]{\mathsf{abort}\,{#1}}
\newcommand{\fold}[1]{\mathsf{in}\,{#1}}
\newcommand{\unfold}[1]{\mathsf{out}\,{#1}}
\newcommand{\judge}[4]{{#1};{#2} \vdash {#3} : {#4}}
\newcommand{\judgetp}[2]{{#1} \vdash {#2} : \mathsf{type}}
\newcommand{\judgectx}[2]{{#1} \vdash {#2} : \mathsf{ctx}}
%% Rule names
\newcommand{\rulename}[3]{{#2}{\mathrm{#3}}_{\mathrm{#1}}}
\newcommand{\intro}[2][]{\rulename{#1}{#2}{I}}
\newcommand{\elim}[2][]{\rulename{#1}{#2}{E}}
\newcommand{\Var}{\rulename{}{}{Var}}
\newcommand{\AllI}{\intro{\forall}}
\newcommand{\AllE}{\elim{\forall}}
\newcommand{\LolliI}{\intro{\lolli}}
\newcommand{\LolliE}{\elim{\lolli}}
\newcommand{\UnitI}{\intro{1}}
\newcommand{\UnitE}{\elim{1}}
\newcommand{\TensorI}{\intro{\tensor}}
\newcommand{\TensorE}{\elim{\tensor}}
\newcommand{\TopI}{\intro{\top}}
\newcommand{\TopE}{\elim{\top}}
\newcommand{\WithI}{\intro{\with}}
\newcommand{\WithEFst}{\elim[fst]{\with}}
\newcommand{\WithESnd}{\elim[snd]{\with}}
\newcommand{\ZeroI}{\intro{0}}
\newcommand{\ZeroE}{\elim{0}}
\newcommand{\SumIInl}{\intro[inl]{\oplus}}
\newcommand{\SumIInr}{\intro[inr]{\oplus}}
\newcommand{\SumE}{\elim{\oplus}}
\newcommand{\MuI}{\intro{\mu}}
\newcommand{\MuE}{\elim{\mu}}
\newcommand{\size}[1]{\left|#1\right|}
\newcommand{\inferrule}[3][]{\frac{#2}{#3}\;{#1}}
$$
$$
\boxed{\judge{\Delta}{\Gamma}{e}{A}}
\\
\inferrule[\Var]
{ }
{\judge{\Delta}{x:A}{x}{A}}
\\
\begin{array}{cc}
\inferrule[\AllI]
{\judge{\Delta, \alpha}{\Gamma}{e}{A}}
{\judge{\Delta}{\Gamma}{\Fun{\alpha}{e}}{\all{\alpha}{A}}}
&
\inferrule[\AllE]
{\judge{\Delta}{\Gamma}{e}{\all{\alpha}{B}} \\
\judgetp{\Delta}{A}}
{\judge{\Delta}{\Gamma}{e\;A}{[A/\alpha]B}}
\\[2em]
\inferrule[\LolliI]
{\judge{\Delta}{\Gamma, x:A}{e}{B}}
{\judge{\Delta}{\Gamma}{\fun{x:A}{e}}{A \to B}}
&
\inferrule[\LolliE]
{\judge{\Delta}{\Gamma}{e}{A \to B} \\
\judge{\Delta}{\Gamma'}{e'}{A}}
{\judge{\Delta}{\Gamma,\Gamma'}{e\;e'}{B}}
\\[2em]
\inferrule[\UnitI]
{ }
{\judge{\Delta}{\cdot}{\unit}{1}}
&
\inferrule[\UnitE]
{\judge{\Delta}{\Gamma}{e}{1} \\
\judge{\Delta}{\Gamma'}{e'}{C}}
{\judge{\Delta}{\Gamma,\Gamma'}{\letunit{e}{e'}}{C}}
\\[2em]
\inferrule[\TensorI]
{\judge{\Delta}{\Gamma}{e}{A} \\
\judge{\Delta}{\Gamma'}{e'}{B} }
{\judge{\Delta}{\Gamma,\Gamma'}{\pair{e}{e'}}{A \tensor B}}
&
\inferrule[\TensorE]
{\judge{\Delta}{\Gamma}{e}{A \tensor B} \\\\
\judge{\Delta}{\Gamma', x:A, y:B}{e'}{C}}
{\judge{\Delta}{\Gamma,\Gamma'}{\letpair{x}{y}{e}{e'}}{C}}
\\[2em]
\inferrule[\TopI]
{ }
{\judge{\Delta}{\Gamma}{\unita}{\top}}
&
\\[1em]
\inferrule[\WithI]
{\judge{\Delta}{\Gamma}{e}{A} \\
\judge{\Delta}{\Gamma}{e'}{B} }
{\judge{\Delta}{\Gamma}{\paira{e}{e'}}{A \with B}}
&
\begin{array}{l}
\inferrule[\WithEFst]
{\judge{\Delta}{\Gamma}{e}{A \with B}}
{\judge{\Delta}{\Gamma}{\fst{e}}{A}}
\\[1em]
\inferrule[\WithESnd]
{\judge{\Delta}{\Gamma}{e}{A \with B}}
{\judge{\Delta}{\Gamma}{\snd{e}}{B}}
\end{array}
\\[3em]
\begin{array}{l}
\inferrule[\SumIInl]
{\judge{\Delta}{\Gamma}{e}{A }}
{\judge{\Delta}{\Gamma}{\inl{e}}{A \oplus B}}
\\[1em]
\inferrule[\SumIInr]
{\judge{\Delta}{\Gamma}{e}{ B}}
{\judge{\Delta}{\Gamma}{\inr{e}}{A \oplus B}}
\end{array}
&
\begin{array}{l}
\inferrule[\SumE]
{\judge{\Delta}{\Gamma}{e}{A \oplus B} \\\\
\judge{\Delta}{\Gamma', x:A}{e'}{C} \\\\
\judge{\Delta}{\Gamma', y:B}{e''}{C} }
{\judge{\Delta}{\Gamma, \Gamma'}{\case{e}{x}{e'}{y}{e''}}{C}}
\end{array}
\\[3em]
&
\inferrule[\ZeroE]
{\judge{\Delta}{\Gamma}{e}{0}}
{\judge{\Delta}{\Gamma}{\abort{e}}{C}}
\end{array}
$$
$$a^2 + b^2 = c^2$$
$$
\begin{array}{lcl}
(\Fun{\alpha}{e})\;A & \mapsto & [A/\alpha]e \\
(\fun{x:A}{e})\;e' & \mapsto & [e'/x]e \\
\letv{\unit}{\unit}{e'} & \mapsto & e' \\
\letv{\pair{x}{y}}{\pair{e_1}{e_2}}{e'} & \mapsto & [e_1/x, e_2/y]e' \\
\fst{\paira{e}{e'}} & \mapsto & e \\
\snd{\paira{e}{e'}} & \mapsto & e' \\
\case{\inl{e}}{x}{e'}{y}{e''} & \mapsto & [e/x]e' \\
\case{\inr{e}}{x}{e'}{y}{e''} & \mapsto & [e/y]e'' \\
\end{array}
$$</div>
The end.

We have two problems here :
Liquid is parsing you code because of the brace. We make use of {% raw %}{% endraw %} tag.
Kramdown is also parsing your mathjax as code and embed it in
<div class="highlighter-rouge"><pre class="highlight"><code>
Your code here
</code></pre></div>
This is because your code is indented four spaces or more.
To avoid this you can remove indentation or make use of the {::nomarkdown}{:/} tag.
This definitely resolve this issue :
{% raw %}{::nomarkdown}
<div>
$$
\newcommand{\lolli}{\multimap}
\newcommand{\tensor}{\otimes}
... More mathjax here
$$
</div>
{:/}{% endraw %}

Related

Mathjax render issues on combination of bmatrix from jekyll with kramdown

I use jekyll to build a static blog with kramdown and use mathjax to render the LaTeX equation in markdown files.
To make mathjax work for all files I write mathjax in _layout/default.html where add following:
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: [
"tex2jax.js",
"MathMenu.js",
"MathZoom.js",
"AssistiveMML.js",
"a11y/accessibility-menu.js"
],
tex2jax: {
inlineMath: [['$', '$']],
displayMath: [['$$', '$$']]
},
jax: ["input/TeX", "output/CommonHTML"],
TeX: {
extensions: [
"AMSmath.js",
"AMSsymbols.js",
"noErrors.js",
"noUndefined.js",
]
}
});
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
A quick example, I can render
$(a_n)_{n=1}^{\infty}$
but failed:
$(a_n)_{n=1}^{\infty} = (a_n)_{n=1}^{\infty}$
It also appear in other equation, e.g.
the equation I exactly write is:
$(1 / \sqrt{n}) 1_n$ \: $ d_i = Y_i - \bar X_i 1_n = \begin{bmatrix} y_{i1} - \bar x_i \\\ y_{i2} - \bar x_i \\\ \vdots \\\ y_{in} - \bar x_i \end{bmatrix}$
But I separate them such as (first part with colon on the first line):
We also have deviation vectore which the collection of the distance of each element from $Y_i$ to its projection on $(1 / \sqrt{n}) 1_n$:
$d_i = Y_i - \bar X_i 1_n =$
$\begin{bmatrix} y_{i1} - \bar x_i \\\ y_{i2} - \bar x_i \\\ \vdots \\\ y_{in} - \bar x_i \end{bmatrix}$
then each of them renders correctly:
I wonder why and how can I fix it :(
For reference, see also my Markdown on GitHub and my page.
Markdown uses underscores to produce italics, and in systems that don't protect the mathematics from being processed by markdown, that can lead to HTML tags being inserted in the middle of the mathematics. Note that in your screen shot of the un-typeset mathematics, the underscore for 1_n is missing, and the text following it, up to the missing underscore for y_{i1} are in italics. That means Markdown has processed and removed this underscores and inserted tags to produce italics. Since MathJax doesn't process math containing HTML tags, that is why the math isn't being processed. See the MathJax documentation for more details.
One solution is to use \_ rather than _ for the underscores, as that will prevent Markdown from processing them, and they will be sent to MathJax as plain underscores.

Latex table from CSV file

The following code for creating Latex table from csv file is missing horizontal line on top of the table.
\documentclass{article}
\usepackage{csvsimple}
% Make csv in question
\begin{filecontents*}{check.csv}
labels,names,A,C,V,tools
a,example,838,663,683,
b,otter,353,215,192,
d,\textbf{broccoli},79,79,117,
e,fibredensityandcrosssection,1086,849,868,
ad,hcp-prefree:exec-centos7.freebuild-centos4-latest,70,76,157,
ar,shots47s\_fmriprep-1.2.3,,,,453
\end{filecontents*}
\begin{table*}[!ht]
\csvreader[%
tabular={|c|c|c|c|c|c|},
table head = \textbf{Labels} &\textbf{{names}} & \textbf{A} & \textbf{C} & \textbf{V} & \textbf{T}\\\hline,
late after line= \\,
late after last line=\\\hline %
]{check.csv}{labels=\labels,names=\names,A=\A,C=\C,V=\V,tools=\tools}%
{\labels & \names & \A & \C & \V & \tools}
\centering
\caption{\label{table1}Number by category}
\end{table*}
\end{document}
How we can add top horizontal line on top of table?
Please note that building such a data prison is bad style. Have a look at http://betterposters.blogspot.de/2012/08/the-data-prison.html , https://www.inf.ethz.ch/personal/markusp/teaching/guides/guide-tables.pdf or https://wiert.me/2014/04/03/andre-vatter-google-wie-tabellen-eigentlich-aussehen-sollten-%EF%BB%BF/ for some guides about nice looking tables.
\documentclass{article}
\usepackage{csvsimple}
% Make csv in question
\begin{filecontents*}{check.csv}
labels,names,A,C,V,tools
a,example,838,663,683,
b,otter,353,215,192,
d,\textbf{broccoli},79,79,117,
e,fibredensityandcrosssection,1086,849,868,
ad,hcp-prefree:exec-centos7.freebuild-centos4-latest,70,76,157,
ar,shots47s\_fmriprep-1.2.3,,,,453
\end{filecontents*}
\begin{document}
\begin{table*}[!ht]
\csvreader[%
tabular={|c|c|c|c|c|c|},
table head = \hline\textbf{Labels} &\textbf{{names}} & \textbf{A} & \textbf{C} & \textbf{V} & \textbf{T}\\\hline,
late after line= \\,
late after last line=\\\hline %
]{check.csv}{labels=\labels,names=\names,A=\A,C=\C,V=\V,tools=\tools}%
{\labels & \names & \A & \C & \V & \tools}
\centering
\caption{\label{table1}Number by category}
\end{table*}
\end{document}

LISP: how to properly encode a slash ("/") with cl-json?

I have code that uses the cl-json library to add a line, {"main" : "build/electron.js"} to a package.json file:
(let ((package-json-pathname (merge-pathnames *app-pathname* "package.json")))
(let
((new-json (with-open-file (package-json package-json-pathname :direction :input :if-does-not-exist :error)
(let ((decoded-package (json:decode-json package-json)))
(let ((main-entry (assoc :main decoded-package)))
(if (null main-entry)
(push '(:main . "build/electron.js") decoded-package)
(setf (cdr main-entry) "build/electron.js"))
decoded-package)))))
(with-open-file (package-json package-json-pathname :direction :output :if-exists :supersede)
(json:encode-json new-json package-json))
)
)
The code works, but the result has an escaped slash:
"main":"build\/electron.js"
I'm sure this is a simple thing, but no matter which inputs I try -- "//", "/", "#//" -- I still get the escaped slash.
How do I just get a normal slash in my output?
Also, I'm not sure if there's a trivial way for me to get pretty-printed output, or if I need to write a function that does this; right now the output prints the entire package.json file to a single line.
Special characters
The JSON Spec indicates that "Any character may be escaped.", but some of them MUST be escaped: "quotation mark, reverse solidus, and the control characters". The linked section is followed by a grammar that show "solidus" (/) in the list of escaped characters. I don't think it is really important in practice (typically it needs not be escaped), but that may explain why the library escapes this character.
How to avoid escaping
cl-json relies on an internal list of escaped characters named +json-lisp-escaped-chars+, namely:
(defparameter +json-lisp-escaped-chars+
'((#\" . #\")
(#\\ . #\\)
(#\/ . #\/)
(#\b . #\Backspace)
(#\f . #\)
(#\n . #\Newline)
(#\r . #\Return)
(#\t . #\Tab)
(#\u . (4 . 16)))
"Mapping between JSON String escape sequences and Lisp chars.")
The symbol is not exported, but you can still refer to it externally with ::. You can dynamically rebind the parameter around the code that needs to use a different list of escaped characters; for example, you can do as follows:
(let ((cl-json::+json-lisp-escaped-chars+
(remove #\/ cl-json::+json-lisp-escaped-chars+ :key #'car)))
(cl-json:encode-json-plist '("x" "1/5")))
This prints:
{"x":"1/5"}

Haskell print the first line into Browser Tab [duplicate]

This question already has an answer here:
Return the first line of a String in Haskell
(1 answer)
Closed 8 years ago.
Just a simple question, my code is complete. It takes an input file, breaks it into lines, reads the file line by line, does the conversions, which is in this case, turns certain things into HTML format (ex: #This is a line into a line with H1 HTML tags, formatting it into a header). The only thing I have left is to take the First line of code, and print that code into the browser tab. Also, the body, or tail must be printed into the window, not the tab. So the first line of my .txt file is The Title! which I want to show in the tab of the web browser. Here is something I have for that:
formatToHTML :: String -> String
formatToHTML [] = []
formatToHTML x
| head x == --any char = "<title>" ++ head ++ "</title>"
| tail x == --rest of file = "<body>" ++ tail ++ "</tail>"
| otherwise = null
or
formatToHTML :: [String] -> String
formatToHTML = unlines. map (show) "<title>" ++ head ++ </title>" $ lines
I dont want to, or I think even need to use guards here, but I cant think of a shorter way to do my task.
I would call this from my main method before I output my file to html.
Also, I know its a amateur haskell question. but how would I represent any char. Say, I want to say, if the head of x exists, print the head with the title tags. print tail with body tags. Help? Thank You
My guess of what you want is:
formatHtml :: [String] -> String
formatHtml [] = ""
formatHtml (x:xs) = unlines theLines
where theLines = [ "<title>" ++ ...convert x to html... ++ "</title>",
"<body>" ] ++ map toHtml xs ++ [ "</body>" ]
toHtml :: String -> String
toHmtl str = ...converts str to HTML...
Example:
formatHtml [ "the title", "body line 1", "body line2" ]
results in:
<title>the title</title>
<body>
body line 1
body line 2
</body>
You still have to define the toHtml function and decide how to convert the first line to the inner html of the tag.

regex to replace '\\' by <br> but only outside some tags

I am new to regex and have been struggling for a while on this one: I want to transform LaTeX files to HTML.
I use mathjax to render equations and some javascript replace functions to convert the tags. I have nearly finished, but I still have an issue with the line breaks: I need to transform \\ to <br>, but only outside the tags \begin{array} and \end{array}.
Example: in this portion, only the \\ before Montrer l'equivalence should be replaced.
$M=\left(
\begin{array}{c|c}
A &B \\ \hline
C &D \\
\end{array}
\right)$
$in$ $\mathcal{M}_{n}(\mathbb{K})$ avec $A$ $\in$ $\mathcal{M}_{r}(\mathbb{K})$ inversible.\\ Montrer l'equivalence:
\[
\Bigl( rg(A) = rg(M) \Bigr) \Leftrightarrow \Bigl( D = CA^{-1}B \Bigr)
\]
\begin{enumerate}
\item Calculer $detB$ en fontion de $A$.
\item En déduire que $detB \geqslant 0$.
\end{enumerate}
$M=
\left(
\begin{array}{c|c}
A &B \\ \hline
C &D \\
\end{array}
\right)$
How can I do this with regex ?
EDIT: I have found here a handy regex tester...
You can use this pattern in a replace with a callback function that return the first capture group or <br> when it is void:
/(\\begin{array}(?:[^\\]+|\\(?!end{array}))*\\end{array})|\\\\/
The idea is to match \begin{array}...\end{array} before \\ to avoid to find \\ inside \begin{array}...\end{array}.
detail:
(?: # open a non-capturing group
[^\\]+ # all characters but \ 1 or more times
| # OR
\\(?!end{array}) # \ not followed by "end{array}"
)* # close non-capturing group, zero or more times
This structure is more efficient than a simple .*? that need many backtracks to succeed. It's a bit longer but more performant since it avoids lazy quantifiers.
(ps: remove the delimiters / in regexpal)