
我有一個由三個大的多頁表格組成的文檔,所有這些表格最後都有指向圖形列表中的項目的連結。透過在生成表的 Latex 時產生 md5 標籤,我可以包含返回原始表的連結。然而,連結到一個圖形的多個項目通常會出現在同一頁上,從而導致像這樣的怪物:
Figure 31 - This figure was referenced on pages 4, 4, 4, 14, 17, 17, 27, 30, 30, 6, 6, 6, 6, 6, 6, 8, 8, 8, 19, 19, 20, 20, 21, 21, 21, 21, 21, 32, 32, 33, 33, 34, 34, 34 34, and 34.
在產生的文件中,每個頁碼都是一個鏈接,指向表中引用該圖的行。
我想完成兩件事:
- 消除重複項,以便僅引用給定頁面上的第一個表格條目
- (可選)按頁碼對清單進行排序
我看過清單重複資料刪除問題這裡,但我是乳膠新手,我什至無法讓下面發布的最小工作示例實際工作:
\documentclass[letterpaper,11pt,openany,oneside]{book}
\makeatletter
\def\removeduplicates#1#2{\begingroup
\let\@tempa#1%
\def\@tempb{}%
\@for\next:=\@tempa\do
{\@ifundefined{lstel@\next}
{\edef\@tempb{\@tempb,\next}
\expandafter\let\csname lstel@\next\endcsname\@empty}
{}%
}%
\edef\x{\endgroup\def\noexpand#2{\@tempb}}\x
\expandafter\strip@comma#2\@nil#2}
\def\strip@comma,#1\@nil#2{\def#2{#1}}
\makeatother
\begin{document}
% some labels on page 1
\section{John} \label{john}
john doe
\section{Mary} \label{mary}
mary sue
%move to page 2
\clearpage
\section{George} \label{george}
george weasley
\section{Australia} \label{australia}
crocodile dundee
%new page, where the references are
\clearpage
\section{References}
\def\alist{\pageref{john},\pageref{mary},\pageref{george},\pageref{australia},\pageref{australia},\pageref{john}}
\removeduplicates\alist\blist
\show\blist
\show\alist
\removeduplicates\alist\alist
\show\alist
\end{document}
當我嘗試運行該範例(對連結中的範例進行簡單修改)時,每行都會出現大約十幾個錯誤\renewduplicates
,抱怨 Missing \endcsname
、 extra\endcsname
和 extra \else
。我不知道如何進行調試。
答案1
您正在使用\edef
且\pageref
不能在此上下文中使用,也不能出現在\csname...\endcsname
避免完全擴展將起作用,同時將\next
when in的第一級擴展進行字串化\csname...\endcsname
。
\providecommand{\expandonce}{\unexpanded\expandafter}
\makeatletter
\def\removeduplicates#1#2{\begingroup
\let\@tempa#1%
\def\@tempb{}%
\@for\next:=\@tempa\do
{\@ifundefined{lstel@\detokenize\expandafter{\next}}
{\edef\@tempb{\expandonce{\@tempb},\expandonce{\next}}
\expandafter\let\csname lstel@\detokenize\expandafter{\next}\endcsname\@empty}
{}%
}%
\edef\x{\endgroup\def\noexpand#2{\expandonce{\@tempb}}}\x
\expandafter\strip@comma#2\@nil#2}
\def\strip@comma,#1\@nil#2{\def#2{#1}}
\makeatother
然而,有一個更巧妙的方法:
\documentclass[letterpaper,11pt,openany,oneside]{book}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\removeduplicates}{mm}
{
\seq_set_split:NnV \l_tmpa_seq { , } #1
\seq_remove_duplicates:N \l_tmpa_seq
\tl_set:Nx #2 { \seq_use:Nn \l_tmpa_seq { , } }
}
\ExplSyntaxOff
\begin{document}
% some labels on page 1
\section{John} \label{john}
john doe
\section{Mary} \label{mary}
mary sue
%move to page 2
\clearpage
\section{George} \label{george}
george weasley
\section{Australia} \label{australia}
crocodile dundee
%new page, where the references are
\clearpage
\section{References}
\def\alist{\pageref{john},\pageref{mary},\pageref{george},%
\pageref{australia},\pageref{australia},\pageref{john}}
\removeduplicates\alist\blist
\show\blist
\show\alist
\removeduplicates\alist\alist
\show\alist
\end{document}
這是我的終端上的輸出:
> \blist=macro:
->\pageref {john},\pageref {mary},\pageref {george},\pageref {australia}.
l.37 \show\blist
?
> \alist=macro:
->\pageref {john},\pageref {mary},\pageref {george},\pageref {australia},\pager
ef {australia},\pageref {john}.
l.38 \show\alist
?
> \alist=macro:
->\pageref {john},\pageref {mary},\pageref {george},\pageref {australia}.
l.41 \show\alist
?
在這個級別上排序是不可能的。
如果您想要刪除頁碼重複項,而不是重複項,則需要提供\pageref{...}
的可擴充版本。我將展示兩個版本的程式碼。第一的:\pageref
refcount
expl3
\documentclass[letterpaper,11pt,openany,oneside]{book}
\usepackage{refcount}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\removeduplicates}{mm}
{
\clist_set:Nx \l_tmpa_clist { #1 }
\clist_remove_duplicates:N \l_tmpa_clist
\tl_set:NV #2 \l_tmpa_clist
}
\ExplSyntaxOff
\begin{document}
% some labels on page 1
\section{John} \label{john}
john doe
\section{Mary} \label{mary}
mary sue
%move to page 2
\clearpage
\section{George} \label{george}
george weasley
\section{Australia} \label{australia}
crocodile dundee
%new page, where the references are
\clearpage
\section{References}
\def\alist{\getpagerefnumber{john},\getpagerefnumber{mary},\getpagerefnumber{george},%
\getpagerefnumber{australia},\getpagerefnumber{australia},\getpagerefnumber{john}}
\removeduplicates\alist\blist
\show\blist
\show\alist
\removeduplicates\alist\alist
\show\alist
\alist
\blist
\end{document}
然後是“經典”版本:
\documentclass[letterpaper,11pt,openany,oneside]{book}
\usepackage{refcount}
\makeatletter
\def\removeduplicates#1#2{\begingroup
\let\@tempa#1%
\def\@tempb{}%
\@for\next:=\@tempa\do
{\@ifundefined{lstel@\next}
{\edef\@tempb{\@tempb,\next}%
\expandafter\let\csname lstel@\next\endcsname\@empty}
{}%
}%
\edef\x{\endgroup\def\noexpand#2{\@tempb}}\x
\expandafter\strip@comma#2\@nil#2}
\def\strip@comma,#1\@nil#2{\def#2{#1}}
\makeatother
\begin{document}
% some labels on page 1
\section{John} \label{john}
john doe
\section{Mary} \label{mary}
mary sue
%move to page 2
\clearpage
\section{George} \label{george}
george weasley
\section{Australia} \label{australia}
crocodile dundee
%new page, where the references are
\clearpage
\section{References}
\def\alist{\getpagerefnumber{john},\getpagerefnumber{mary},\getpagerefnumber{george},\getpagerefnumber{australia},\getpagerefnumber{australia},\getpagerefnumber{john}}
\removeduplicates\alist\blist
\show\blist
\show\alist
\removeduplicates\alist\alist
\show\alist
\blist
\alist
\end{document}
兩個版本都定義\blist
並重新定義\alist
為包含“1,2”。
使用內建清單列印頁面超連結的版本。
\documentclass[letterpaper,11pt,openany,oneside]{book}
\usepackage{refcount}
\usepackage{xparse}
\usepackage{hyperref}
\ExplSyntaxOn
\NewDocumentCommand{\createlinks}{m}
{
\seq_set_split:Nnx \l_tmpa_seq { , } { #1 }
\seq_remove_duplicates:N \l_tmpa_seq
\seq_clear:N \l_tmpb_seq
\seq_map_inline:Nn \l_tmpa_seq
{
\seq_put_right:Nn \l_tmpb_seq { \hyperlink { page.##1 } { ##1 } }
}
\seq_use:Nn \l_tmpb_seq { ,~ }
}
\cs_generate_variant:Nn \seq_set_split:Nnn { Nnx }
\ExplSyntaxOff
\begin{document}
% some labels on page 1
\section{John} \label{john}
john doe
\section{Mary} \label{mary}
mary sue
%move to page 2
\clearpage
\section{George} \label{george}
george weasley
\section{Australia} \label{australia}
crocodile dundee
%new page, where the references are
\clearpage
\section{References}
\def\alist{\getpagerefnumber{john},\getpagerefnumber{mary},\getpagerefnumber{george},%
\getpagerefnumber{australia},\getpagerefnumber{australia},\getpagerefnumber{john}}
\createlinks\alist
\end{document}