我正在为我们的研究所制作一个 LaTeX 模板。指南规定引用图形、方程式或表格时应采用一种不寻常的风格。第一次在文中提到它们时,必须加下划线。那么我该怎么做才能让它自动运行呢?我已经尝试了一些词汇表和首字母缩略词,但没有任何效果。
我很高兴收到你的来信,Bernte
答案1
以下是修改我的答案回答上一个问题,要求在第一次引用时添加边注。这使用该cleveref
包允许上下文感知引用,这使得在名称和数字下划线变得容易。
编辑:正如下面的@egreg所指出的,如果将引用列表传递给,则先前的答案将中断\cref
。为了纠正这个问题,现在检查传递的参数是否包含逗号。如果是,则无论引用如何,都不会执行下划线。换句话说,这种方法将在第一次单独进行引用时加下划线。
\documentclass{article}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage[left=1.5in,right=1in]{geometry}
\usepackage{cleveref}
\crefname{equation}{Equation}{Equations}
\crefname{figure}{Figure}{Figures}
\crefname{table}{Table}{Tables}
\usepackage{xstring}
\makeatletter
%Redefine \cref so that it will make a margin note if the passed label is referenced for the first time.
\let\oldcref=\cref
\def\cref#1{%
\IfSubStr{#1}{,}{\oldcref{#1}}{%
%check if the label has already been referenced.
\ifcsname marginnote@#1\endcsname
%already exists...therefore not the first citation
\oldcref{#1}%
\else
\underline{\oldcref{#1}}%}
\expandafter\gdef\csname marginnote@#1\endcsname{}%first citation...with this defined, will not underline again
\fi
}}
\makeatother
\author{John Doe}
\title{Automatic underlining of the first use of a reference}
\begin{document}
\maketitle
\begin{multicols}{2}
See \cref{fig:figure}. \lipsum[1]
A new reference to \cref{fig:figure} is not underlined.
Let's do the same with an equation.
\begin{equation}
\label{eq:1}f(x) = x^2
\end{equation}
Refering to \cref{eq:1} for the first time, there should be a marginal note, as is visible here, but it shoudl begin with ``Eq.'', not ``Fig.''.
Here we can test a list of references \cref{eq:1,tab:1,fig:figure} which should bypass the underling no matter the contents.
Adding another reference to \cref{eq:1} does not result in an underline; however, referring to \cref{tab:1} for the first time does.
\end{multicols}
% lets put a table and a figure
\begin{table}
\caption{Table title}
\label{tab:1}
\begin{tabular}{cc}
x & y \\ \hline
3 & 3 \\
4 & 4 \\ \hline
\end{tabular}
\end{table}
\begin{figure}
\centering\rule{150pt}{10pt}
\caption{A figure}\label{fig:figure}
\end{figure}
\end{document}