Nowadays, many technical positions, such as big data, artificial intelligence and data analysis, have certain requirements on mathematics. Mathematics is becoming more and more important, and many people start to try to learn mathematics. Suddenly, the idea of learning mathematics with Python programming comes to their mind. After searching a lot of relevant information on the Internet, I found a lot of good attempts, but there was little information in this field in China, so I thought of opening a technology column.

Practice makes a set of mathematical formulae that facilitate the spread of the Internet to share cross-platform editor, cross environment show that is necessary, if still stay in the era of Word or PDF, that mathematics is restricted in the document or pictures and cannot be spread by one of the most popular web manner, and in the PDF file processing software such as Word, mathematical formula editor is troublesome, And most importantly, there is a disconnect from programming.

Edit and display mathematical formulas

I think Markdown and LaTex Math Math are the best way to document your learning experience (especially mathematical formulas) and display it on zhihu, Jianshu, wechat official accounts and web pages.

Although many academic mathematical papers are edited directly using LaTex syntax, as in Markdown, careful comparison shows that the entire document is written directly using LaTex syntax. The effect is not much different from the Markdown + LaTex Math approach. But LaTex’s syntax, editor, configuration, Chinese language support, and so on are much more complex than Markdown’s, and it’s not as sophisticated as Markdown’s already mature ecosystem (toolchain, community, etc.).

There are many editors in Markdown, and for many beginners, I personally recommend using VS Code.

  • First, VS Code is more convenient to Chinese, if you want more people to learn to use Python to learn math, it is important to have a Chinese interface. VS Code is cross-platform, both Mac and Windows.
  • VS Code is an excellent Code editor. It’s one of the most recommended editors out there (probably not one). To learn math in Python, you can’t use pure Markdown. A Code editor is essential. VS Code is extremely comfortable for both Python and Markdown.
  • Third, VS Code is rich in plug-ins. The tool chain of Python compilation, Markdown writing and preview, LaTex Math display and so on is quite complete. For example, you only need to install the Python Extension Pack, which contains Microsoft official Python, MagicPython, Jupyter and other excellent plug-ins. For the Markdown plugin, it is recommended to install Markdown All in One and Markdown+Math.

Syntax for LaTex Math

The LaTex Math Math has many and complicated grammars. It is impossible to remember all of them, and it is not recommended to do so. I even failed to remember all the Markdown grammars. For example, I have a grammar manual from Rice University and a LaTex Math online PDF manual from Rice University.

Of course, VS Code with the above plug-in installed also has a LaTex Math syntax prompt. Use the English character backslash \ to prompt you. Let’s use the LaTex Math syntax in this PDF in Markdown and give a brief explanation of how to use it.

For the Greek letters, use the $LaTex Greek letter syntax $, that is, surround the Greek letter syntax for LaTex with two dollar signs. For example:

$\ Gamma $, $\ iota $, $\ sigma $, $\ phi $, $\ upsilon $, $\ Pi $$\ Bbbk $, $\ heartsuit $$$, $\ oint $\ int
Copy the code

This is what the output looks like

,,,,,,,,,

It is worth noting that Greek letters are uppercase and lowercase, and this case is controlled by whether the first letter of LaTex is uppercase or lowercase.

The syntax for trigonometric functions, logarithms, exponential trigonometric functions, logarithms, exponentials, and other symbols is the same as the letters. It also uses the $LaTex symbol $, which means to enclose the Greek grammar for LaTex with two dollar signs. Let’s take a look at this example:

$\ tan $, $\ sin $, $\ cos $$$, $\ \ lg arcsin $, $\ arctan $, $\ min $, $\ Max $$$, $\ log $\ exp
Copy the code

The output is as follows:

,,,,,,,,,

The same thing happens with the operators, but the thing to notice is that the plus sign, the minus sign, the equal sign, the greater than sign, the less than sign are a little bit different, you just use the sign, so you have to notice that.

+ $$, $$, $= $$$, $> < $, $\ times $, $\ div $, $\ equiv $, $\ leq $$$, $\ neq $\ geq  
Copy the code

The output is:

,,,,,,,,,,

Set symbol Set is the knowledge that high school mathematics will learn, is also a very important basic concept, there are many symbols of set, so I will specially list them, although their grammar is the same as above.

$\ CPU $, $$, $\ \ cap in $, $\ notin $, $\ ni $, $\ subset $, $\ subseteq $, $\ supset $$$, $\ infty $\ supseteq
Copy the code

The output is:,,,,,,,,,

There are a lot of symbols in mathematics. Here are just a few of the symbols that are commonly used. More details can be found in the RICE University PDF.

Mathematical formulas with Markdown Math

We have only introduced a single mathematical symbol display problem, but a complete mathematical formula can contain multiple mathematical symbols and numbers. Before introducing the mathematical formula, we need to understand the concept of inlining and block first. Inline output versus block output we used two dollar signs for every sign we printed, right? This is called inlining, which means that we can embed mathematical symbols into paragraphs of text, such as:

$f(x)=\frac{P(x)}{Q(x)}$  
Copy the code

Functional:We can see the formula in the text paragraph.

