BaroqueW

BaroqueW

and his sidekick nikkitaa

BaroqueW RSS Feed
 
 
 
 

Latex - How to create a glossary?

To ease the reading of your technical reports, that usually end up being crowded with abbreviations, it’s good to add a glossary to them. Here is an example of how to add a glossary to a LaTeX file. In this case, the glossary chapter is not numbered (\chapter*) but is listed in the table of contents (\addcontentsline{toc}{chapter}{Glossary}).

  1. \makeglossary
  2. \begin{document}
  3.  
  4. This website is done in PHP\glossary{[PHP] PHP Hypertext Preprocessor}.
  5.  
  6. %%%%%%%%%%%
  7. %debut du glossaire%
  8. %%%%%%%%%%%
  9.  
  10. \chapter*{Glossary}\label{glossary}\addcontentsline{toc}{chapter}{Glossary}
  11.  
  12. \begin{itemize}
  13. \input{Glossary}
  14. \end{itemize}
  15. \end{document}

Next is a script you can add in TexShop (MacOSX) to generate the glossary files properly. Click on Macros>Open Macros Editor. Chose “New Item” and copy the script below. You can also associate it to a keyboard shortcut to run it easily after every compilation. To get the glossary in your pdf file, compile your LaTeX files, compile the glossary and recompile your LaTeX files.

  1. –AppleScript direct
  2.  
  3. set fileName to #NAMEPATH#
  4. set n to (number of characters of contents of fileName)
  5. set fileNamequoted to quoted form of fileName
  6. set baseName to do shell script "basename " & fileNamequoted
  7. set m to (number of characters of contents of baseName)
  8. set dirName to quoted form of (characters 1 thru (n-m-1) of fileName as string)
  9.  
  10. set shellScript to "cd " & dirName & ";"
  11. set shellScript to shellScript & "./script.sh;"
  12. do shell script shellScript

Note that the script above requires a file named “script.sh” in the folder containing your .tex files. This file uses a PERL script named “writeglossary.prl” (cf. the end of this post) to create an item list from the glossary data. The entries are then sorted in alphabetical order and deletes the duplicates. Remember to replace “Paper.glo” in the script with ‘Your file’.glo.

  1. #!/bin/tcsh
  2. perl writeglossary.prl Paper.glo > Glossary.tmp
  3. #perl script.pl
  4. perl -e ‘while(<>) {push @lines, $_;}warn "\nSorted $. lines in ascending alphabetical order, ignoring case\n\n";print sort {lc($a) cmp lc($b)}@lines’ Glossary.tmp > Glossary.tex

And here is the file “writeglossary.prl”:

  1. # Simple perl script for converting glossary data (*.glo) files produced by
  2. # Latex and converting them to a form suitable for including
  3. # as a Glossary.tex file in a report.
  4. #
  5. # To use:   perl writeglossary.prl filename.glo > Glossary.tex
  6. # Then, in your Latex report file, set up a "description" environment using \begin{description}
  7. # and use \input{Glossary} to import the formatted glossary items.
  8. # (See me310report.tex for example.)
  9. #
  10. # Started 2 August 2006 -mrc
  11. # Note: Macs and Linux usually have perl installed by default. On Windows you might need
  12. # to install it or else use Visual Basic or simply use find/replace in MS Word to
  13. # accomplish the same string replacements as done by this perl script.
  14.  
  15. ############
  16. # Main loop: keep reading new lines until end of file.
  17. # Next line always goes into $_ and pattern matching is done
  18. # on $_ by default.
  19. line: while (<>) {
  20.  
  21. # Replace any instance of "glossaryentry{" with "item " on every line.
  22. # /g is for global matching (as many times as applies)
  23. s/glossaryentry{/item /g;
  24.  
  25. #Then take what comes before "}" and throw away the rest.
  26. if(/}/){
  27. $_ = $`;
  28. }
  29.  
  30. #Append a newline and print to the output.
  31. print($_,"\n");
  32.  
  33. } # end while(<>)

Related posts


Leave a Reply