Skip to content

Instantly share code, notes, and snippets.

@7shi
Last active February 4, 2023 18:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 7shi/583d525bdd904599de36587a7c88a324 to your computer and use it in GitHub Desktop.
Save 7shi/583d525bdd904599de36587a7c88a324 to your computer and use it in GitHub Desktop.
[はてなブログ] KaTeXのテスト
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.4/dist/katex.min.css" integrity="sha384-vKruj+a13U8yHIkAyGgK1J3ArTLzrFGBbBc0tDp4ad/EyewESeXE/Iv67Aj8gKZ0" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.4/dist/katex.min.js" integrity="sha384-PwRUT/YqbnEjkZO0zZxNqcxACrXe+j766U2amXcgMg5457rve2Y7I6ZJSm2A0mS4" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.4/dist/contrib/auto-render.min.js" integrity="sha384-+VBxd3r6XgURycqtZ117nYw44OOcIax56Z4dCRWbxyPt0Koah1uHoK0o4+/RRE05" crossorigin="anonymous"></script>
<script>
document.addEventListener("DOMContentLoaded", () => {
function render(src, elem, displayMode) {
if (elem.rendered) return;
try {
katex.render(src, elem, { displayMode });
} catch (ex) {
elem.textContent = ex.message;
}
elem.rendered = true;
}
for (const elem of Array.from(document.getElementsByClassName("math-k"))) {
const t = elem.textContent;
const div = document.createElement("div");
render(elem.textContent, div, true);
elem.parentNode.replaceChild(div, elem);
}
for (const elem of Array.from(document.getElementsByTagName("code"))) {
const t = elem.textContent;
if (t.length > 2 && t.startsWith("$") && t.endsWith("$")) {
const span = document.createElement("span");
render(t.substring(1, t.length - 1), span, false);
elem.parentNode.replaceChild(span, elem);
}
}
});
</script>
行内の数式は `` `$`` ~ ``$` `` で囲みます。
【例】`$x^2=y^2+z^2$` を `$x$` について解けば `$x=\pm\sqrt{y^2+z^2}$` です。
独立した数式は ` ```math-k` ~ ` ``` ` で囲みます。
```math-k
x=\frac{-b±\sqrt{b^2-4ac}}{2a}
```
@7shi
Copy link
Author

7shi commented Feb 5, 2020

説明記事

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment