
我正在處理一個需要在樣式開頭有交叉引用部分的文件:
- 第1.1、1.2、1.3段的證據1
- 證據2見第1.2、2.3、3.4段
ETC
我的文件格式良好,並且可以完全按照我希望的方式使用\label
和引用各個段落\ref
。
但是,我希望能夠多次定義一個標籤,以便在\ref
呼叫時列印所有標籤的清單 - 而不僅僅是最後一個標籤(如當前所做的那樣)。
我已經考慮過為此使用索引,但似乎無法讓它以其簡單的格式工作,更不用說自訂輸出了。
無論如何,是否允許\label
多次定義?
答案1
\documentclass{article}
\makeatletter
\def\@newl@bel#1#2#3{{%
\@ifundefined{#1@#2}%
{\def\tmp{#3}}%
{%
\edef\tmp{%
{\expandafter\expandafter\expandafter\@firstoftwo\csname#1@#2\endcsname,
\@firstoftwo#3}%
{\expandafter\expandafter\expandafter\@secondoftwo\csname#1@#2\endcsname,
\@secondoftwo#3}%
}}%
\expandafter\xdef\csname#1@#2\endcsname{\tmp}%
}}
\def\@testdef #1#2#3{}
\makeatother
\begin{document}
good stuff in section(s) \ref{a}
bad stuff in section(s) \ref{b}
\section{zz}\label{a}
zz
\section{zzz}\label{a}
zzz
\section{zzzz}\label{a}
zzzz
\section{aaa}\label{b}
aaa
\section{zzzzz}\label{a}
zzzzz
\end{document}
答案2
您不一定必須允許多個標籤。除了 @DavidCarlisle 很酷的低階駭客之外,我還提供了這個解決方案(本質上是一個名為 的自訂交叉引用巨集\eref{<evidence no>}{<label>}
),它循環遍歷內部創建的清單:
\documentclass{article}
\usepackage{etoolbox}
\usepackage{lipsum}
\makeatletter
\newcounter{para}
\renewcommand{\thepara}{\thesection.\@arabic\c@para}
\let\svd@paragraph=\paragraph
\renewcommand{\paragraph}{\refstepcounter{para} \svd@paragraph}
\newcommand{\eref}[2]{%
\ifcsundef{evlist#1}{\@namedef{evlist#1}{}}{}
\def\process{\def\process{,}}%
\def\do##1{\process\ref{##1}}%
\expandafter\listadd\csname evlist#1\endcsname{#2}%
Evidence #1 found at paragraph
\expandafter\dolistloop\csname evlist#1\endcsname}
\makeatother
\begin{document}
\section{First Section}
\paragraph{One}\label{para:1}
\lipsum[1]
\eref{1}{para:1}
\paragraph{Two}\label{para:2}
\lipsum[2]
\eref{1}{para:2}
\paragraph{Three}\label{para:3}
\lipsum[3]
\eref{2}{para:3}
\end{document}
\paragraph
(請注意,如果您的文件結構中沒有使用,則沒有 use 第 6-9 行。)