Web Develop/Frameworks and Libraries
[NuxtJS] MathJax 수식 적용
RuTel
2023. 6. 12. 17:43
728x90
MathJax는 웹 브라우저에 수학 표기법을 표시하기 위한 JS Library이며 아래와 같은 형식을 따른다.
\(-{1 \over {500}}x + 212\)
프론트엔드에서 위 MathJax 수식을 올바르게 보여주기 위해 아래 과정이 필요하다.
1. nuxt.config.js의 head에 스크립트 코드 추가
// Global page headers: https://go.nuxtjs.dev/config-head
head: {
( ... )
script: [
{
src: 'https://polyfill.io/v3/polyfill.min.js?features=es6',
type: 'text/javascript',
body: true
},
{
src: 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js',
type: 'text/javascript',
body: true
}
]
},
2. 프론트 페이지의 mounted에 아래 코드 추가
async mounted() {
// mathjax 수식 렌더링 적용
if (window.MathJax) {
window.MathJax.typeset();
}
}
그러면 아래와 같은 html 요소가 있을 때 수식이 제대로 보여지게 된다.
<p v-html="math_exp"></p>
data() {
return {
math_exp : "<div class="math"> <p>The graph of \(y = 3x - 5\) in the \(xy\)-plane is a line. What is the slope of the line?</p></div>"
}