在網頁中寫入數學公式


今天跟大家分享在網頁中使用數學公式。

像這樣的公式大家怎么寫出來呢?

甚至還有更復雜的:

以前有一種很常見的做法,就是利用數學公式生成器生成一張圖片,然后將圖片嵌在網頁里。

就是先用這個數學公式生成器生成公式的圖片,然后在將該img的style屬性改成  style="padding:0px;border: none;margin:0px;vertical-align: bottom;"   插入到網頁中就可以了。

這種方法可以利用阮一峰的數學公式生成器。具體做法去他的博客看,我就不搬運了:阮一峰數學生成器博客(點擊我)。

但是,有時候我們不想在網頁中嵌入圖片,就是不用圖片這種方法怎么去實現呢?答案是利用Mahtjax這個插件。首先上官網:Mathjax官網,然后是github的資料:GitHub,最后還有一個英文文檔:文檔

這些都是我查找的資料,我說一下簡單的使用方法。

第一步先是引入:你可以通過引入CDN,也可以下載js引入,個人推薦CDN引入。

<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"> </script>

上述的configure支持了TeX和MathLL語法,如果需要支持AsciiMath語法,將configure改為“?config=TeX-AMS=MML_HTMLorMML”

第二步:有3種使用方法:
①:
TeX and LaTeX格式方式
默認運算符是$$...$$和\[... \]為顯示數學,\(...\)用於在線數學。請特別注意,在$...$在線分隔符不使用默認值。這是因為,美元符號出現常常在非數學設置,這可能會導致一些文本被視為意外數學。例如,對於單元分隔符,“...的費用是為第一個$2.50和$2.00每增加一...”將導致短語“2.50的第一個,和”被視為數學因為它屬於美元符號之間。注意HTML的標簽與TeX語法可能有沖突,“小於號/大於號/ampersands&”需要前后空格,比如:$$a < b$$
為了防止出錯,加上一段js代碼,例子如下:
<!DOCTYPE html>
<html>
<head>
<title>MathJax TeX Test Page</title>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript"
  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
</head>
<body>
When $a \ne 0$, there are two solutions to \(ax^2 + bx + c = 0\) and they are
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$
</body>
</html>

 ②:第二種方法:添加MathML中等標簽的形式

 

<!DOCTYPE html>
<html>
<head>
<title>MathJax MathML Test Page</title>
<script type="text/javascript"
  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
</head>
<body>

<p>
When
<math xmlns="http://www.w3.org/1998/Math/MathML">
  <mi>a</mi><mo>&#x2260;</mo><mn>0</mn>
</math>,
there are two solutions to
<math xmlns="http://www.w3.org/1998/Math/MathML">
  <mi>a</mi><msup><mi>x</mi><mn>2</mn></msup>
  <mo>+</mo> <mi>b</mi><mi>x</mi>
  <mo>+</mo> <mi>c</mi> <mo>=</mo> <mn>0</mn>
</math>
and they are
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">
  <mi>x</mi> <mo>=</mo>
  <mrow>
    <mfrac>
      <mrow>
        <mo>&#x2212;</mo>
        <mi>b</mi>
        <mo>&#x00B1;</mo>
        <msqrt>
          <msup><mi>b</mi><mn>2</mn></msup>
          <mo>&#x2212;</mo>
          <mn>4</mn><mi>a</mi><mi>c</mi>
        </msqrt>
      </mrow>
      <mrow> <mn>2</mn><mi>a</mi> </mrow>
    </mfrac>
  </mrow>
  <mtext>.</mtext>
</math>
</p>

</body>
</html>

 ③第三種:AsciiMath輸入。以``為符號。

<!DOCTYPE html>
<html>
<head>
<title>MathJax AsciiMath Test Page</title>
<script type="text/javascript"
  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=AM_HTMLorMML-full"></script>
</head>
<body>

<p>When `a != 0`, there are two solutions to `ax^2 + bx + c = 0` and
they are</p>
<p style="text-align:center">
  `x = (-b +- sqrt(b^2-4ac))/(2a) .`
</p>

</body>
</html>

運行結果都是這樣子的

DONE


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM