如何按数字顺序对列表进行排序?

如何按数字顺序对列表进行排序?

基本上,我想为历史课创建一个摘要。因此,整个讲座中会出现很多年份。

如果我按照以下示例进行设置,LaTeX 是否允许对列表进行排序?

\begin{description}
  \item[1912] Something good happened
  \item[1923] Something bad happened
  \item[1901] Something terrible happened
\end{description}

LaTeX 是否可以识别数字并正确地重新排序?

期望输出:

1901可怕的事情发生了

1912好事发生了

1923发生了一些不好的事情

LaTeX 能够在 bib 中做到这一点,那么它可以以某种方式扩展吗?

答案1

该方法通过使用文件上的外部列表,自动按年份或任何其他数字对项目进行排序.csv

\documentclass{article}
\usepackage{filecontents} % To create an external .csv file
\begin{filecontents}{\jobname.csv}
Order, Text
1912, Something good happened.
1923, Something bad happened.
1901, Something terrible happened.
\end{filecontents}

\usepackage{datatool}
\DTLloaddb{externalcsv}{\jobname.csv}

\begin{document}

\begin{enumerate}
\DTLsort{Order=ascending}{externalcsv}%
\DTLforeach{externalcsv}{\myorder=Order, \mytext=Text}{\item[\myorder.]\mytext}
\end{enumerate}

\end{document}

输出

在此处输入图片描述

答案2

glossaries您可以按照以下示例所示使用该包:

在此处输入图片描述

\documentclass{article}
\usepackage[automake, nonumberlist]{glossaries}

\newglossaryentry{1912}{name={1912}, description={Something good happened}}
\newglossaryentry{1923}{name={1923}, description={Something bad happened}}
\newglossaryentry{1901}{name={1901}, description={Something terrible happened}}
\makeglossaries
\begin{document}
\glsaddall % adds every defined term into the list
\gls{1912} % just adds the entry with the key 1912 t the list
\printglossary[title={List of Historic Events}]
\end{document}

答案3

如果您希望解决方案尽可能接近您的语法:

在此处输入图片描述

\documentclass{article}
\usepackage{expl3}
\usepackage{xparse}
\usepackage{enumitem}
\usepackage{xcolor}

\begin{document}

\ExplSyntaxOn

\seq_new:N \l_item_seq

\DeclareDocumentCommand{\oitem}{om}{
    \IfValueTF{#1} {\int_set:Nn \l_tmpa_int {#1}} {\int_set:Nn \l_tmpa_int {0}}
    \seq_put_right:Nx \l_item_seq {{\int_use:N \l_tmpa_int}{#2}}
}

\DeclareDocumentEnvironment{description}{o}{
    \seq_clear:N \l_item_seq
    \IfValueTF{#1}{\begin{itemize}[#1]}{\begin{itemize}}
}{  
    \seq_sort:Nn \l_item_seq {
        \int_compare:nNnTF { \tl_item:nn {##1} {1} } > { \tl_item:nn {##2} {1} }
        { \sort_return_swapped: }
        { \sort_return_same: }
     }
    \seq_map_variable:NNn \l_item_seq \l_tmpa_tl {
        \item[\tl_item:Nn \l_tmpa_tl {1}] \tl_item:Nn \l_tmpa_tl {2}
    }
    \end{itemize}
}

\ExplSyntaxOff

\begin{description}[left=0mm, font=\bfseries]
  \oitem[1912]{Something good happened}
  \oitem[1923]{Something bad happened}
  \oitem[1901]{Something terrible happened}
\end{description}


\begin{description}[left=0mm, font=\color{blue}\bfseries]
  \oitem[1945]{Something good happened}
  \oitem[1967]{Something bad happened}
  \oitem[1932]{Something terrible happened}
\end{description}

\end{document} 

相关内容