Home » Blog » Generating Flashcards with Linux and Latex

Generating Flashcards with Linux and Latex 用 Linux 和 Latex 生成记忆卡

If you get bored of those fancy electronic ways of memorise words or other stuff and would like to resort to the old fashioned flashcards, here with the help of Linux and Latex, it is just in a few steps.

First, stuff everything you would like to memorise into a text file say listfile, which contains a list of items in the following format: A [TAB] B whrere A is to show on the front of the flashcard and B is to show on the back, [TAB] stands for a horitontal tab.

Then, execute the following script (better to save it into a file, say flashcard_gen.sh) ./flashcard_gen.sh listfile > latexfile.tex

# Argument checking
if [ "$#" -ne 1 ]; then 
    echo "Usage:" $0 "filename"
    exit 1
fi
# Print the LaTeX headers
echo "\documentclass[xkcard,grid,frame]{flashcards}"
echo "\cardfrontstyle[\LARGE\bf]{headings}"
echo "\cardbackstyle[\LARGE\slshape]{plain}"
echo "% turn of those nasty overfull and underfull hboxes"
echo "\hbadness=10000"
echo "\hfuzz=50pt"
 
echo "\begin{document}"
 
# Read the file provided
while IFS=$'\t' read -r -a arr
# The -r tells read that \ isn't special in the input data; 
# The -a arr tells it to split the input-line into words and store the results in arr; 
# The IFS=$'\t' tells it to use only tabs to split words, instead of the regular Bash default of also allowing spaces to split words as well.
do
    echo "\begin{flashcard}{${arr[0]}}" 
    echo ${arr[1]}
    echo "\end{flashcard}"
done < $1
 
echo "\end{document}"
exit 0

Then compile the generated tex file and print out the pdf file, cut along the grids and everything is DONE.

A latex package called flashcards is required and can be downloaded from http://www.ctan.org/tex-archive/macros/latex2e/contrib/flashcards.

A zip file containing everything can be downloaded from here.