![2df5f65b225f204f0798b0ba69631794.png](https://i-blog.csdnimg.cn/blog_migrate/e7b02931d84a7c948a860abc8b0b2134.jpeg)
有时候你需要自制一份课件PPT或者手写笔记的关键词索引,用来开卷考试查阅PPT或者复习查阅笔记时能快速定位查阅内容位置,这时你可能想要一个按字母顺序排序的列表来帮助构建需要的索引。
LaTeX自身的index
本身并不支持这样的功能,修改起来也颇为麻烦,这里使用datatool
包,创建一个数据库,然后添加条目,最后排序输出。
创建list
数据库:
DTLifdbexists{list}{DTLcleardb{list}}{DTLnewdb{list}}
定义添加行的命令sortitem
newcommand{sortitem}[2]{%
DTLnewrow{list}%
DTLnewdbentry{list}{label}{#1}%
DTLnewdbentry{list}{description}{#2}%
}
定义条目输出命令showlist
,这里使用列表输出,用到了
![793d5f1b31bb2122c56ceaf10378230a.png](https://i-blog.csdnimg.cn/blog_migrate/6157c50b13fab7c4eadf84af4f4bc643.png)
中定义的myright
命令来右对齐标记(页码或者其他),以及DTLforeach
命令来遍历数据库中的条目
newcommandshowlist{
begin{itemize}
DTLforeach*{list}{theLabel=label,theDesc=description}{%
item {sffamilybfseriestheLabel}myright{theDesc}
}%
end{itemize}%
}
最后定义sortedlist
环境来收集条目,录入数据库
newenvironment{sortedlist}{%
DTLifdbexists{list}{DTLcleardb{list}}{DTLnewdb{list}}%
}{%
setlengthcolumnseprule{0.4pt}
begin{paracol}{2}
section*{Original order}
showlist
DTLsort{label}{list}%
switchcolumn
section*{Alphabetical order}
showlist
end{paracol}
}
测试一下
begin{sortedlist}
sortitem{software engineering difficulties}{1-3}
sortitem{requirements analysis}{1-8}
sortitem{design and architecture}{0-1}
sortitem{architecture-centric design}{0-2}
sortitem{non-functional properties and design}{0-3}
end{sortedlist}
输出正常
![e3aca41a20c3a53529484706f4ef4043.png](https://i-blog.csdnimg.cn/blog_migrate/eaf903ed7d28e2c2965423a28c28d3f1.jpeg)
完整代码:
documentclass[a4paper,12pt]{article}
usepackage{datatool}
usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}
usepackage{paracol}
usepackage{enumitem}
defmyright#1{%
{unskipnobreakhfilpenalty50
hskip1emhbox{}nobreakhfil{itshape #1}
parfillskip=0pt finalhyphendemerits=0 par}}
newcommand{sortitem}[2]{%
DTLnewrow{list}%
DTLnewdbentry{list}{label}{#1}%
DTLnewdbentry{list}{description}{#2}%
}
newcommandshowlist{
begin{itemize}
DTLforeach*{list}{theLabel=label,theDesc=description}{%
item {sffamilybfseriestheLabel}myright{theDesc}
}%
end{itemize}%
}
newenvironment{sortedlist}{%
DTLifdbexists{list}{DTLcleardb{list}}{DTLnewdb{list}}%
}{%
setlengthcolumnseprule{0.4pt}
begin{paracol}{2}
section*{Original order}
showlist
DTLsort{label}{list}%
switchcolumn
section*{Alphabetical order}
showlist
end{paracol}
}
begin{document}
begin{sortedlist}
sortitem{software engineering difficulties}{1-3}
sortitem{requirements analysis}{1-8}
sortitem{design and architecture}{0-1}
sortitem{architecture-centric design}{0-2}
sortitem{non-functional properties and design}{0-3}
end{sortedlist}
end{document}