跳到主要内容

高级特性

本章介绍 LaTeX 的高级特性,包括自定义命令、宏包开发等内容。

自定义命令​

newcommand​

定义新命令:

\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}

实数集 $\R$,自然数集 $\N$。

带参数的命令​

\newcommand{\abs}[1]{\left| #1 \right|}

$\abs{x}$
$\abs{\frac{a}{b}}$

带可选参数的命令​

\newcommand{\set}[2][]{\{#2 \mid #1\}}

$\set{x}$ % {x}
$\set[x>0]{x}$ % {x | x>0}

renewcommand​

重定义命令:

\renewcommand{\thesection}{\Roman{section}}

providecommand​

仅在命令不存在时定义:

\providecommand{\R}{\mathbb{R}}

自定义环境​

newenvironment​

\newenvironment{important}
{\begin{quote}\bfseries}
{\end{quote}}

\begin{important}
这是重要内容。
\end{important}

带参数的环境​

\newenvironment{boxed}[1]
{\begin{center}\fbox{\begin{minipage}{#1}}
{\end{minipage}\end{center}}

\begin{boxed}{5cm}
这是一个带边框的内容。
\end{boxed}

计数器​

内置计数器​

page       % 页码
section % 节
subsection % 小节
figure % 图
table % 表
equation % 公式
footnote % 脚注

计数器操作​

\setcounter{section}{0}      % 设置值
\addtocounter{section}{1} % 增加值
\stepcounter{section} % 增加1
\refstepcounter{section} % 增加1并设置引用标签
\value{section} % 获取值
\thesection % 显示值

自定义计数器​

\newcounter{mycounter}
\setcounter{mycounter}{0}
\stepcounter{mycounter}
\themycounter

计数器格式​

\arabic{counter}   % 阿拉伯数字:1, 2, 3
\roman{counter} % 小写罗马数字:i, ii, iii
\Roman{counter} % 大写罗马数字:I, II, III
\alph{counter} % 小写字母:a, b, c
\Alph{counter} % 大写字母:A, B, C
\fnsymbol{counter} % 符号:*, †, ‡

长度​

内置长度​

\textwidth   % 文本宽度
\textheight % 文本高度
\linewidth % 当前行宽度
\parindent % 段落缩进
\parskip % 段落间距
\baselineskip % 行距

长度操作​

\setlength{\parindent}{2em}
\addtolength{\parindent}{1em}
\settowidth{\parindent}{示例文本}
\settoheight{\parindent}{示例文本}
\settodepth{\parindent{示例文本}

自定义长度​

\newlength{\mylength}
\setlength{\mylength}{5cm}

条件判断​

ifthen 宏包​

\usepackage{ifthen}

\ifthenelse{\equal{a}{a}}{相等}{不相等}
\ifthenelse{\isodd{3}}{奇数}{偶数}
\ifthenelse{\lengthtest{\parindent > 0pt}}{有缩进}{无缩进}

etoolbox 宏包​

\usepackage{etoolbox}

\ifnum\value{section}>0
大于0
\else
不大于0
\fi

\ifdim\textwidth>10cm
宽文本
\else
窄文本
\fi

循环​

loop 宏包​

\usepackage{loop}

\loop{5}{\arabic{count} }

手动循环​

\newcount\mycount
\mycount=0
\loop
\advance\mycount by 1
\the\mycount,
\ifnum\mycount<10
\repeat

宏包开发​

基本结构​

创建 .sty 文件:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypackage}[2024/01/01 My Package]

% 宏包内容
\newcommand{\mycommand}{...}

\endinput

宏包选项​

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypackage}[2024/01/01 My Package]

\newif\if@option
\@optionfalse

\DeclareOption{option}{\@optiontrue}
\ProcessOptions

\if@option
% 选项开启时的代码
\fi

\endinput

加载其他宏包​

\RequirePackage{amsmath}
\RequirePackage{graphicx}

文档类开发​

基本结构​

创建 .cls 文件:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myclass}[2024/01/01 My Class]

\LoadClass{article}

% 类内容
\RequirePackage{amsmath}
\RequirePackage{graphicx}

\endinput

类选项​

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myclass}[2024/01/01 My Class]

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions

\LoadClass{article}

\endinput

钩子​

LaTeX 钩子​

\AtBeginDocument{文档开始时执行}
\AtEndDocument{文档结束时执行}
\AtBeginSection{节开始时执行}
\AtEndSection{节结束时执行}

etoolbox 钩子​

\usepackage{etoolbox}

\AtBeginEnvironment{quote}{\itshape}
\AtEndEnvironment{quote}{\upshape}
\BeforeBeginEnvironment{quote}{\par\noindent}
\AfterEndEnvironment{quote}{\par}

错误处理​

错误消息​

\PackageError{mypackage}{错误描述}{帮助信息}
\PackageWarning{mypackage}{警告信息}
\PackageInfo{mypackage}{信息}

检查命令是否存在​

\@ifundefined{mycommand}
{\newcommand{\mycommand}{...}}
{\renewcommand{\mycommand}{...}}

调试​

显示信息​

\show\mycommand     % 显示命令定义
\showthe\textwidth % 显示长度值
\typeout{信息} % 输出到日志

调试模式​

\tracingmacros=1   % 跟踪宏展开
\tracingcommands=1 % 跟踪命令
\tracingall % 跟踪所有

示例​

自定义定理样式​

\usepackage{amsthm}

\newtheoremstyle{mytheorem}
{10pt} % 上方间距
{10pt} % 下方间距
{\itshape} % 正文字体
{} % 缩进
{\bfseries} % 标题字体
{.} % 标题后标点
{ } % 标题后间距
{\thmname{#1}\thmnumber{ #2}\thmnote{ (#3)}}

\theoremstyle{mytheorem}
\newtheorem{mythm}{定理}

自定义列表​

\usepackage{enumitem}

\newlist{steps}{enumerate}{1}
\setlist[steps,1]{label=Step \arabic*:}

\begin{steps}
\item 第一步
\item 第二步
\end{steps}

小结​

本章介绍了 LaTeX 高级特性:

  • 自定义命令:newcommand、renewcommand
  • 自定义环境:newenvironment
  • 计数器:操作和格式
  • 长度:内置长度和自定义长度
  • 条件判断:ifthen 和 etoolbox
  • 宏包开发:创建 .sty 文件
  • 文档类开发:创建 .cls 文件
  • 钩子:AtBeginDocument 等
  • 错误处理和调试

掌握高级特性可以创建可复用的 LaTeX 组件。下一章将介绍中文文档。