> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/zh-tw/shen-ru-wen-zhang/32-markdown-into-latex-with-style.md).

# 以優雅樣式將 Markdown 轉為 LaTeX

**發表後更新** （2017 年 5 月 13 日）：我們非常感謝 Vít Novotný，他是以下內容的作者／維護者： `markdown` 套件，並就原始文章提供了一些有幫助的回饋。很高興發表這個更新版本，納入了他的建議與意見。

### 簡介

不久前，我們曾為以下內容的發佈而歡欣鼓舞： [`markdown 套件`](http://www.ctan.org/pkg/markdown) 這使得可以在 LaTeX 文件中撰寫 markdown 語法——我們也以一篇名為 [如何在 Overleaf 上使用 Markdown 撰寫](/latex/zh-tw/shen-ru-wen-zhang/30-how-to-write-in-markdown-on-overleaf.md) 其目的在於示範部分支援的語法。

**記得**：你需要編譯你的 `.tex` 檔案時使用 `--shell-escape` 選項，這樣才能在本機 LaTeX 安裝上運作——Overleaf 會自動為你處理。請注意，大多數期刊投稿入口並未啟用 `--shell-escape`——除非你只被允許上傳稿件的輸出 PDF，否則含有 markdown 的 LaTeX 檔案在出版商的投稿系統處理時，可能無法成功編譯。

以下內容中暗示的一個想法是： [較早的文章](/latex/zh-tw/shen-ru-wen-zhang/30-how-to-write-in-markdown-on-overleaf.md) 就是 markdown 與 LaTeX 的組合能夠讓內容與樣式完美分離。作者（尤其是那些已經熟悉 markdown 的人）可以使用更簡潔的語法（但有些限制），同時仍能運用 LaTeX 套件與範本的各種優點來處理樣式與排版。那篇 [較早的文章](/latex/zh-tw/shen-ru-wen-zhang/30-how-to-write-in-markdown-on-overleaf.md) 提供了一些在 [Beamer 簡報中使用 markdown 的範例](https://www.overleaf.com/read/dnrwnjrpjjhw) 和 [海報](https://www.overleaf.com/read/jtbgmmgqrqmh)：今天我們要看看更多有趣又令人興奮的樣式可能性！

### 在具樣式的 LaTeX 範本中使用 markdown

除了上述範例之外，你幾乎可以在所有範本中使用 markdown。以下是幾個快速範例：

* 該 [Legrange orange book](https://www.overleaf.com/latex/templates/the-legrand-orange-book-template-english/jtctyfmnpppc) 範本
* 該 [受 Tufte 啟發的講義](https://www.overleaf.com/latex/templates/handout-design-inspired-by-edward-tufte/dtsbhhkvghzz) 範本
* 這個 [非常不錯的文章範本](https://www.overleaf.com/read/xfsrpsmdtqvt) 由 Thiago 提供

你可能會注意到，其中兩個是文章範本，一個是書籍範本。 [`markdown 套件`](http://www.ctan.org/pkg/markdown) 會偵測 `\chapter` 指令是否可在特定的 LaTeX 文件中使用，並會將第一層標題 `markdown` 行呈現為 `\chapter` 或 `\section` 適當的格式。

### 超快速講義：markdown + pocketmod

從我第一次認識 markdown 起，我就覺得它是一種快速又方便的筆記方式：它很像我會在零碎紙張或便利貼上匆匆寫下筆記的樣子——尤其是條列與編號清單語法。當我第一次建立 [PocketMod 範本](https://www.overleaf.com/read/nqbhpnrkskrx) 時，我就渴望有一種方法能在 LaTeX 中撰寫 markdown，讓我可以製作 PocketMod *與* markdown，因為這感覺……很合適。在小小的折疊小冊子上用簡單語法記下筆記，但搭配精美的排版——有些人把 PocketMod 稱為「zines」，因為它們是你可以用一張 A4 紙做成的 8 頁小冊子。

果然真的可行！這讓快速製作一本 [每週行事曆小冊子](https://www.overleaf.com/read/zfzcxygqwqsq) 或一本小小的 [圖畫故事書](https://www.overleaf.com/read/rppnsfgnrhsf)。是的，看著一本由她自己的畫作做成的故事書，確實讓我女兒開心了一陣子……嗯，大概只有 10 分鐘！這個 [`markdown 套件`](http://www.ctan.org/pkg/markdown) 讓你自訂每種 markdown 語法要如何轉譯成 LaTeX，所以我重新定義了標準的 `![](img_file "標題")` 語法，將標題放在圖片檔下方（不加任何「Figure」前綴或編號），然後插入分頁。這讓製作圖畫書更加容易。這裡有一段示範如何摺疊小冊子的影片：

{% embed url="<https://www.youtube.com/embed/IAb31rIeGZo>" %}

### 簡易食譜小冊子

你仔細想想，食譜其實就是由各種清單組成：材料清單、準備或烹調步驟清單，也許還有備註清單（烹調時間、份量等等）。因此，與其寫成這樣：

```latex
\begin{enumerate}
\item 先做這個
\item 然後做那個
...
\end{enumerate}
```

我當然會很希望能這樣寫：

```
#. 先做這個
#. 然後做那個
```

材料部分也同樣如此：

```
- 2 顆蛋
- 一撮鹽
```

因此，若要輕鬆做出一道快速食譜，我們可以這樣寫：

```
# 炒蛋

烹調時間
：5 分鐘

份量
: 1

## 材料
- 4 顆蛋
- 1 茶匙奶油
- 1/4 杯牛奶
- 鹽、胡椒（依口味）

## 步驟

#. 打散雞蛋、牛奶……
#. 在平底鍋中加熱奶油……
#. ...
```

嗯，這看起來很有希望。Overleaf 的幾位同事（以及我們的家人）貢獻了他們最喜歡的快速食譜（非常適合第一次下廚的人），而我則迅速拼湊出一個（原諒這個雙關語） [快速食譜範本](https://www.overleaf.com/read/gscqdhnwzsfg) 你可以用 markdown 撰寫的 *和* 做成一本小冊子。

[![A recipe booklet produced on Overleaf using markdown](/files/50bdf609ea7cb7da805bf295bc085dbf875c3c39)](https://www.overleaf.com/read/gscqdhnwzsfg)

接下來還有另一點有趣的地方。從底層來看，這仍然是 LaTeX，因此我們可以繼續用一般 LaTeX 的各種技巧來為不同元素——章節標題、項目清單、字型等等——設定樣式。就在這時，我有點玩得過頭了，並想出了 *三個* 用於 `simple-recipe` 類別的「主題」！

### 加碼內容 1：使用 markdown 套件時的一些注意事項

* SyncTeX 可能無法完全正常運作。在 Overleaf 上，這表示「預覽中的位置」以及「在 `markdown` 區域中將無法使用。
* `\\` 失去其原本的意義：一般「兩個空格後接換行」 `markdown` 技巧在這裡行不通，因為尾端空白會被 Lua 解析器移除，如 [`markdown` 套件說明文件](http://texdoc.net/pkg/markdown). `\newline` 仍然可行。
* 請小心同一行中出現多個底線字元——如果以下情況，這可能會造成問題：

  * 你的方程式中有很多下標（提示：在這種情況下脫離 `markdown` 環境）；
  * 你喜歡在 BibTeX 鍵中使用底線。

  你可以設定 `underscores=false` 搭配最新的 `markdown 套件` 檔案來讓下標底線運作，但對於 BibTeX 鍵中的底線仍然無法使用。

  **更新** （2017 年 5 月 13 日）：BibTeX 鍵中的底線將可透過 `markdown` v2.5.2 之後的版本運作——謝謝你，Vít！
* 要顯示字面上的井字號會有些困難，因為連 `\#` 也無法運作！我通常會定義一個新指令來代替。

### 加碼內容 2：更新版 markdown 套件中的新功能

`markdown` v2.4 及之後的版本新增了對一些很棒語法的支援：

* **行內註腳語法**: `...^[這是註腳]` （需要 `inlineFootnotes` 選項）
* **引文：**`[@smith1990]` （需要 `引文` 選項；另外也要小心 BibTeX 鍵中的底線！）
* **圍欄程式碼**：你可以使用波浪號 `~` 或反引號 `` ` `` 作為圍欄。

```
~~~~
int main() {
  cout << "Hello world!";
  return 0;
}

~~~~
```

如果你想使用這些新功能，請務必上傳更新版的 `markdown.lua`, `markdown.tex` 和 `markdown.sty` 到你的專案——或者你也可以直接建立一個新的專案，從 [這個範本開始](https://www.overleaf.com/read/whdrnpcpnwrm).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/zh-tw/shen-ru-wen-zhang/32-markdown-into-latex-with-style.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
