Latex

grammer

多行

多行公式前后要用\begin{aligned}\end{aligned}
在要对齐的符号钱加上&

\begin{aligned}
\sum_{k=1}^{500000}a_kp_k &= \sum_{k=1}^{500000}\left( a_k \cdot \frac{1}{5000} \right) \\
&= \frac{1}{500000}\cdot\sum_{k=1}^{500000}a_k \\
&= \frac{1}{500000}\cdot(1\cdot 999995 + 10 \cdot 995 + 10000 \cdot 5 + 488989 \cdot (-5)) \\
&= -1.78
\end{aligned}

效果:
\(\begin{aligned} \sum_{k=1}^{500000}a_kp_k &= \sum_{k=1}^{500000}\left( a_k \cdot \frac{1}{5000} \right) \\ &= \frac{1}{500000}\cdot\sum_{k=1}^{500000}a_k \\ &= \frac{1}{500000}\cdot(1\cdot 999995 + 10 \cdot 995 + 10000 \cdot 5 + 488989 \cdot (-5)) \\ &= -1.78 \end{aligned}\)

单边大括号多行

f(n) = \left\{
  \begin{array}{ll}
  f(n-1) \cdot a &if\ n \gt 0 \\
  1 &if\ n = 0
  \end{array}
\right.

效果: \(f(n) = \left\{ \begin{array}{ll} f(n-1) \cdot a &if\ n \gt 0 \\ 1 &if\ n = 0 \end{array} \right.\)

writing latex document in visual studio code

  1. install MacTex
    brew install --cask mactex-no-gui
    

    if install fail, probably there is not enough space in your computer.

  2. setting PATH environment variable
    add the directory of mactex binaries to the ~/.zshrc, for example:
    export PATH=/usr/local/texlive/2022/bin/universal-darwin:$PATH
  3. update latex packges
    sudo tlmgr update --self && sudo tlmgr update --all
    
  4. open/restart visual studio code if you install maxtex and set PATH environment variable during visual studio code is opening, please restart it.
  5. install LaTex Workshop extension

then you can build *.tex file to PDF file and preview it in visual studio code.

if you write with chinese, follow the steps below:

  1. add the following to the settings.json:
    "latex-workshop.latex.tools": [
       {
          "name": "latexmk",
          "command": "latexmk",
          "args": [
          "-synctex=1",
          "-interaction=nonstopmode",
          "-file-line-error",
          "-pdf",
          "%DOC%"
          ]
       },
       {
          "name": "xelatex",
          "command": "xelatex",
          "args": [
          "-synctex=1",
          "-interaction=nonstopmode",
          "-file-line-error",
          "%DOC%"
          ]
       },
       {
          "name": "pdflatex",
          "command": "pdflatex",
          "args": [
          "-synctex=1",
          "-interaction=nonstopmode",
          "-file-line-error",
          "%DOC%"
          ]
       },
       {
          "name": "bibtex",
          "command": "bibtex",
          "args": [
          "%DOCFILE%"
          ]
       }
    ],
    "latex-workshop.latex.recipes": [
       {
          "name": "xelatex",
          "tools": [
          "xelatex"
          ]
       },
       {
          "name": "latexmk",
          "tools": [
          "latexmk"
          ]
       },
       {
          "name": "pdflatex -> bibtex -> pdflatex*2",
          "tools": [
          "pdflatex",
          "bibtex",
          "pdflatex",
          "pdflatex"
          ]
       }
    ],
    "latex-workshop.view.pdf.viewer": "tab",
    "latex-workshop.latex.clean.fileTypes": [
       "*.aux",
       "*.bbl",
       "*.blg",
       "*.idx",
       "*.ind",
       "*.lof",
       "*.lot",
       "*.out",
       "*.toc",
       "*.acn",
       "*.acr",
       "*.alg",
       "*.glg",
       "*.glo",
       "*.gls",
       "*.ist",
       "*.fls",
       "*.log",
       "*.fdb_latexmk"
    ]
    
  2. add package in your .tex file:
    \usepackage[UTF8]{ctex}