If we need to output a complex mathematical formula, or if we need to protrude and display the formula independently, then we need to use the block output of the formula. The block output syntax uses four dollar signs? Mathematical formulas?Let’s look at the case.

? f(x)=\frac{P(x)}{Q(x)}? 
Copy the code

With block output, the function is centered, and it’s worth noting that when we use block output mathematical formulas, we need newlines in Markdown to write the formulas.

Four simple operations let’s look at four simple operations that we can write with Markdown Math,

$2x - 5y =  8$  
$3x + 9y =  - 12$
$7x \times 2y \neq 3z$
Copy the code

Notice here\timesIs the multiplication sign.\neqIs not equal to, the output effect is as follows:





The exponential output of Markdown Math is ^. This is a bitwise operator in Python. The exponential operator in Python is **.

$x^3+x^9$  
$x^y$  
Copy the code

The output is:



SQRT [n]{} = SQRT [n]{} = SQRT [n]{} = SQRT [n]{} = SQRT [n]{} = SQRT [n]{}

$\sqrt{3x-1}+\sqrt[5]{2y^5-4}$
Copy the code

The output is:

Trigonometric formulas are often mixed with parentheses, letters, symbols, and operators, so be careful when writing them. Let’s look at the following example:

? \cos (2\theta) = \cos^2 \theta - \sin^2 \theta?
Copy the code

The output is as follows:

The syntax for writing a fraction with a numerator and a denominator is \frac{numerator}{denominator}, enclosing the numerator and denominator with curly braces.

? \frac{x}{2y} +\frac{x-y}{x+y} ?
Copy the code

The output is as follows:

The summing formula is complicated, it involves both upper indices and lower indices, so when we print the exponent ^ we can think of it as the upper index, and use _ to print the lower index. Let’s look at a specific example:

? \sum_{n=1}^\infty k?
Copy the code

The output is as follows:

The output of the limit after we understand the concept of upper and lower indices, the output limit is going to use lower indices,

? \lim\limits_{x \to \infty} \exp(-x) = 0?
Copy the code

Output results:

The output of factorial

? \frac{n! }{k! (n-k)! } = \binom{n}{k}?Copy the code

The output is as follows:

The Markdown Math output matrix is surrounded by \begin{matrix} and \end{matrix}, with $Spaces between them and \\ line breaks.

?
  \begin{matrix}
   1 & 2 & 3 \\
   4 & 5 & 6 \\
   7 & 8 & 9
  \end{matrix} 
?
Copy the code

The output is:

Complex mathematical formula

It is very complicated to write piecewise functions. In this case, we will use the cases syntax of LaTex. Just surround it with \begin{cases} and \end{cases}, and use \\ in the middle to segment.

?
X(m,n)=
\begin{cases}
x(n),\\
x(n-1)\\
x(n-1)
\end{cases}
?
Copy the code

The output of the piecewise function is as follows:

This is the basic use of Markdown Math, which is a mathematical formula for LaTex grammar output, and we will look at it later as we learn about it.

Cross-platform display of Markdown Math

Mathematical formulas can be gracefully displayed on we-media platforms such as Zhihu, Jianshu and public accounts, as well as on web pages and blog system WordPress, so as to facilitate the reading and dissemination of mathematical formulas. The Markdown editor supports both Markdown syntax and Markdown Math syntax perfectly. You can just copy and paste the contents of a Markdown file written in VS Code and make some simple changes. As for the insert of zhihu’s built-in mathematical formula, if it is directly imported into the Markdown file, it will show some problems. The mathematical formula needs to be written again with the Tex editor of Zhihu. Delete it.

Since we can replace Markdown typography with HTML instead of Markdown, we just need to focus on how to display the math on the page. The perfect solution is to use MathJax. we just need to insert MathJAXJS inside the tag. For example, in the following example, you just need to copy the following code and save it as an HTML file, but note that the inline syntax is a little different, instead of $sign and formula $, it is: \(symbol and formula \)


      
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <script type="text/javascript" async
  src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML" async>
</script>
</head>
<body>
<p>When \ [a \ 0 ne \], \ [ax ^ 2 + bx + c = 0 \) will have two solutions, they are:? x = {-b \pm \sqrt{b^2-4ac} \over 2a}.?</p>
</body>
</html>
Copy the code

A mathematical formula on public display WeChat public closed and wonderful work, beautify WeChat public issue of typography while using HTML and CSS syntax, but there are many places need to be aware of, so layout is also relatively more troublesome, compared to zhihu, Jane books since the media platform, such as public layout also can be more beautiful. However, it is difficult to display mathematical formulas on official accounts. Wechat official accounts do not support LaTex grammar, so you need to make the formulas into pictures. Other we-media platforms that do not support LaTex can also do this.

[Md2All]md.aclickall.com/, you can directly paste the Markdown document edited by VS Code into it, but also can be beautifully typesetted, but this method has two problems, one is the generated picture is fuzzy, unsightly; Secondly, if there are too many mathematical formulas, the CDN of Qiniuyun needs to be used, and the configuration method is relatively simple. Generally speaking, Md2All has the best comprehensive effect. Compared with other methods, it is more convenient, and it can also compose Markdown documents and mathematical formulas in an elegant public account.