2

I am not fully certain how to word my question, so I may also have missed already existing answers about it and I am pretty unsure about my tagging.

So I have two different ideas that I think may be answered by the same type of techniques

  • One is building a document that is a vocabulary list, with for instance columns "word", "category" (verb, noun, adjective,...), "example of use", "meaning". I can make a nice longtable for that, but I would like a simple way to sort this table, either by alphabetical order, or by category. Like a compilation option, or just something I could change by option=1 or option=2
  • The second one is a bit more complex, yet the idea is the same : Still for a language lesson, I have a course that in which each chapter is organized always in the same way :

    • 1 Vocabulary
    • 2 Concepts
    • 3 Text
    • 4 Grammar

    I would like en easy way to have either the document organized by chapter, or by category, like every vocabulary sections, then every concepts and so on.

Is something like this feasible ?

1 Answers1

1

One possibility is to use datatool. The data could be stored in a CSV file. For example:

Word,Category,Example,Meaning
jump,verb,"The cow jumped over the moon.","To project one's self into the air through the muscular action of feet and legs."
cat,noun,"The cat sat on the mat.","feline animal"
hungry,adjective,"The hungry duck ate some corn.","desire for food"

If the file is called vocab.csv, then it can be loaded using:

\DTLloaddb{vocab.csv}{vocab}

You can then sort it according to a particular column using \DTLsort. For example, to sort on the Word column:

\DTLsort{Word}{vocab}

You can then iterate over the data using \DTLforeach. For example:

\documentclass{article}

\usepackage{array}
\usepackage{longtable}
\usepackage{datatool}

\DTLloaddb{vocab}{vocab.csv}

\begin{document}

\section*{Vocabulary List}
\DTLsort{Word}{vocab}% sort the data
\begin{longtable}{ll>{\raggedright}p{.25\linewidth}>{\raggedright}p{.25\linewidth}}
\bfseries Word &
\bfseries Category &
\bfseries Example &
\bfseries Meaning
\DTLforeach*{vocab}% database label
 {\Word=Word,\Category=Category,\Example=Example,\Meaning=Meaning}% placeholder assignments
 {\tabularnewline\Word & \Category & \Example & \Meaning}
\end{longtable}

\section*{Vocabulary List (by Category)}
\DTLsort{Category}{vocab}% sort the data
\begin{longtable}{ll>{\raggedright}p{.25\linewidth}>{\raggedright}p{.25\linewidth}}
\bfseries Word &
\bfseries Category &
\bfseries Example &
\bfseries Meaning
\DTLforeach*{vocab}% database label
 {\Word=Word,\Category=Category,\Example=Example,\Meaning=Meaning}% placeholder assignments
 {\tabularnewline\Word & \Category & \Example & \Meaning}
\end{longtable}

\end{document}

(Change the argument of \begin{longtable} and the table header as required.)

imaeg of document

Since this uses TeX to sort, it can be very slow for large databases. In which case, you may want to consider using datatooltk to perform the sorting. (There are some examples in Sorting Data.)

You can adapt this method for the second part of your example. (Include extra columns for Concepts, Text etc, iterate over the data using \DTLforeach* and use the placeholders defined in the command=header list.)

Nicola Talbot
  • 41,153
  • I am mainly using an oline editor, so I will try to see if using a csv file is easy. Not 100% sure how to adapt for my second part though, but I will come back sking for help later ! – Anthony Martin Jun 07 '17 at 13:41