我想按标题的字母顺序(而不是按出现的顺序)对图表列表进行排序。
就像是
List of figures
4 Aaaa 3
1 Bbbbb 1
8 Ddd dd 10
有办法吗?
答案1
一个latex3
实现,用于\tex_strcmp:D
比较字符串。
- 这是从pdftex 或xetex 中的
\tex_strcmp:D
primitive 中获取的 latex3 别名。它需要两个参数,如果 arg1 > arg2,则扩展为 1,如果 arg1 == arg2,则扩展为 0,否则扩展为 -1。\pdfstrcmp
\strcmp
tocbibind
2020 年 5 月 18 日更新:它现在与(重新定义\listoffigures
)和hyperref
(重新定义\contentsline
)兼容
\documentclass{article}
\usepackage{tocbibind}
\usepackage{hyperref}
\usepackage{xpatch}
\ExplSyntaxOn
\makeatletter
% a seq of clist "<sort key>,<orig contents line>"
\seq_new:N \g_lof_seq
% backup
\cs_set_eq:NN \contentsline_orig \contentsline
\cs_set_eq:NN \numberline_orig \numberline
% patch \@starttoc to insert two hooks
\xpatchcmd \@starttoc
{ \begingroup }
{
\begingroup
\tl_if_eq:nnT {#1} {lof}
{ \hook_before_starttoc: }
}
{}{\fail}
\xpatchcmd \@starttoc
{ \endgroup }
{
\tl_if_eq:nnT {#1} {lof}
{ \hook_after_starttoc: }
\endgroup
}
{}{\fail}
% hook inserted at the beginning of \@starttoc
\cs_new:Nn \hook_before_starttoc:
{
\seq_clear:N \g_lof_seq
\cs_set_eq:NN \contentsline \contentsline_new
\cs_set_eq:NN \numberline \numberline_new
}
% hook inserted at the end of \@starttoc
\cs_new:Nn \hook_after_starttoc:
{
% for i in seq, sort by the top element of i
\seq_sort:Nn \g_lof_seq
{
\clist_set:Nn \l_tmpa_clist {##1}
\clist_set:Nn \l_tmpb_clist {##2}
\clist_get:NN \l_tmpa_clist \l_tmpa_tl
\clist_get:NN \l_tmpb_clist \l_tmpb_tl
\int_compare:nNnTF
% \tex_strcmp:D is the primitive \pdfstrcmp in pdftex
% or \strcmp in xetex
{ \exp_args:NVV \tex_strcmp:D \l_tmpa_tl \l_tmpb_tl } = 1
{ \sort_return_swapped: }
{ \sort_return_same: }
}
% restore
\cs_set_eq:NN \contentsline \contentsline_orig
\cs_set_eq:NN \numberline \numberline_orig
% output
\seq_map_inline:Nn \g_lof_seq
{
\clist_set:Nn \l_tmpa_clist {##1}
\clist_item:Nn \l_tmpa_clist {2}
}
}
% variant definition of \contentsline
% for every <contents line>, store clist of "<title>,<orig contents line>" in \g_lof_seq
\@ifpackageloaded{hyperref}
{
% eg, \contentsline{figure}{\numberline{3}{\ignorespaces Aaa}}{2}{figure.3}%
\cs_new:Npn \contentsline_new #1#2#3#4
{
% eg, #2 == \numberline{3}{\ignorespaces Aaa}
#2
\clist_clear:N \l_tmpa_clist
\clist_put_right:NV \l_tmpa_clist \l_tmpa_tl
\clist_put_right:Nn \l_tmpa_clist
{ \contentsline{#1}{#2}{#3}{#4} } % different here
\seq_gput_right:NV \g_lof_seq \l_tmpa_clist
}
}
{
% eg, \contentsline{figure}{\numberline{3}{\ignorespaces Aaa}}{2}%
\cs_new:Npn \contentsline_new #1#2#3
{
% eg, #2 == \numberline{3}{\ignorespaces Aaa}
#2
\clist_clear:N \l_tmpa_clist
\clist_put_right:NV \l_tmpa_clist \l_tmpa_tl
\clist_put_right:Nn \l_tmpa_clist
{ \contentsline{#1}{#2}{#3} } % different here
\seq_gput_right:NV \g_lof_seq \l_tmpa_clist
}
}
% variant definition of \numberline, store title part in \l_tmpa_tl
\cs_new:Npn \numberline_new #1#2
{
\tl_set:Nn \l_tmpa_tl {#2}
}
\makeatother
\ExplSyntaxOff
\begin{document}
\renewcommand{\listfigurename}{List of Figures (sorted by title)}
\listoffigures
\newpage
\begin{figure} content \caption{Ddd} \end{figure}
\begin{figure} content \caption{Bbb} \end{figure}
\begin{figure} content \caption{Aaa} \end{figure}
\begin{figure} content \caption{Ccc} \end{figure}
\end{document}