Skip to content

Instantly share code, notes, and snippets.

@anamewing
Last active March 24, 2025 10:53
Show Gist options
  • Save anamewing/5154b6278f63e8d738bb5d57413a923f to your computer and use it in GitHub Desktop.
Save anamewing/5154b6278f63e8d738bb5d57413a923f to your computer and use it in GitHub Desktop.
Add Simplified Chinese translation to cleveref.
\crefname{equation}{式}{式}
\crefname{figure}{图}{图}
\crefname{table}{表}{表}
\crefname{page}{页}{页}
\crefname{chapter}{章}{章}
\crefname{section}{节}{节}
\crefname{appendix}{附录}{附录}
\crefname{theorem}{定理}{定理}
\crefname{lemma}{引理}{引理}
\crefname{corollary}{推论}{推论}
\crefname{proposition}{命题}{命题}
\crefname{definition}{定义}{定义}
\crefname{example}{例}{例}
\crefname{algorithm}{算法}{算法}
\crefname{listing}{列表}{列表}
\crefname{line}{行}{行}
\crefformat{chapter}{#2第#1章#3}
\crefformat{section}{#2第#1节#3}
\crefformat{subsection}{#2第#1节#3}
\crefformat{subsubsection}{#2第#1节#3}
\crefrangeformat{chapter}{#3第#1章#4至#5第#2章#6}
\crefrangeformat{section}{#3第#1节#4至#5第#2节#6}
\crefrangeformat{subsection}{#3第#1节#4至#5第#2节#6}
\crefrangeformat{subsubsection}{#3第#1节#4至#5第#2节#6}
\crefmultiformat{chapter}{#2第#1章#3}{和#2第#1章#3}{,#2第#1章#3}{和#2第#1章#3}
\crefmultiformat{section}{#2第#1节#3}{和#2第#1节#3}{,#2第#1节#3}{和#2第#1节#3}
\crefmultiformat{subsection}{#2第#1节#3}{和#2第#1节#3}{,#2第#1节#3}{和#2第#1节#3}
\crefmultiformat{subsubsection}{#2第#1节#3}{和#2第#1节#3}{,#2第#1节#3}{和#2第#1节#3}
\crefrangemultiformat{chapter}{#3第#1章#4至#5第#2章#6}{和#3第#1章#4至#5第#2章#6}{,#3第#1章#4至#5第#2章#6}{和#3第#1章#4至#5第#2章#6}
\crefrangemultiformat{section}{#3第#1节#4至#5第#2节#6}{和#3第#1节#4至#5第#2节#6}{,#3第#1节#4至#5第#2节#6}{和#3第#1节#4至#5第#2节#6}
\crefrangemultiformat{subsection}{#3第#1节#4至#5第#2节#6}{和#3第#1节#4至#5第#2节#6}{,#3第#1节#4至#5第#2节#6}{和#3第#1节#4至#5第#2节#6}
\crefrangemultiformat{subsubsection}{#3第#1节#4至#5第#2节#6}{和#3第#1节#4至#5第#2节#6}{,#3第#1节#4至#5第#2节#6}{和#3第#1节#4至#5第#2节#6}
\newcommand{\crefpairconjunction}{~和~}
\newcommand{\crefmiddleconjunction}{, }
\newcommand{\creflastconjunction}{~和~}
\newcommand{\crefpairgroupconjunction}{~和~}
\newcommand{\crefmiddlegroupconjunction}{, }
\newcommand{\creflastgroupconjunction}{~和~}
\newcommand{\crefrangeconjunction}{~至~}
@gerwang
Copy link

gerwang commented Mar 24, 2025

空格是个比较麻烦的问题,目前若是加载了 hyperref 包的话,CTeX 包可以在 Cleveref 引用时生成的“图 4”的两个字符之间自动加空格,但是不能在4的后面自动加空格。去掉 hyperref 包之后一切正常。 也可以用下面的正则匹配下,然后手动替换空格。

(\\[cC]ref\{((fig:|tab:|algo:|thm:|cha:appendix:|sub:|ssub:)[\w\d:-]+,)*((fig:|tab:|algo:|thm:|cha:appendix:|sub:|ssub:)[\w\d:-]+)\})(?!。|:|?|,| |\n|)|(|;|\\|([a-z]\)))

有一个折中的解决方法,可以在4的后面自动加空格(仍然保留 hyperref 包)。方法如下:
首先导入 xspace

\usepackage{xspace}

然后再定义一个新的命令为 \cleveref(命令的名字可以任意改)

% xspace 原本只适配英文标点,使用 xspaceaddexceptions 手动适配中文标点
\xspaceaddexceptions{、,。?!:;)}
\newcommand{\cleveref}[1]{\cref{#1}\xspace}

这样引用的时候输入\cleveref{标签label}即可自动加空格。
我目前的方案是将上面的命令 \cleveref 直接替换掉原来的 \ref,实现如下:同样需要导入 xspace,但由于 hyperref 包的作用,不能在 document 结构前让 \ref 失效,故需要在 document 结构后定义,即:

\usepackage{xspace}

\begin{document}

\xspaceaddexceptions{、,。?!:;)}
\newcommand{\cleveref}[1]{\cref{#1}\xspace}
\let\ref\relax
\newcommand{\ref}[1]{\cleveref{#1}}

正文

\end{document}

最后十分感谢 @anamewing 对中文的适配

直接在所有\cref后面加\xspace并不完全可行,因为“图1”后面要加空格,但“第1章”后面并不需要加。理论上应该只在以数字结尾的引用后面加 \xspace

\crefformat{equation}{式~(#2#1#3)\xspace}
\crefformat{figure}{图~#2#1#3\xspace}
\crefformat{table}{表~#2#1#3\xspace}

我的思路是为“图1”、“表1”这类需要加\xspace的修改\crefformat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment