当在ctexset中将number={}时如何使用\ref?

2021-02-05 20:44发布

## 编译环境 操作系统 * [ ] Windows 7/8/10 * [ ] macOS * [x ] Linux Tex发行版 * [x ] TexLive `2020` * [ ] MikTe...

## 编译环境 操作系统 * [ ] Windows 7/8/10 * [ ] macOS * [x ] Linux Tex发行版 * [x ] TexLive `2020` * [ ] MikTeX `版本号` * [ ] CTeX ## 我的问题 以前用[**ChenLaTeXBookTemplate**](https://github.com/latexstudio/ChenLaTeXBookTemplate "**ChenLaTeXBookTemplate**")书籍LaTeX模板写的东西,需要添加对**section**的引用,发现用`\ref`无法得到编号。 经查,原因是其`\ctexset`中`section/number={}`设置的原因造成的,但这个设置又是必须的,否则无法使用其自定义的章节标题盒子样式。 整理了一段MWE: ```tex \documentclass{ctexbook} \usepackage{zhlipsum} \ctexset{ section = { number = {}, }, } \begin{document} \chapter{安装软件} \section{下载}\label{sec-down} \zhlipsum[1] \section{解压}\label{sec-unzip} \zhlipsum[2],参见第\ref{sec-down}节 \section{安装}\label{sec-inst} \zhlipsum[3],参见第\ref{sec-test}节 \section{测试}\label{sec-test} \zhlipsum[4],参见第\ref{sec-inst}节 \end{document} ``` 结果为: ![](https://wenda.latexstudio.net/data/attach/210205/54ghCRzG.png)
4条回答
预言书
2021-02-07 10:43 .采纳回答
## 这是不合理的 首先这么做是**不合理**的, 因为 `\ref` 想引用也不知道要去引用什么, 是引用 `1.1` 还是 `1` 还是 `1.foo bar baz 1` 呢? 在你写 `\ctexset{section/number={}}` 的时候应该知道它不会提供形如 `1.1` 的引用, 你不能让它去做连你都不知道可能会输出什么的事. ### 原理 `\ref` 会将编号写入 `aux` 文件, 例如 ```latex \documentclass{ctexbook} \ctexset{section/number=\thechapter.\arabic{section}} \begin{document} \chapter{testchapter} \section{testsection}\label{sec:test} \end{document} ``` 会写入 `.aux` 文件 ```latex ... \newlabel{sec:test}{{1.1}{1}} \gdef \@abspage@last{1} ``` 当你使用 `\ref` 的时候, 会将 `\newlabel{sec:test}{{1.1}{1}}` 中的 `1.1` 写到 `\ref` 的位置. 而 `\ctexset{section/number={}}` 只会保留计数器 `section` 的值, 并不写入引用的内容(因为没有东西可以引用), 如你提供的文件编译后会有 `.aux` 文件 ```latex ... \newlabel{sec-down}{{}{1}} \@writefile{toc}{\contentsline {section}{\numberline {}解压}{1}{}\protected@file@percent } \newlabel{sec-unzip}{{}{1}} \@writefile{toc}{\contentsline {section}{\numberline {}安装}{2}{}\protected@file@percent } \newlabel{sec-inst}{{}{2}} \@writefile{toc}{\contentsline {section}{\numberline {}测试}{2}{}\protected@file@percent } \newlabel{sec-test}{{}{2}} \gdef \@abspage@last{3} ``` 并没有东西可以被写进 `\ref`. ## 替代方案 如果你想在不写编号的同时还能实现对章节的引用, 可以使用 `nameref` 宏包. 例子 ```latex \documentclass{ctexbook} \ctexset{section/number={}} \usepackage{hyperref, nameref} % 这里用 hyprref 是为了高亮显示引用的部分 \begin{document} \chapter{testchapter} \section{testsection}\label{sec:test} \nameref{sec:test} \Nameref{sec:test} \end{document} ``` 效果 ![](https://wenda.latexstudio.net/data/attach/210207/BwHxLOmb.png) --- **补充回答** 我刚刚在 (https://github.com/latexstudio/ChenLaTeXBookTemplate) clone 了 ChenLaTeXTemplate, 发现其并没有对 `section` 进行 `\ctexset{section/number={}}` 的设置, 或许您可以尝试更新一下模板?在 `tjbook` 的 95 行只找到了 ```latex ... %-------------------------------------------------------------------------------- % 章节格式设置 %-------------------------------------------------------------------------------- \ctexset { chapter = { beforeskip = 0pt, fixskip = true, format = \huge\bfseries, name = {,}, nameformat = \mbox{}\hfill\chapternamebox, number = \arabic{chapter}, numberformat = \fontencoding{OT1}\fontfamily{cmss}\fontsize{60pt}{72pt}\selectfont, aftername = \par\medskip, titleformat = \chaptertitlebox, aftertitle = \par\smallskip }} ``` 并且输出的 ![](https://wenda.latexstudio.net/data/attach/210208/yUHbxQpn.png) 也没有问题.

一周热门 更多>