我正在用 latex 写一份小型财务报告...整个报告中有几个expenses
重复的字样,例如food
,,,等等。我认为为它们制作标签会很方便hotels
。train tickets
例如,我第一次写入 时food
,可以food
在文件中靠近它的地方写一个标签tex
(例如\labelexpenses{food}
);当我写入 时hotels
,我会\labelexpenses{hotels}
在它旁边写入。然后稍后,通过Expense~\refexpenses{food}
在 tex 文件中写入类似的东西,它可以显示在 pdf 中Expense 1
;Expense~\refexpenses{hotels}
在 tex 文件中生成Expense 2
。数字1
和2
由系统生成。
所以它与\label{...}
和非常相似\ref{...}
,但保留我自己可以定义的东西,数字将由系统自动生成。
有人知道如何实现这一点吗?
答案1
我猜你的意思是这样的:
\documentclass{article}
\usepackage{hyperref}
\newcounter{refexp}
\setcounter{refexp}{0}
\newcommand{\labelexpenses}[1]{%
\phantomsection
\refstepcounter{refexp}%
\label{#1}%
}
\newcommand{\refexpenses}[1]{%
\ref{#1}%
}
\begin{document}
For instance, the first time I write food, I can write a label food
close to it in the tex file (for example
\verb|\labelexpenses{food}|\labelexpenses{food}); when I write hotels,
I write \verb|\labelexpenses{hotels}|\labelexpenses{hotels} next to it.
Then later, by writing something like Expense~\refexpenses{food} in the
tex file, it could show in the pdf Expense 1; Expense~\refexpenses{hotels}
in the tex file generates Expense 2. The numbers 1 and 2 are generated by
the system.
\end{document}
答案2
以下是另一个类似的选项:
\documentclass{article}
\usepackage{booktabs}
\newcounter{expensecntr}
\renewcommand{\theexpensecntr}{(\arabic{expensecntr})}
\newcommand{\labelexpense}[1]{%
\refstepcounter{expensecntr}%
\label{expense:#1}%
#1~\theexpensecntr}
\newcommand{\refexpense}[1]{\ref{expense:#1}}
\begin{document}
\begin{tabular}{lr}
\toprule
\textbf{Expense} & \textbf{Total} \\
\midrule
\labelexpense{Food} & abc \\
\labelexpense{Hotel} & def \\
\labelexpense{Gas} & ghi \\
\bottomrule
\end{tabular}
Expense~\refexpense{Hotel} is really expensive.
\end{document}
答案3
过去我曾这样做过:
\documentclass{article}
\usepackage{etoolbox}
\usepackage{hyperref}
\makeatletter
% save text as a \ref-erencable reference in the auxfile
% usage: \labelledText{label}{text} --> \ref{label} recalls it
\newrobustcmd{\labelledText}[2]{#2\edef\@currentlabel{#2}\label{#1}}
\makeatother
\begin{document}
Here is something that I want to refer to later
\labelledText{later}{101}
And here I go using \ref{later} again.
\newcounter{test}
Saving test to none: \labelledText{none}{\thetest}.
\addtocounter{test}5
Saving test to five: \labelledText{five}{\thetest}.
Using the saved values of none=\ref{none} and five=\ref{five}.
\end{document}
输出如下:
因此,您可以使用\labelledText{mylabel}{some text}
打印some text
并为其分配标签mylabel
。之后,您可以使用\ref{mylabel}
重新打印结果。
不管有没有输出,这都可以工作超链接。