[Tex/LaTex] Sort sections according to alphabetical order

sectioningsorting

Is it possible to tell latex, that it should sort the sections you used in your document according to their alphabetical order in the final pdf document? I.e. I want to have something like this

\begin{document}
\section{A}
\section{C}
\section{B}
\end{document}

and in the pdf I get the result as

A
B
C

Is that possible, and if yes, how? Putting the sections into separate files is not preferred (but possible if scripted) due to having ~300 sections.
Edit: I can also rewrite it as

\begin{sortEnvironment}{Title}
Content
\end{sortEnvironment}

if that makes it easier.

Best Answer

(This answer is an improvement over my previous answer where you had to create the external files manually and run external command from shell prompt. For the present one, you do everything in LaTeX.)

In this answer, I used the LaTeX environment you wanted to use. Here, the sections are written to separate files, and the list is put in a master file, which is sorted using external shell command and then input when you issue the \PrintSections command.

Here is the code. You need to provide the -shell-escape option while executing latex or pdflatex.

\documentclass{article}

\usepackage{amsmath}

\usepackage{lipsum}

\usepackage{newfile}

\makeatletter

% Section list stream
\newoutputstream{sslist}
\openoutputfile{"\jobname.sslist.tex"}{sslist}

\usepackage{environ}
\newoutputstream{ssout}
\NewEnviron{sortEnvironment}[1]{
  % Save section file name in external list for input
  \addtostream{sslist}{\noexpand\input {"\jobname.#1"}}
  % Save the section in an external file
  \openoutputfile{"\jobname.#1.tex"}{ssout}
  \addtostream{ssout}{\noexpand\section{#1}}
  \toks@=\expandafter{\BODY}
  \addtostream{ssout}{\the\toks@}}
  [\closeoutputstream{ssout}]

\def\PrintSections{%
  % Close list stream
  \closeoutputstream{sslist}
  % Sort list stream using external shell and save in file
  \immediate\write18{sort "\jobname.sslist.tex" > "\jobname.sslist.sorted.tex"}
  % Get the sorted sections through the above list
  \input {"\jobname.sslist.sorted.tex"}
}                               

\makeatother


\begin{document}

\begin{sortEnvironment}{Title}
  Content
\end{sortEnvironment}

\begin{sortEnvironment}{Ab}
  Ab, Ab, Ab, Ab, Ab.
\end{sortEnvironment}

\begin{sortEnvironment}{A}
  Ab, Ab, Ab, Ab, Ab.
\end{sortEnvironment}

\begin{sortEnvironment}{Zebra}
  Zebras have \textit{strips} all over the body.
\end{sortEnvironment}


\begin{sortEnvironment}{Aa}
  About AA.

  $x = y^2$
\end{sortEnvironment}

\begin{sortEnvironment}{CAD}
  CAD as in ``Computer-Aided Design''
  or ``Canada Dollar''?

  \lipsum[1]

\end{sortEnvironment}

\begin{sortEnvironment}{Name Contains Mathematics $x + y$}
  Section name has mathematics.
\end{sortEnvironment}


\begin{sortEnvironment}{Name Does Not Contain Mathematics Ex Plus Wye}
  Section name does not have mathematics.
\end{sortEnvironment}


\begin{sortEnvironment}{Section Names Can Have Multiple Words}

  \lipsum[2]

\end{sortEnvironment}

\section{Before Sorted Sections}
We have some texts here. The sorted sections are printed after this.


\PrintSections

\section{After Sorted Sections}
And some texts at the end.


\end{document}

Here is the output.

enter image description here

enter image description here