Setting up LaTeX to show on Hugo site.
I’ve been interested in LaTeX for years, and never really had a use case beyond a few simple thing, such as writing a pamphlet/leaflet, or my resume. When I found out that you could get LaTeX, specifically MathTeX/KaTeX to show in a hugo website. I figured i’d give it a try and see how well it works. I’m going to use Katex as i’ve read it can be implemented a little faster than standard MathJaX.
Setup⌗
There are a few simple steps that need to take place for LaTeX to integrate with you’re Hugo Site.
- Set up a KaTeX partial.
- Reference it somewhere it can be seen everywhere.
- Active the partial.
Create the Partial.⌗
- Its best to set up a KaTex partial to access everywhere on your site. We will call
this
katex.htmland we will store this where your other partials are store. In my example these are atmywebsitename/layouts/partials/helpers/. - You will need to import the required files. these can be found from the KaTeX site. You should add the following to the katex.html file you just made. Make sure this matches what ever information is available on the KaTeX documentation
Integrate this into your website.⌗
Its important to integrate this into something that is commonly accessed in all
of your site. the header or footer or head are perfect places.
- Create a
footer.htmlorheader.htmlorhead.htmlin the same partials folder atlayouts/patrialsif these arent already made, if they are edit as needed. Note if doing this in afooter.htmlthis will overide thefooter.htmlprovided by the theme you are using. - Add the following to your prefered partial. I will be adding this to my
head.html:
- Now that this is referenced you should be able to reference the use of KaTeX for each of mark down files seperatly. This makes it so you don’t have to use KaTeX JS and CSS file for posts that it’s not needed in.
Using the Katex Partial⌗
Now we can start using the KaTeX partial in pages that need it. All we need to do is add the following to the front matter of any markdown file on our site.
partial
Now this should allow the KaTeX partial to be used on any page with that in the front matter.
The following: $$\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}$$ will
display as this:
If you wanted to have inline equations you should add the following to your partial:
This will provide inline equations with single dollar signs.
Now you can add in large extravagent math equations to your hearts fancy: