> 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/geng-duo-zhu-ti/25-how-to-write-a-latex-class-file-and-design-your-own-cv-part-1.md).

# 如何撰寫 LaTeX 類別檔並設計你自己的 CV（第 1 部分）

第 1 部分 | [第 2 部分](/latex/zh-tw/geng-duo-zhu-ti/26-how-to-write-a-latex-class-file-and-design-your-own-cv-part-2.md)

**作者：James Allen（2011 年 3 月）**

每個人都想要一份看起來專業的履歷，而能提供這種效果的 LaTeX 範本不勝枚舉。不過，如果你和我一樣，你會希望擁有自己的履歷，並把它做成你自己的風格。這表示你需要能自行自訂外觀與感覺，而這在 LaTeX 中向來相當困難。在這一系列部落格文章中，我希望引導你建立自己的自訂類別檔，並向你展示其實可以很輕鬆地把履歷格式化成你想要的樣子。我們會以履歷樣式為主，但這些方法對任何文件類型都完全相同。

### 什麼是類別檔？

當你在 `\documentclass{article}` 寫入你的 LaTeX 檔案時，你就是在載入類別檔 `article.cls`。這會定義所有像 `\section` 和 `\title` 這類用來建構文件的命令。同時也會設定這些命令如何影響頁面的格式與版面配置。

### 設定你自己的類別檔

自訂文件格式最乾淨的方式，是把所有相關資訊都保存在個人類別檔中。這樣可以讓文件的結構與格式設定清楚分離，並且方便重複使用。這很容易設定，所以請建立一個名為 cv.tex 的文件，內容如下：

```latex
\documentclass{my_cv}
\begin{document}
\section{Education}
\subsection{University of Nowhere}
\section{Work}
\subsection{ABC Limited.}
\end{document}
```

這是嘗試載入你的自訂類別檔 `my_cv.cls`，但它目前還不存在。請在與 `my_cv.cls` 相同的目錄中建立 `cv.tex` ，並在其中寫入以下這一行：

```latex
\LoadClass{article}
```

如果你現在編譯文件，應該會看到以預設 article 樣式顯示的標題。

那麼這裡發生了什麼事？類別檔需要包含大量格式資訊與內部設定，才能讓 LaTeX 正常運作，但我們不想把這些全部手動輸入。相反地，我們可以把新的類別檔建立在 `article.cls`上。 `\LoadClass` 包含 `article.cls` 並載入其中定義的所有命令與樣式。請注意，我們不使用一般的 `\documentclass` 命令來載入 `article.cls` ，因為 `\documentclass` ；它只能在 LaTeX 文件的最一開始呼叫一次。

### 告訴 LaTeX 你的類別

所有類別檔都應以兩行類似下列內容開頭，而你應該把它們加到 `my_cv.cls` 的頂端：

```latex
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{my_cv}[2011/03/26 My custom CV class]
```

該 `\NeedsTeXFormat` 命令會告訴編譯器這個套件是給哪個版本的 LaTeX 使用。LaTeX 的目前版本是 LaTeX2e，而且幾乎所有發行版都使用它。

該 `\ProvidesClass` 命令會提供編譯器一些關於你的套件的資訊。第一個參數應與你的類別檔檔名相符，並告訴 LaTeX 你的套件名稱。第二個參數是選用的，會提供你的類別說明，並顯示在日誌與其他地方。說明必須以日期開頭，格式必須完全如上，且應該是套件最後修改的日期。這可在載入類別時用來檢查你是否使用了夠新的版本。例如，如果你透過以下方式載入它： `\documentclass{my_cv}[2012/01/01]` 而日期比類別說明中的日期更新，就會顯示一則警告，指出該類別已過時。

### 修改章節標題

標準的 article 章節標題其實不太適合履歷，所以我們想把它們換成更俐落的樣式。為此，我們可以重新定義 `\section` 命令來輸出自訂標題。

幸好已經有一個很優秀的套件叫做 `titlesec` ，它提供了簡單的方法來自訂標題樣式。將它加入你的類別檔：

```latex
\RequirePackage{titlesec}
```

請注意，我們應該使用 `\RequirePackage` 而不是一般的 `\usepackage` 命令，因為我們現在是在類別檔中。 `\RequirePackage` 命令可確保每個套件只會被載入一次，即使從不同的樣式檔與類別檔多次呼叫也一樣。

該 `titlesec` 這個套件提供了 `\titleformat` 命令，它讓我們可以自訂章節標題。請在 `my_cv.cls` 的末尾加入以下內容，以自訂標題格式：

```latex
\titleformat{\section}         % 自訂 \section 命令
  {\Large\scshape\raggedright} % 讓 \section 標題變大（\Large），
                               % 小型大寫字母（\scshape）並靠左對齊（\raggedright）
  {}{0em}                      % 可用來替所有章節加上前綴，例如 'Section ...'
  {}                           % 可用來在標題前插入程式碼
  [\titlerule]                 % 在標題後插入一條水平線
```

如果我們編譯 `cv.tex` ，現在就會看到一些更適合履歷的主標題：

![Screenshot1.png](/files/8cdb79af0e15a12e946a9b8902e84e66c95a3054)

我們也可以自訂 \subsection 標題：

```latex
\titleformat{\subsection}
  {\large\scshape\raggedright}
  {}{0em}
  {}
```

現在子章節也使用相同的樣式：

![Screenshot2.png](/files/dbf9645cca81fe47e372d83c895d86c8a4ffb1b1)

你應該試試看一些可用的格式選項，看看自己喜歡哪一種：

* `\bfseries`, `\itshape`：讓標題變成粗體或斜體；
* `\scshape`：小型大寫字母；
* `\small`, `\normalsize`, `\large`, `\Large`, `\LARGE`, `\huge`, `\Huge`：設定字型大小；
* `\rmfamily`, `\sffamily`, `\ttfamily`：分別將字型類型設為襯線體、無襯線體或打字機體。

### 為章節標題加入日期

我們可以定義一些新命令，讓章節標題中包含日期。請在你的類別檔中加入以下內容：

```latex
\newcommand{\datedsection}[2]{%
  \section[#1]{#1 \hfill #2}%
}
\newcommand{\datedsubsection}[2]{%
  \subsection[#1]{#1 \hfill #2}%
}
```

這定義了兩個新命令 `\datedsection` 和 `\datedsubsection` ，它們接受兩個引數：如前所述的章節名稱，以及一個會排在頁面右側的日期。 `\hfill` 命令會告訴 LaTeX 盡可能填滿空間，因此會把第二個引數（`#2`）推到頁面右側。修改 `cv.tex` 以使用這些命令：

```latex
\documentclass{my_cv}

\begin{document}

\section{Education}
\datedsubsection{University of Nowhere}{2004--2008}
我從 2004 年到 2008 年就讀於 University of Nowhere。

\section{Work}
\datedsubsection{ABC Limited.}{2008--Now}
我自 2008 年起在 ABC Limited 工作。

\end{document}
```

我們的履歷現在包含了日期：

![Screenshot3.png](/files/7bfa09aa78d9339fd03f4d0f8aee54176aeaff1d)

### 結論

這個指南的第一部分就到這裡，希望我已經涵蓋了足夠內容，讓你可以自己開始製作實用的類別檔。僅用不多的命令，就已經建立出一個看起來相當合理的履歷範本，而我們也只是觸及了還能自訂的部分表面。在本指南接下來的幾個部分中，我會談到如何把選項傳給你的類別以進行設定、如何建立漂亮的標題，以及如何設定一些一般版面配置選項。

感謝閱讀！

請注意，我自己在建立類別檔方面也還算新手，所以如果有人能指出我在這裡提到的做法有哪些更好的方式，請告訴我。

第 1 部分 | [第 2 部分](/latex/zh-tw/geng-duo-zhu-ti/26-how-to-write-a-latex-class-file-and-design-your-own-cv-part-2.md)


---

# 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/geng-duo-zhu-ti/25-how-to-write-a-latex-class-file-and-design-your-own-cv-part-1.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.
