免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
返回列表 发帖

关于《撸题集》的 LaTeX(2022-3-19 重新编辑)

由于这里的 MathJax 会影响代码,所以我将代码发了在:kuing.is-programmer.com/posts/205617.html
那边博客编辑时有问题弄坏了,而这边早已解决“MathJax 会影响代码”的问题,所以还是发回来这里,顺便作一些改进。

目录(点击跳转)
$\href{#sec0}{\text{零、文档类与编译方式}}$
$\href{#sec1}{\text{一、加载宏包}}$
$\href{#sec2}{\text{二、一些设置}}$
$\href{#sec3}{\text{三、定理类环境、题目环境的定制}}$
$\href{#sec4}{\text{四、为懒人而设(其实也不完全是)的各种命令,请按需而取之,慎用}}$
$\href{#sec5}{\text{五、带圈数字和脚注}}$
$\href{#sec6}{\text{六、用 tikz 画 LaTeX 里没有的符号、用 mdframed 宏包定制引用环境}}$
$\href{#sec7}{\text{七、选择题、填空题}}$
$\href{#sec8}{\text{八、图片并排放置}}$
$\href{#sec9}{\text{九、引号替换——输入 “  ” 输出 「  」}}$
$\href{#secA}{\text{十、begin{document} 之后}}$

时常有网友向我要《撸题集》的 LaTeX 模板,由于我觉得:
  • 我的 LaTeX 水平还不算太好,怕一些烂代码误人子弟;
  • 有很多自定义命令比较个人化,只适用于我的书上,并不适合他人使用;
  • 如果直接给出一个能编译的模板,恐怕很多人就不会再用心去看里头的代码了。
所以我决定不将完整代码给出,仅抽取一些大众适用的,应该没太大问题的代码贴出来,仅供大家参考。

我希望的是,我提供各种各样的零件,你们自己动手组装,喜欢哪些就装哪些。
同时还可以把你平时用惯的代码也装进去,最终制成你自己了解的模板。
当然,这需要你对 LaTeX 有一定基础,所以如果还没入门的,建议先看点基本教程再说。

我会尽量给出代码的说明,各位请先仔细阅读后再按需取用,切勿随便抄了就算,否则极容易出现问题。


零、文档类与编译方式

建议使用 ctex 系列的文档类——小论文之类用 ctexart,写书用 ctexbook,这样就基本不用自己去设置中文。
(唯一缺点是总有字体警告,虽然无关痛痒,但对强逼症者就……)
如果不用 ctex 的文档类,就需要用 xeCJK 宏包设置字体,并自行修改标题等各种格式为中文风格,比较麻烦,这里不作介绍。
(其实当年我就是这样做的,因为我追求“三0”——Errors: 0   Warnings: 0   Bad Boxes: 0)

编译方式:XeLaTeX
  1. % !Mode:: "TeX:UTF-8"
  2. \documentclass[UTF8,fontset=windows]{ctexbook}
  3. \ctexset{
  4. section/format+=\raggedright,
  5. }
复制代码
第一行是编码标识,避免 WinEdt 打开乱码,有些编辑器的编码标识写法可能不同,有见过用%# -*- coding:utf-8 -*-的。
选项里fontset=windows表示使用 windows 系统里的中文字体,之后的是让节标题左对齐。
更多选项及设置请阅读 ctex 宏集手册,建议读下。


一、加载宏包
  1. \usepackage{amsmath,amssymb}
  2. %数学必备宏包

  3. \usepackage{mathtools}
  4. %amsmath 的拓展包

  5. \usepackage{amsthm}
  6. %定理和证明环境,如果想要更自由的定制,可改用 ntheorem 包

  7. \usepackage{bm}
  8. %数学粗体,用于向量等,命令是\bm

  9. \usepackage{mathrsfs}
  10. %数学花体,命令是\mathscr

  11. \usepackage{esvect}
  12. %好看的向量箭头符号,命令是\vv

  13. \usepackage{centernot}
  14. %用于较长符号(如\iff)的否定形式,命令是\centernot(后文自定义命令中有用到)

  15. \usepackage{tikz}
  16. %画图
  17. \usetikzlibrary{calc,angles,quotes}%加载一些 library(请按需添加)
  18. \tikzset{every picture/.style={line join=round,>=latex,}}%tikz 全局设置

  19. \usepackage[framemethod=tikz]{mdframed}
  20. %制作各种花式框框,这里我用来定制引用环境(见后文)

  21. \usepackage{enumitem}
  22. %枚举环境增强包,方便设置条目格式

  23. \usepackage{graphicx}
  24. %插图包

  25. \usepackage{fancyhdr}
  26. %页眉页脚包

  27. \usepackage[perpage]{footmisc}
  28. %脚注包,perpage 选项表示按页编号

  29. \usepackage{caption}
  30. %修改图表标题格式

  31. \usepackage[a4paper,top=1in,bottom=1in,left=0.8in,right=0.8in,headheight=13pt]{geometry}
  32. %页面设置,请根据你的喜好修改之

  33. \usepackage{hyperref}
  34. %PDF 链接等设置
  35. \hypersetup{%
  36. pdfstartview=FitH,      %PDF 打开时=页面适合窗口
  37. CJKbookmarks=true,      %中文书签支持
  38. bookmarksnumbered=true, %书签中章节编号
  39. bookmarksopen=true,     %书签目录展开
  40. colorlinks,             %彩色链接
  41. %下面几个请根据你的喜好修改之,不设也可以,颜色自有默认值
  42. %linkcolor=颜色名,      %内部链接颜色,我书上设的是 violet
  43. %urlcolor=颜色名,       %url 链接颜色,我书上设的是 blue
  44. %citecolor=颜色名,      %参考文献链接颜色,我书上设的是 teal
  45. %pdftitle={XXX},         %文件标题
  46. %pdfauthor={XXX},        %文件作者
  47. %pdfsubject={XXX},       %文件主题
  48. %pdfkeywords={XXX}       %文件关键字
  49. }
复制代码
要了解某宏包的用法,只需在命令行运行:texdoc 宏包名称,比如
texdoc mathtools
就可以打开系统自带的 mathtools 宏包的使用手册。


二、一些设置
  1. %限制两行之间的实际距离至少 3pt
  2. \lineskiplimit=3pt
  3. \lineskip=3pt

  4. %允许页尾多行公式换页
  5. \allowdisplaybreaks

  6. %空白偶数页去除页眉页脚
  7. \makeatletter
  8. \def\cleardoublepage{\clearpage\if@twoside\ifodd\c@page\else
  9. \thispagestyle{empty}\hbox{}\newpage\if@twocolumn\hbox{}\newpage\fi\fi\fi}
  10. \makeatother

  11. %定义弧符号,命令是 \hu
  12. \DeclareSymbolFont{yh}{OMX}{yhex}{m}{n}
  13. \DeclareMathAccent{\hu}{\mathord}{yh}{"F3}

  14. %一些默认没有的数学函数命令需要新建
  15. \DeclareMathOperator\arccot{arccot}
  16. \DeclareMathOperator\arcsec{arcsec}
  17. \DeclareMathOperator\arccsc{arccsc}
  18. \DeclareMathOperator\sech{sech}
  19. \DeclareMathOperator\csch{csch}
  20. \DeclareMathOperator\arcsinh{arcsinh}
  21. \DeclareMathOperator\arccosh{arccosh}
  22. \DeclareMathOperator\arctanh{arctanh}
  23. \DeclareMathOperator\arccoth{arccoth}
  24. \DeclareMathOperator\arcsech{arcsech}
  25. \DeclareMathOperator\arccsch{arccsch}

  26. \newcommand\px{\mathrel{/\mkern-5mu/}}  %斜的平行符号
  27. \newcommand\rmd{\mathop{}\!\mathrm{d}}  %微分直立 d

  28. \newcommand\relph[1][=]{\mathrel{\phantom{#1}}}
  29. %二元关系符“幻影”,用于多行公式右边太长需再换行时要空出的位置(常为=,故以此为默认参数)
复制代码
最后那个 \relph 的用法示例如下
$\newcommand\relph[1][=]{\mathrel{\phantom{#1}}}$
\begin{align*}
A &= loooooooooooooooooooo \\
& \relph ooooong ~ equation \\
&= \cdots\\
&= \cdots
\end{align*}
\begin{align*}
A &= loooooooooooooooooooo \\
& \relph ooooong ~ equation \\
&= \cdots\\
&= \cdots
\end{align*}


三、定理类环境、题目环境的定制
  1. %自定义定理样式
  2. \newtheoremstyle{moren}{}{}{\normalfont}{\parindent}{\bfseries}{.}{ }{}
  3. \theoremstyle{moren}
  4. \newtheorem{thm}{定理}[section]   %定理类环境按节计数
  5. \newcommand\thmautorefname{定理}   %设置用 \autoref 引用时显示的名称
  6. \newtheorem{lem}{引理}[section]
  7. \newcommand\lemautorefname{引理}
  8. %命题、猜想等类似,不再详写
  9. \newtheorem{cor}{推论}[thm]       %推论按定理计数
  10. \newcommand\corautorefname{推论}

  11. %方程、图表按节计数,修改图表 \autoref 引用名称
  12. \numberwithin{equation}{section}
  13. \numberwithin{figure}{section}
  14. \numberwithin{table}{section}
  15. \renewcommand\figureautorefname{图}
  16. \renewcommand\tableautorefname{表}

  17. %题目专用样式
  18. \newtheoremstyle{tmstyle}{}{}{\normalfont}{\parindent}{\bfseries}{}{ }
  19. {\colorbox{gray!25}{\thmname{#1} \thmnumber{#2}\thmnote{(#3)}.}}
  20. \theoremstyle{tmstyle}
  21. \newtheorem{problem}{题目}[section]
  22. \newcommand\problemautorefname{题目}

  23. %修改 proof 证明环境
  24. \makeatletter
  25. \renewcommand\proofname{证明}
  26. \renewenvironment{proof}[1][\proofname]{\par
  27. \pushQED{\qed}%
  28. \normalfont \topsep6\p@\@plus6\p@ \labelsep1em\relax
  29. \trivlist
  30. \item[\hskip\labelsep\indent
  31. \bfseries #1]\ignorespaces
  32. }{%
  33. \popQED\endtrivlist\@endpefalse
  34. }
  35. \makeatother
复制代码
如需用到 定理' 、定理'' 之类的,请见:
http://kuing.orzweb.net/viewthread.php?tid=356

书中用于分隔题目的四种循环花色的设计,请见:
http://kuing.orzweb.net/viewthread.php?tid=3814


四、为懒人而设(其实也不完全是)的各种命令,请按需而取之,慎用

注:有些与悠闲论坛上的自定义命令是一致的,故此有些命令在 http://kuing.orzweb.net/viewthread.php?tid=6 里也能找到例子。
  1. \let\le\leqslant
  2. \let\leq\leqslant
  3. \let\ge\geqslant
  4. \let\geq\geqslant

  5. \let\iff\Longleftrightarrow
  6. \let\implies\Longrightarrow
  7. \let\impliedby\Longleftarrow
  8. \let\riff\implies
  9. \let\liff\impliedby
  10. \newcommand\niff{\centernot\iff}    %否定形式,需要 centernot 宏包
  11. \newcommand\nriff{\centernot\riff}
  12. \newcommand\nliff{\centernot\liff}

  13. \let\mbb\mathbb
  14. \newcommand\inR{\in\mbb R}
  15. \newcommand\inN{\in\mbb N}
  16. \newcommand\inZ{\in\mbb Z}
  17. \newcommand\inC{\in\mbb C}
  18. \newcommand\inQ{\in\mbb Q}

  19. \let\veps\varepsilon
  20. \let\kongji\varnothing  %空集
  21. \let\buji\complement    %补集

  22. \newcommand\du{^\circ}  %角度

  23. %将 \S 重定义为三角形面积所用,需要用原来的 \S 时可以用 \oldS
  24. \let\oldS\S
  25. \renewcommand\S[1]{S_{\triangle #1}}

  26. %批量生成数列命令 \an \bn \Sn 等,在文本模式或数学模式下均可使用
  27. \newcommand\slcmds{a,b,c,d,x,y,z,A,B,C,D,F,S,T}
  28. \newcommand\getslcmd[1]{%
  29. \expandafter\newcommand\csname #1n\endcsname{\ensuremath{\{#1_n\}}}}
  30. \makeatletter
  31. \@for\slcmdtmp:=\slcmds\do{\expandafter\getslcmd\expandafter{\slcmdtmp}}
  32. \makeatother

  33. \newcommand\holder{H\"older}
  34. \newcommand\schur{Sch\"ur}
  35. \newcommand\LHS{\text{LHS}}
  36. \newcommand\RHS{\text{RHS}}

  37. \newcommand\Rtt{\text{Rt}\triangle}
  38. \newcommand\sumcyc{\sum_{\mathrm{cyc}}}
  39. \newcommand\sumsym{\sum_{\mathrm{sym}}}
  40. \newcommand\prodcyc{\prod_{\mathrm{cyc}}}
  41. \newcommand\prodsym{\prod_{\mathrm{sym}}}

  42. \newenvironment{led}{\left\{\begin{aligned}\relax}{\end{aligned}\right.}
  43. %带左大括号的aligned,用于方程组等
复制代码
最后那个 led 环境虽然也可以用于写分段函数,但写法稍麻烦(包括但不限于多几个 &)。
分段函数建议用 mathtools 宏包提供的 dcases 环境来写。


五、带圈数字和脚注

(这部分重写过,原先需要 tikz,现已不需要)

首先将带圈数字归为 CJK 类:
  1. \xeCJKsetcharclass{9312}{9331}{1}% 圈1至圈20
  2. \XeTeXcharclass9450=1% 圈0
复制代码
这样在源码直接用输入法输入的 ①②③……⑩ 才能正常显示。

然后设置一个命令输入带圈数字(用于脚注或枚举环境等),这里分两种情况(二选一):
(1)简易情况:如果你能确定你不需要输入超过 ⑩ 的带圈数字,则:
  1. \newcommand\quan[1]{%
  2. \ifnum#1>0\relax\ifnum#1<11\relax
  3. \symbol{\numexpr9311+#1\relax}%
  4. \else#1\fi\else#1\fi
  5. }
复制代码
这样,用 \quan{1},...,\quan{10} 就可以输出 ① 至 ⑩;
(2)如果有需要输入“圈0”或“圈11”以上,由于默认的宋体没有这些字符,则需先设置一个有这些字符的字体。
比如,Cambria 字体有“圈0”及“圈11”以上,则:
  1. \newCJKfontfamily\Cam{Cambria}
  2. \newcommand\quan[2][Cam]{%
  3. \ifnum#2=0\relax{\CJKfamily{#1}\symbol{9450}}%
  4. \else\ifnum#2>0\relax\ifnum#2<21\relax
  5. {\CJKfamily{#1}\symbol{\numexpr9311+#2\relax}}%
  6. \else#2\fi\else#2\fi\fi
  7. }
复制代码
这样,用 \quan{0},...,\quan{20} 就可以输出 Cambria 字体的“圈0”至“圈20”。

最后改脚注改为带圈数字:
  1. \renewcommand\thefootnote{\protect\quan{\value{footnote}}}
复制代码
如果不喜欢这套方法,也可以考虑完全用 tikz 来画,就无需考虑字体,见:
http://kuing.orzweb.net/viewthread.php?tid=3125


六、用 tikz 画 LaTeX 里没有的符号、用 mdframed 宏包定制引用环境
  1. \newlength\Aheight%用作临时高度
  2. %画平行四边形
  3. \newcommand\pxsbx{\mathord{\text{%
  4. \begin{tikzpicture}[baseline,line width=0.1ex,line join=round]
  5. \settoheight\Aheight{$A$}
  6. \addtolength\Aheight{-.05ex}
  7. \draw (0,.05ex)--(.74em,.05ex)--(.9em,\Aheight)--(.16em,\Aheight) -- cycle;
  8. \end{tikzpicture}}}}
  9. %画平行且等于
  10. \newcommand\pqd{\mathrel{\text{%
  11. \begin{tikzpicture}[baseline,line width=0.1ex,line cap=round]
  12. \draw (0,0) -- (.7em,0)
  13. (0,.3ex) -- (.7em,.3ex)
  14. (.2em,.55ex) -- (.4em,1.8ex)
  15. (.35em,.55ex) -- (.55em,1.8ex);
  16. \end{tikzpicture}}}}

  17. %引用环境
  18. \mdfsetup{skipbelow=3pt plus 3pt minus 3pt}
  19. \newenvironment{squote}[1][引用]{\begin{mdframed}[%
  20. hidealllines=true,
  21. singleextra={
  22.   \draw[gray,line cap=rect] (O|-P) -- +(3cm,0pt);
  23.   \draw[gray,line cap=rect] (O|-P) -- +(0pt,-1cm);
  24.   \draw[gray,line cap=rect] (O-|P) -- +(-3cm,0pt);
  25.   \draw[gray,line cap=rect] (O-|P) -- +(0pt,1cm);
  26.   },
  27. firstextra={
  28.   \draw[gray,line cap=rect] (O|-P) -- +(3cm,0pt);
  29.   \draw[gray,line cap=rect] (O|-P) -- +(0pt,-1cm);
  30. },
  31. secondextra={
  32.   \draw[gray,line cap=rect] (O-|P) -- +(-3cm,0pt);
  33.   \draw[gray,line cap=rect] (O-|P) -- +(0pt,1cm);
  34. }]
  35. #1\par}{\end{mdframed}}
复制代码

七、选择题、填空题
(代码可能比较烂,欢迎批评)
先是选择题右边的括号以及填空题的横线:
  1. \newcommand\fkh[1][ ]{\nolinebreak\hfill\mbox{(#1}\nolinebreak )} %选择题括号
  2. \newcommand\tk[1][3]{\nolinebreak\mbox{\underline{\hspace{#1em}}}} %填空题横线,默认线长 3em
复制代码
然后是重点内容:选择题选项自动排列。
(这部分代码重写过,原先依赖 calc 包,现已不需要,原先的代码不但长,也没考虑到当选项长到一行写不完时,应当用 itemize 使换行有缩进,因而重写后的代码又依赖 enumitem 宏包)
  1. \newlength\onelen
  2. \newlength\twolen
  3. \newcommand\onech[4]{\par
  4. \parbox{\onelen}{A. #1}%
  5. \parbox{\onelen}{B. #2}%
  6. \parbox{\onelen}{C. #3}%
  7. \parbox{\onelen}{D. #4}\par
  8. }
  9. \newcommand\twoch[4]{\par
  10. \parbox{\twolen}{A. #1}%
  11. \parbox{\twolen}{B. #2}\par
  12. \parbox{\twolen}{C. #3}%
  13. \parbox{\twolen}{D. #4}\par
  14. }
  15. \newlength\bhwd
  16. \newcommand\fourch[4]{%
  17. \settowidth\bhwd{A. }\addtolength\bhwd{\parindent}%
  18. \begin{itemize}[nosep,labelsep=0pt,leftmargin=\bhwd]
  19. \item[A. ]#1
  20. \item[B. ]#2
  21. \item[C. ]#3
  22. \item[D. ]#4
  23. \end{itemize}}
  24. %
  25. %自动选择上述方式
  26. \newlength\xxwd
  27. \newcommand\xx[4]{%
  28. \settowidth\xxwd{\vbox{\halign{##\cr #1\cr #2\cr #3\cr #4\crcr}}}%
  29. \addtolength\xxwd{2.5em}%
  30. \setlength\onelen{\dimexpr(\linewidth-\parindent)/4\relax}%
  31. \setlength\twolen{\dimexpr(\linewidth-\parindent)/2\relax}%
  32. \ifdim\xxwd<\onelen\relax
  33. \onech{#1}{#2}{#3}{#4}%
  34. \else\ifdim\xxwd<\twolen\relax
  35. \twoch{#1}{#2}{#3}{#4}%
  36. \else\fourch{#1}{#2}{#3}{#4}%
  37. \fi\fi
  38. }
复制代码
用法:\xx{选项一}{选项二}{选项三}{选项四},将会自动选择合适的排列方式。
(这里仅限四个选项,如有其他数目的需求或更多变的格式,可考虑使用 tasks 宏包)


八、图片并排放置
  1. %单图 \onetu{图},“图”里面可以写 \caption 和 \label,下同
  2. \newcommand\onetu[1]{%
  3. \begin{figure}[!ht]
  4. \centering
  5. #1
  6. \end{figure}}

  7. \newcommand\mnpgb[2]{%
  8. \begin{minipage}[b]{\dimexpr#1\relax}
  9. \centering
  10. #2
  11. \end{minipage}}

  12. %两图(可调宽度比)\twotu[x]{图}{图},0<x<1为宽比例,默认0.5即平分
  13. \newcommand\twotu[3][0.5]{%
  14. \begin{figure}[!ht]
  15. \mnpgb{#1\linewidth}{#2}%
  16. \mnpgb{\linewidth-#1\linewidth}{#3}%
  17. \end{figure}}

  18. %三图、四图,\threetu{图}{图}{图},\fourtu{图}{图}{图}{图}
  19. \newcommand\threetu[3]{%
  20. \begin{figure}[!ht]
  21. \mnpgb{\linewidth/3}{#1}%
  22. \mnpgb{\linewidth/3}{#2}%
  23. \mnpgb{\linewidth/3}{#3}%
  24. \end{figure}}
  25. \newcommand\fourtu[4]{%
  26. \begin{figure}[!ht]
  27. \mnpgb{\linewidth/4}{#1}%
  28. \mnpgb{\linewidth/4}{#2}%
  29. \mnpgb{\linewidth/4}{#3}%
  30. \mnpgb{\linewidth/4}{#4}%
  31. \end{figure}}

  32. %一行四项的图形选项专用,\fourtuxx{图}{图}{图}{图}
  33. \newcommand\fourtuxx[4]{%
  34. \mnpgb{(\linewidth-\parindent)/4}{#1\par A}%
  35. \mnpgb{(\linewidth-\parindent)/4}{#2\par B}%
  36. \mnpgb{(\linewidth-\parindent)/4}{#3\par C}%
  37. \mnpgb{(\linewidth-\parindent)/4}{#4\par D}%
  38. }
复制代码

九、引号替换——输入 “  ” 输出 「  」
  1. \let\oldsyhl=“
  2. \let\oldsyhr=”
  3. \let\olddyhl=‘
  4. \let\olddyhr=’
  5. \catcode`\“=\active %或者=13
  6. \catcode`\”=\active
  7. \catcode`\‘=\active
  8. \catcode`\’=\active
  9. \newcommand{“}{「}
  10. \newcommand{”}{」}
  11. \newcommand{‘}{『}
  12. \newcommand{’}{』}
复制代码

十、begin{document} 之后
封面、前言之类的东西就不说了。
至于页眉页脚,ctex 的文档类默认已设置好,如果不喜欢,可以在目录之前自己重设,比如:
  1. \pagestyle{fancy}
  2. \fancyhf{}
  3. \fancyhead[RE]{\nouppercase\leftmark} %字母默认会全变大写,加 \nouppercase 使之保持原样
  4. \fancyhead[LO]{\nouppercase\rightmark}
  5. \fancyhead[LE,RO]{撸撸撸}
  6. \fancyfoot[LE,RO]{\thepage}
  7. \fancypagestyle{plain}{
  8. \fancyhf{}
  9. \fancyfoot[LE,RO]{\thepage} %将plain的页码设为与上面一致(新章页为plain)
  10. \renewcommand{\headrulewidth}{0pt}
  11. }
复制代码
最后就是目录,通常目录页码为罗马数字,所以大概可以这样:
  1. \pagenumbering{roman}
  2. \renewcommand\contentsname{目  录}
  3. \tableofcontents
  4. \cleardoublepage
  5. \pagenumbering{arabic}
复制代码
之后就可以开始正文啦。


完。
1

评分人数

分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
$\href{https://kuingggg.github.io/}{\text{About Me}}$

重发

TOP

说一些不足之处:

第八部分(图形并排放置)最后的 \fourtuxx 只能四图选项并排,缺少了 2x2 的图形选项,只是我书上没遇到过,所以当时懒得弄。

大题的小问(I)(II)这些,我书上是直接手写的,懒,其实应该定制一个环境用 enumerate 来写。

TOP

返回列表 回复 发帖