自动用括号括住示例数字

自动用括号括住示例数字

由于 LaTeX 默认会这样,对示例数字的引用\ref只会打印示例的数字,而无需额外的格式。但是,在我撰写的论文中,示例数字几乎总是应该用括号括起来,例如“(2)”,而不是“2”。(我引用的不是方程式,而是用 制作的语言示例gb4e;我不确定我见过的方程式引用的答案是否有效。)

有没有办法自动引入此功能,而不会破坏诸如此类的事情hyperref?此外,是否有任何方法可以避免在引用图形和表格时使用括号,而是在这些情况下输出一个简单的“2”?我对将图形引用放在括号中感到满意,因为手动将其他每个命令都放在括号中很麻烦\ref

以下是目前其工作原理的 MWE:

\documentclass{article}
\usepackage{gb4e}
\usepackage{hyperref}

\begin{document}

You can see the behaviour in example \ref{example}.

\begin{exe}
\ex\label{example} This is an example.
\end{exe}

\end{document}

答案1

一种方法是使用cleveref

\documentclass{article}
% Package loading order matters here.
% Load these packages in this order, after all other packages.
\usepackage{gb4e}
\usepackage{hyperref}
\usepackage[compress]{cleveref}
\crefname{xnumi}{}{}
\creflabelformat{xnumi}{(#2#1#3)}
\crefrangeformat{xnumi}{(#3#1#4)--(#5#2#6)}

\begin{document}
As we can see in \cref{lingex} the parentheses are correctly placed.
\begin{exe}
\ex\label{lingex}
\begin{xlist}
\ex An example.
\ex A second example.
\end{xlist}
\end{exe}
\begin{exe}
\ex\label{secondex}
\begin{xlist}
\ex Another example.
\ex And another.
\end{xlist}
\end{exe}
We can also refer to groups of examples like as shown in \cref{lingex,secondex} or we can refer to a range as in \crefrange{lingex}{secondex}.

This is a reference to \Cref{table}. Notice how it is formatted differently.

\begin{table}[hb]
\centering
\begin{tabular}{c}
Pretend this is a table.
\end{tabular}
\caption{A table caption\label{table}}
\end{table}

\end{document}

代码输出

相关内容