> For the complete documentation index, see [llms.txt](https://ayakaleaf-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://ayakaleaf-pro.ayaka.space/latex/zh-cn/geng-duo-zhu-ti/25-how-to-write-a-latex-class-file-and-design-your-own-cv-part-1.md).

# 如何编写 LaTeX 类文件并设计你自己的简历（第 1 部分）

第1部分 | [第 2 部分](/latex/zh-cn/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` 命令，因为我们现在是在类文件中。The `\RequirePackage` 该命令可确保每个宏包只会被加载一次，即使它在不同的样式文件和类文件中被多次调用。

该 `titlesec` 宏包提供了命令 `\titleformat` ，它让我们能够自定义节标题。请在 `my_cv.cls` 的末尾添加以下内容，以定制标题的格式：

```latex
\titleformat{\section}         % Customise the \section command
  {\Large\scshape\raggedright} % Make the \section headers large (\Large),
                               % small capitals (\scshape) and left aligned (\raggedright)
  {}{0em}                      % Can be used to give a prefix to all sections, like 'Section ...'
  {}                           % Can be used to insert code before the heading
  [\titlerule]                 % Inserts a horizontal line after the heading
```

如果我们编译 `cv.tex` ，现在我们会看到一些更适合简历的主标题：

![Screenshot1.png](/files/a39af4b051cad2a070204d4ee6d970f1a79df710)

我们也可以自定义 \subsection 标题：

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

现在各个小节也采用相同的样式：

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

你应该尝试一下可用的格式选项，看看自己喜欢哪种：

* `\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` 它们接受两个参数：一个是像之前一样的节名称，另一个是将排版在页面右侧的日期。The `\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/bd14abb7d5d1fa79332638f3d779f148077fa924)

### 结论

本指南的第一部分就到这里，不过希望我已经讲得足够多，让你可以开始制作实用的类文件了。只用了不多的几个命令，我们就创建了一个看起来已经相当不错的简历模板，而这还只是我们还能自定义内容的冰山一角。在本指南接下来的几部分中，我会讨论如何向你的类传递选项以进行配置、如何创建一个漂亮的标题，以及如何设置一些通用的版式选项。

感谢阅读！

请注意，我自己在创建类文件方面还比较新手，所以如果有人能指出我在这里提到的做法有哪些更好的实现方式，请告诉我。

第1部分 | [第 2 部分](/latex/zh-cn/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://ayakaleaf-pro.ayaka.space/latex/zh-cn/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.
