0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GithubPagesでMathJaxを通してLatexをレンダリングする方法

0
Posted at

こんにちは、本記事ではGithubPagesでlatexをどうやってレンダリングできるかを説明したいと思います。

問題

以下のマークダウン(Markdown)コードが含むGithubPagesのポストを考えてみよう。

---
layout: post
title: "組合せ数学"
date: 2026-10-31
categories: 数学
---

# 方式

$$
\begin{equation}
\binom{n}{k} = \frac{n!}{k!(n-k)!}
\end{equation}
$$

このコードがリーモートオリジンのメイン(origin main)にマージ(merge)され、GithubPagesの新しいデプロイメントが完了した後、実際にサイトでポストを見れば次のイメージに似たものが見られるでしょう。

Skärmavbild 2026-11-01 kl. 13.52.06.png

見ての通り全然Latexがレンダリングされてませんよね。これを処理しましょう。

解決

例のポストに、以下の様にJavaScriptコードを追加しましょう。
これは、ポストを読み込む時にMathJaxを実行し、latexレンダリングをするため一行のコードです。

---
layout: post
title: "組合せ数学"
date: 2026-10-31
categories: 数学
---
<script defer src="https://cdn.jsdelivr.net/npm/mathjax@4/tex-mml-chtml.js"></script>

# 方式

$$
\begin{equation}
\binom{n}{k} = \frac{n!}{k!(n-k)!}
\end{equation}
$$

上記の場合は CDN(Content Delivery Network)を通してMathJaxソフトウェアをダウンロードされることになっています。これによって、サイトが実行しているサーバーにMathJaxをインストールする必要がなくなるので便利な仕組みです。

結果

「解決」節の記述の様にマークダウンを微調整してから又、リーモートメインのマージによる新しいデプロイメントが完了してポストを確認したら、以下の様に見えます

Skärmavbild 2026-11-01 kl. 15.42.07.png

綺麗にレンダリングなってますね。

つづき

この節の内容は問題解決じゃなく、改善点に集中します。

方式ナンバーを付ける

普通のLatexドキュメントの様に、方式の横にナンバーを付けたかったら以下のコードを使ってください

---
layout: post
title: "組合せ数学"
date: 2026-10-31
categories: 数学
---
<script defer src="https://cdn.jsdelivr.net/npm/mathjax@4/tex-mml-chtml.js"></script>
<script> 
    window.MathJax = {
        tex: {
        tags: 'ams' // This gives equations numbers
       }
    };
</script>

# 方式

$$
\begin{equation}
\binom{n}{k} = \frac{n!}{k!(n-k)!}
\end{equation}
$$

再びレンダリングしたらこんな風に見えます。

Skärmavbild 2026-11-03 kl. 21.21.21.png

美しい。

Jekyllの_layoutフォルダーを使う

latexが必要のポストを作る度に上記のJavascriptコードを入力するのが面倒だと思ったら、Jekyllの_layoutフォルダーを使うのがおすすめです。

GithubPagesが使用しているリポジトリのルーツで『_layout』と名付けたフォルダーを作ってください。
つづいて、そのフォルダーの中に「mylayout.html」と名付けたファイルを作ってください。

mylayout.htmlに下記を入力してください

<!DOCTYPE html>
<html>

<body>
    <h1>  {{ page.title }} </h1>
    
    <script defer src="https://cdn.jsdelivr.net/npm/mathjax@4/tex-mml-chtml.js"></script>
    <script> 
        window.MathJax = {
            tex: {
            tags: 'ams' // This gives equations numbers
           }
        };
    </script>
    
    {{ content }}

</body>
</html>

上に作った非常にシンプルなレイアウトを使うにはポストのマークダウンフロントマター(Frontmatter)を調整しなければなりません。

下の様にFrontmatterのlayout変数を微調整して、Javascriptコードの部分を削除してください。

---
layout: mylayout
title: "組合せ数学"
date: 2026-10-31
categories: 数学
---

# 方式

$$
\begin{equation}
\binom{n}{k} = \frac{n!}{k!(n-k)!}
\end{equation}
$$

