![按字母顺序显示 itemize 中的项目](https://linux22.com/image/271120/%E6%8C%89%E5%AD%97%E6%AF%8D%E9%A1%BA%E5%BA%8F%E6%98%BE%E7%A4%BA%20itemize%20%E4%B8%AD%E7%9A%84%E9%A1%B9%E7%9B%AE.png)
我想知道是否可以按字母顺序显示项目,而不管我们在后端输入的顺序如何?例如,我使用 itemize 如下:
\begin{itemize}
\item ISDYNSTP: Is dynamic time step used?
\item ISCDCA:
\item MVAR
\item IS2TL_
\end{itemize}
但我想按字母顺序显示项目。我不知道这是否可行。我有数百个逐项项目。实际上,我正在尝试为不同的参数创建一个词汇表。
答案1
从中获取一些代码如何对字母数字列表进行排序,对界面进行轻微的更改,以便通过datatool
包裹:
\documentclass{article}
\usepackage{datatool}% http://ctan.org/pkg/datatool
\newcommand{\sortitem}[1]{%
\DTLnewrow{list}% Create a new entry
\DTLnewdbentry{list}{description}{#1}% Add entry as description
}
\newenvironment{sortedlist}{%
\DTLifdbexists{list}{\DTLcleardb{list}}{\DTLnewdb{list}}% Create new/discard old list
}{%
\DTLsort{description}{list}% Sort list
\begin{itemize}%
\DTLforeach*{list}{\theDesc=description}{%
\item \theDesc}% Print each item
\end{itemize}%
}
\begin{document}
Default:
\begin{itemize}
\item ISDYNSTP: Is dynamic time step used ?
\item ISCDCA:
\item MVAR
\item IS2TL
\end{itemize}
Sorted:
\begin{sortedlist}
\sortitem{ISDYNSTP: Is dynamic time step used ?}
\sortitem{ISCDCA:}
\sortitem{MVAR}
\sortitem{IS2TL}
\end{sortedlist}
\end{document}
为了允许格式化已排序的元素,最好修改语法。为此,以下 MWE 提供了一个更新版本,\sortitem[<sort label>]{<label/description>}
它采用可选的<sort label>
(用作排序标签):
\documentclass{article}
\usepackage{datatool}% http://ctan.org/pkg/datatool
\newcommand{\sortitem}[2][\relax]{%
\DTLnewrow{list}% Create a new entry
\ifx#1\relax
\DTLnewdbentry{list}{sortlabel}{#2}% Add entry sortlabel (no optional argument)
\else
\DTLnewdbentry{list}{sortlabel}{#1}% Add entry sortlabel (optional argument)
\fi%
\DTLnewdbentry{list}{description}{#2}% Add entry description
}
\newenvironment{sortedlist}{%
\DTLifdbexists{list}{\DTLcleardb{list}}{\DTLnewdb{list}}% Create new/discard old list
}{%
\DTLsort{sortlabel}{list}% Sort list
\begin{itemize}%
\DTLforeach*{list}{\theDesc=description}{%
\item \theDesc}% Print each item
\end{itemize}%
}
\begin{document}
Default:
\begin{itemize}
\item ISDYNSTP: Is dynamic time step used ?
\item ISCDCA:
\item MVAR
\item IS2TL
\end{itemize}
Sorted:
\begin{sortedlist}
\sortitem{ISDYNSTP: Is dynamic time step used ?}
\sortitem[ISCDCA]{\textit{ISCDCA:}}
\sortitem[MVAR]{\textbf{MVAR}}
\sortitem{IS2TL}
\end{sortedlist}
\end{document}
如果你想要不区分大小写比较,只需将\DTLsort
命令替换为\DTLsort*
。