Recently I have joined a course to relearn some linear algebra. What I didn’t have any intuition during college.
I want to do exercises and also add them to this site for content ๐
I am using hugo-coder theme and it comes with katex support. When never I need to write some math, I just need to
enable math
front matter in that post.
+++
math = true
+++
Hugo will include katex script
tag to the page so any texts between $ ... $
or paragraphs inside $$ ... $$
will be parsed and
rerendered by Katex.
Sounds simple right? But it ain’t that easy.
All Katex functions begin with a \
for example \frac{1}{2}
for $\frac{1}{2}$, \\
for line break. But because I was writing
posts in markdown files, \\
is converted to \
when displayed on page.
For example this is valid katex code, but is not rendered correctly.
$$
\text{hello} \\
\text{this should be on new line}
$$
Because when hugo build markdown file to html, it becomes
<p>$$
\text{hello} \
\text{this should be on new line}
$$</p>
And after that, katex render the block as $$ \text{hello} \ \text{this should be on new line} $$
So after found out the problem, I had to add \\\
to any new line, and I wasn’t happy to do so.
An other problem is when editing in markdown, my editor doesn’t know the code between $
signs need
to be parsed as latex
instead of markdown
. So I don’t have syntax highlighting or autocompletion
features to speed up my typing.
With a programming instinct that scared the whole world, I find a new way to fix them both.
First, I move the code out of markdown files to separate .tex
files. And then, include their raw
content back into the main markdown file.
<!-- mardown file -->
+++
math = true
+++
I do math for fun
$$
{{< include "my-matrix.tex" >}}
$$
% my-matrix.tex file
\begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 1 \\
0 & 0 & 1 \\
\end{bmatrix}
Second, I need to write code for include
function to glue all content together. It’s a hugo shortcode file at layouts/shortcodes/include.html
.
{{ $file := .Get 0 }}
{{ (printf "%s%s" .Page.File.Dir $file) | readFile | replaceRE "^---[\\s\\S]+?---" "" | safeHTML }}
It work flawlessly and I keep doing that up until now. This is result. $$ \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 1 \\ 0 & 0 & 1 \\ \end{bmatrix} $$
The code of matrix.tex
is even available to view or download later ๐