14

Basically, I want to create a summary for a history class. Therefore a lot of years occur throughout the lectures.

Does LaTeX allow to sort a list if I set it up like in the following example?

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

Is it possible that LaTeX recognizes the number and reorders it correctly?

Desired output:

1901 Something terrible happened

1912 Something good happened

1923 Something bad happened

LaTeX is able to do so in the bib, so can it be extended somehow?

New contributor
Yves is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Do you plan to have events with the same date? Or, in general, items with the same numeric label? – egreg Jul 31 at 14:09
  • Just curious why a simple text editor "by line sort" on a selection would not work? Are you reading in dates from a file? Do you update the list quite often? – William 'Ike' Eisenhauer 3 hours ago
21

This approach automatically orders the items by year or any other number, by using an external list on an .csv file.

\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}

Output

enter image description here

| improve this answer | |
  • 2
    thanks a lot! I wasn't aware that one can read in csv files:) – Yves Jul 30 at 9:04
11

You could make use of the glossaries package as shown in the following example:

enter image description here

\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}
| improve this answer | |
9

If you prefer the solution to be as close to your syntax as possible:

enter image description here

\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} 
| improve this answer | |
  • Alternatively, you can get environment contents by b arg-spec in xparse, then split it to a l3seq by \item. – muzimuzhi Z Jul 31 at 13:09

Your Answer

Yves is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.