<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Hugo on Mustafa Albazy</title><link>https://mustafa.page/tags/hugo/</link><description>Recent content in Hugo on Mustafa Albazy</description><image><title>Mustafa Albazy</title><url>https://mustafa.page/mustafa-profile-pic-compressed.jpg</url><link>https://mustafa.page/mustafa-profile-pic-compressed.jpg</link></image><generator>Hugo -- 0.150.1</generator><language>en</language><lastBuildDate>Wed, 21 Jun 2023 00:00:00 +0000</lastBuildDate><atom:link href="https://mustafa.page/tags/hugo/index.xml" rel="self" type="application/rss+xml"/><item><title>Math Latex Rendering Using Katex Library for Hugo Themes</title><link>https://mustafa.page/posts/2023-06-21-hugo-math-papermod/</link><pubDate>Wed, 21 Jun 2023 00:00:00 +0000</pubDate><guid>https://mustafa.page/posts/2023-06-21-hugo-math-papermod/</guid><description>&lt;p&gt;As I&amp;rsquo;m using Hugo static site generator with PaperMod theme, I noticed that the Latex math equations in the markdown content/posts files do not render as expected.&lt;/p&gt;
&lt;p&gt;After some digging in the theme source code I found out there&amp;rsquo;s no Latex based library being called out during the site building process.&lt;/p&gt;
&lt;p&gt;Which explains why something like the below doesn&amp;rsquo;t render:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;$$\sum_{n=1}^{k} \frac{1}{n^2} = \frac{\pi^2}{6}$$&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;When in a perfect world it supposed to render like:&lt;/p&gt;</description><content:encoded><![CDATA[<p>As I&rsquo;m using Hugo static site generator with PaperMod theme, I noticed that the Latex math equations in the markdown content/posts files do not render as expected.</p>
<p>After some digging in the theme source code I found out there&rsquo;s no Latex based library being called out during the site building process.</p>
<p>Which explains why something like the below doesn&rsquo;t render:</p>
<p><code>$$\sum_{n=1}^{k} \frac{1}{n^2} = \frac{\pi^2}{6}$$</code></p>
<p>When in a perfect world it supposed to render like:</p>
<p>$$\sum_{n=1}^{k} \frac{1}{n^2} = \frac{\pi^2}{6}$$</p>
<p>To solve this you have to include a Latex parsing library into your Hugo theme.</p>
<h3 id="latexkatex">Latex/Katex</h3>
<p>I have used Katex, which is a lightweight javascript library that can parse and render Latex math equations, it&rsquo;s mainly designed to be used for online web pages with focus on performance.</p>
<p>You could read more about Katex on <a href="https://katex.org">https://katex.org</a>.</p>
<h3 id="solution">Solution</h3>
<p>Depending on the theme you are using you have to include the following code block into the theme.</p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-html" data-lang="html"><span style="display:flex;"><span>{{ if or .Params.math .Site.Params.math }}
</span></span><span style="display:flex;"><span>&lt;<span style="color:#7ee787">link</span> rel<span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;stylesheet&#34;</span> href<span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;https://cdn.jsdelivr.net/npm/katex@0.16.7/dist/katex.min.css&#34;</span> crossorigin<span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;anonymous&#34;</span>&gt;
</span></span><span style="display:flex;"><span>&lt;<span style="color:#7ee787">script</span> defer src<span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;https://cdn.jsdelivr.net/npm/katex@0.16.7/dist/katex.min.js&#34;</span> crossorigin<span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;anonymous&#34;</span>&gt;&lt;/<span style="color:#7ee787">script</span>&gt;
</span></span><span style="display:flex;"><span>&lt;<span style="color:#7ee787">script</span> defer src<span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;https://cdn.jsdelivr.net/npm/katex@0.16.7/dist/contrib/auto-render.min.js&#34;</span> crossorigin<span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;anonymous&#34;</span>
</span></span><span style="display:flex;"><span>    onload<span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;renderMathInElement(document.body);&#34;</span>&gt;&lt;/<span style="color:#7ee787">script</span>&gt;
</span></span><span style="display:flex;"><span>    &lt;<span style="color:#7ee787">script</span>&gt;
</span></span><span style="display:flex;"><span>        document.addEventListener(<span style="color:#a5d6ff">&#34;DOMContentLoaded&#34;</span>, <span style="color:#ff7b72">function</span>() {
</span></span><span style="display:flex;"><span>            renderMathInElement(document.body, {
</span></span><span style="display:flex;"><span>                delimiters<span style="color:#ff7b72;font-weight:bold">:</span> [
</span></span><span style="display:flex;"><span>                    {left<span style="color:#ff7b72;font-weight:bold">:</span> <span style="color:#a5d6ff">&#34;$$&#34;</span>, right<span style="color:#ff7b72;font-weight:bold">:</span> <span style="color:#a5d6ff">&#34;$$&#34;</span>, display<span style="color:#ff7b72;font-weight:bold">:</span> <span style="color:#79c0ff">true</span>},
</span></span><span style="display:flex;"><span>                    {left<span style="color:#ff7b72;font-weight:bold">:</span> <span style="color:#a5d6ff">&#34;$&#34;</span>, right<span style="color:#ff7b72;font-weight:bold">:</span> <span style="color:#a5d6ff">&#34;$&#34;</span>, display<span style="color:#ff7b72;font-weight:bold">:</span> <span style="color:#79c0ff">false</span>}
</span></span><span style="display:flex;"><span>                ]
</span></span><span style="display:flex;"><span>            });
</span></span><span style="display:flex;"><span>        });
</span></span><span style="display:flex;"><span>    &lt;/<span style="color:#7ee787">script</span>&gt;
</span></span><span style="display:flex;"><span>{{ end }}
</span></span></code></pre></div><p>You could create a <code>math.html</code> file in the <em>partials</em> folder and then call that file into your header.</p>
<p>Or in my case, using the PaperMod theme, I&rsquo;ve inserted this code into <code>partials/extend_head.html</code> which is being called into the <code>partials/head.html</code> file.</p>
<p>Now this included library is in <em>if condition</em>, so to enable/disable the library you have two options.</p>
<p>Add <code>math: true</code> in your config file under the <em>params</em>. And this can enable or disable the library on the whole site.</p>
<p>However, if you enable it that way, then you will end up calling the library on every post regardless if there are Latex equations or not in the content. And this can add unnecessary content to download while browsing the site.</p>
<p>So in my experience the best way is to enable it only on the posts that contains Latex equations, and disable it on everything else.</p>
<p>To do so, add <code>math: false</code> In the frontmatter of all your posts/markdown files. And on the posts that have Latex equations you&rsquo;ll have to add it as <code>math: true</code>.</p>
]]></content:encoded></item></channel></rss>