2010年3月7日 星期日

[工具] 最簡單的 Latex 語法

image

(LyX)

使用 LyX 你幾乎可以像 Word 一樣, 簡單地產生所要的 Latex 文件並且產生高品質的 pdf. 

什麼是 Latex ?

Tex 的作者 Donald E. Knuth 的說法最為傳神:

Tex is a typesetting system it is "intended for the creation of beautiful books - and especially for books that contain a lot of mathematics".

因為我閱讀了很多的書籍, 想做有關 vector space 的數學筆記. 所以很自然的就會想到 Latex.

可是在這裡, 我想介紹的是另一個工具: MikTex (你安裝 LyX 後, 裡面會自動安裝這個工具程式)

miktex

利用 MiKTex 讓我可以用 command line 的方式, 撰寫 Latex 文件. 我追求的是乾淨的 tex 碼, 而不是自動產生的一堆醜八怪又看不懂的程式碼. 如果想學 Latex 我想直接寫是最佳途徑.

所以當我 遇到不會的語法時, 直接到 LyX 看一下想要的數學公式該怎麼寫即可. 透過這種方式, 我就可以延續我手寫 html 產生想要網頁的方式, 產生 Latex 文件. 乾淨又漂亮.

最簡單作法

Step 1: 開啟 MikTex: TeXworks.exe

image

Step 2: 輸入下面最簡單 Latex 語法

\documentclass[a4paper,12pt]{article}
\begin{document}

Hello World

\end{document}

ex:

snap003

  Step 3: 產生 PDF. Ctrl + T (或者滑鼠點選上面的綠色箭頭)

image

  Step 4: Enjoy.

 

最近剛好讀完大前研一的[思考的技術] 一書, 下面是部分片段與大家分享:

[我希望每一個人都能按下 ”歸零”的按鈕, 然後吸收每一件新事物時, 都用自己的眼睛 耳朵, 用心觀察] in page 306.

[現在的學校是破壞人腦的凶器: … 學校的功能應該是, 培養孩子們在沒有答案的時候, 養成”動腦思考”的習慣. 也就是讓孩子對於無解的問題設法提出假設, 並不厭其煩努力證明自己的假設是正確的. …
現在的學校只教導孩子們可以輕鬆解答的問題. 背誦 99 乘法表, 學寫漢字 … 並沒有讓孩子思考]
in pages 177-178.

[應該考的是”思考模式”而不是知識] in page 185.

其他 Latex 的細節

斜體

\emph{斜體字}

image

ex:

\documentclass[a4paper4,12pt]{article}
\begin{document}

Latex is a \emph{powerful} typeset language.

\end{document}

 

雙引號

``  被包的文字      `` (~的那個鍵)

image

ex:

\documentclass[a4paper4,12pt]{article}
\begin{document}

``Latex is a \emph{powerful} typeset language,`` said Jing.

``The book`` is a good book for students.

\end{document}

 

Section

<自動編號>

\section{段落名稱}
\subsection{子段落名稱}

image

ex:

\documentclass[a4paper4,12pt]{article}
\begin{document}
\section{Background Mathematics}
\subsection{Vector Spaces}
\end{document}

 

<取消自動編號>

\section*{段落名稱}
\subsection*{子段落名稱}

image

ex:

\documentclass[a4paper4,12pt]{article}
\begin{document}
\section*{Background Mathematics}
\subsection*{Vector Spaces}
\end{document}

 

粗體

\textbf{粗體}

image

ex:

\documentclass[a4paper,12pt]{article}
\begin{document}

\textbf{Hello} World

\end{document}

 

一般數學

<數學放在文字中>
    一般文字 $數學公式$ 一般文字

image

ex:
\documentclass[a4paper,12pt]{article}
\begin{document}

Let $f$ be the function defined by $f(x)=3x+7$.

\end{document}

 

<數學自己一行>

    一般文字 \[數學公式\] 一般文字

image

ex:
\documentclass[a4paper,12pt]{article}
\begin{document}
If $f(x) = 3x + 7$ and $g(x) = x + 4$ then
\[ f(x) + g(x) = 4x + 11 \]
and
\[ f(x)g(x) = 3x^2 + 19x +28. \]
\end{document}

 

<數學公式後面加上編號>

    \begin{equation}
    你的數學公式
    \end{equation}

image  

ex:
\documentclass[a4paper,12pt]{article}
\begin{document}

    \begin{equation}
    y=a+b.
    \end{equation}
    Here is an example.
    \begin{equation}
    x=c+b.
    \end{equation}

\end{document}

 

上/下標

    _: 下標
    ^: 上標
image

ex1:
\documentclass[a4paper,12pt]{article}
\begin{document}

    \[ ds^2 = dx_1^2 + dx_2^2 + dx_3^2 - c^2 dt^2 \]

\end{document}

 

image

ex2:

\documentclass[a4paper,12pt]{article}
\begin{document}

    $x^{17} - 1$
\end{document}

 

希臘字母: http://www.maths.tcd.ie/~dwilkins/LaTeXPrimer/GreekLetters.html

數學符號: (無窮大)

http://www.maths.tcd.ie/~dwilkins/LaTeXPrimer/MathSymb.html

 

在公式中, 加入文字:

\mbox{空格 一般文字 空格}

image

ex:

\documentclass[a4paper,12pt]{article}
\begin{document}

    \[ M^\bot = \{ f \in V' : f(m) = 0 \mbox{ for all } m \in M \}.\]

\end{document}

 

References:

[1] http://www.maths.tcd.ie/~dwilkins/LaTeXPrimer/
[2] http://frodo.elon.edu/tutorial/tutorial/