GeneralAlphabetical Section Arrangement

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
hbaromega
Posts: 48
Joined: Mon Mar 07, 2011 8:21 pm

Alphabetical Section Arrangement

Post by hbaromega »

Can I arrange my sections or chapters in that way that they will appear alphabetically irrespective of their continuation in writing in the source file?

For example, I want to write a book or encyclopedia for several countries' cultures and economies in the world. So my section/chapter named "Germany" will appear before the section/chapter named "USA", even though I write in the following order:

Code: Select all

\section*{United States of America}
Blah blah blah.

\section*{Germany}
Blah blah blah.
So in the output I should get "Germany" before "United States of America" and the ToC accordingly.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
TikZ book
hugovdberg
Posts: 133
Joined: Sat Feb 25, 2012 6:12 pm

Alphabetical Section Arrangement

Post by hugovdberg »

To achieve this there are several options, and the best choice depends on your compiler and your operating system. Most solutions I've found on the other sites place the different pieces of text (be it sections, subsections or macros) in separate files with a their filename as the sort key.
Then you have a number of options to include the files in the correct order (this list is probably not exhaustive):
  • Manually sort the list of \input commands, which is at least easier than cut-and-paste whole blocks of text
  • Use a shell script to list files in a folder, sort them and write the list of files as \input commands to a tex file that is then statically included in your main file.
  • Use LuaTeX to read the folder contents and write the \input commands directly in the main file.
  • Another option using Lua is to use a macro to store all sections in a key/value array, sort the array by the keys and then write the contents in sorted order in the output.
Since the options are quite numerous and each one is a custom tailored solution I did not create an example of either, but this should help you investigate your options. If you could give more details about your setup or preferences I might be able to help you further.
Ubuntu 13.10 + Tex Live 2013 + Texmaker / Windows 7 Pro + MikTex 2.9 + TexnicCenter / Android 4.3 + TexPortal + DroidEdit
Post Reply