では、再びレンダリングをしたらどうなるでしょうか。

Skärmavbild 2026-11-03 kl. 21.59.05.png

任務完了ですね。

テーマを保ったままJavascriptコードをlayoutに移す

前の例では、完全に新しいlayoutを使った為テーマが消えましたね。latexレンダリングをするJavascriptコードは移せましたけど、結果はミニマリストのレトロhtmlサイトでした。

自分はJekyllテーマとしてMinimaを使っています。ポストのFrontmatterのlayout変数はpostに定義してます。従って、ポストレイアウトはMinimaのpost.htmlを使ってます。では、mylayout.htmlの様にMinimaのpost.htmlにJavascriptコードを移せば、テーマを保ってlatexがレンダリングされる様子ですね。

テーマのpost.htmlを見つける

GithubPagesが使っているテーマは「_config.yml」に書いてあります。
自分はMinimaの1e8a445コミットを使っています。

Githubを探せば簡単に求めてるファイルが見つかります:post.html

テーマのpost.htmlをオーバーライドする

GithubPagesリポジトリの_layoutフォルダーの中にGithubで見つけたpost.htmlの内容を一緒の名前のファイルへコピーしてください。

コピーしたファイルの内容にlatexレンダリングを可能するJavascriptコードを入力する。
htmlが詳しく無いので適当な場所に入力しました。<header>タッグが終わる行に入れました。

最終的には以下の様な内容になると考えます。

---
layout: base
---

<!-- This is a customized version of minimas own post.html from commit: 1e8a445. -->
<!-- I did the customization to so that I can have Latex rendered using Mathjax -->

<article class="post h-entry" itemscope itemtype="http://schema.org/BlogPosting">

  <header class="post-header">
    <h1 class="post-title p-name" itemprop="name headline">{{ page.title | escape }}</h1>
    <div class="post-meta">
      {%- assign date_format = site.minima.date_format | default: "%b %-d, %Y" -%}
      {% assign pdate = page.date | date_to_xmlschema %}
      {%- if page.modified_date %}<span class="meta-label">Published:</span>{% endif %}
      <time class="dt-published" datetime="{{ pdate }}" itemprop="datePublished">
        {{ page.date | date: date_format }}
      </time>
      {%- if page.modified_date -%}
        <span class="bullet-divider">•</span>
        <span class="meta-label">Updated:</span>
        {%- assign mdate = page.modified_date | date_to_xmlschema %}
        <time class="dt-modified" datetime="{{ mdate }}" itemprop="dateModified">
          {{ mdate | date: date_format }}
        </time>
      {%- endif -%}
      {%- if page.author %}
      <div class="{% unless page.modified_date %}force-inline {% endunless %}post-authors">
        {%- for author in page.author %}
          <span itemprop="author" itemscope itemtype="http://schema.org/Person">
            <span class="p-author h-card" itemprop="name">{{ author }}</span></span>
          {%- if forloop.last == false %}, {% endif -%}
        {% endfor %}
      </div>
      {%- endif %}
    </div>
  </header>
  <script defer src="https://cdn.jsdelivr.net/npm/mathjax@4/tex-mml-chtml.js"></script>
  <script> 
        window.MathJax = {
            tex: {
            tags: 'ams' // This gives equations numbers
           }
        };
  </script>
  <div class="post-content e-content" itemprop="articleBody">
    {{ content }}
  </div>

  {%- if site.disqus.shortname -%}
    {%- include disqus_comments.html -%}
  {%- endif -%}

  <a class="u-url" href="{{ page.url | relative_url }}" hidden></a>
</article>

注意点
上記ファイルのFrontmatterのlayout変数がbaseに指している為、Githubからbase.htmlもpost.htmlみたいにコピーする必要があります。
最終的にこんな設定の様になります。

..
├── _layouts
│   ├── base.html
│   └── post.html
├── _posts
│   ├── 2026-10-31-組合せ数学.md
..  ..

この、設定でポストをレンダリングをしたら以下の様に見えます。

Skärmavbild 2026-11-03 kl. 21.21.21.png

素晴らしい。

参照

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?