Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

We need to make a paper that describes a list of interfaces. The assignment however says we need to sort the interfaces alphabetically. Because this list is quite long (20+) I am looking for a way to sort the subsections (each subsection describes an interface) alphabetically. Without having to move code myself.

Is there a package that can handle this?

Here is a sample of the code:

\newcommand{\interfaceItem}[5]{
   \subsection{#1}\label{interfaceItem:#1}
   \paragraph{Paragraph 1}#2
   \paragraph{Paragraph 2}#3
   \paragraph{Paragraph 3}#4
   \paragraph{Paragraph 4}#5
}
share|improve this question
1  
Would it be possible to have each subsection in a separate file, and then have a program compile the whole document together before leaving it to TeX? –  Henrik Hansen Apr 21 '12 at 11:56
    
This sheds some light on your options: tex.stackexchange.com/questions/51967/… –  Henrik Hansen Apr 21 '12 at 11:57
    
Well the subsection itself is written by a macro. I don't think it's that difficult to write the code to a file instead. –  CommuSoft Apr 21 '12 at 11:58
    
Can you give an indication how your paper is organised? If the whole text of the subsections has to be moved, this is certainly impossible from within TeX if the subsections are not "contained" in some sort of structure. –  Stephan Lehmke Apr 22 '12 at 7:18
    
I have added a small description. –  CommuSoft Apr 23 '12 at 7:38

2 Answers 2

up vote 5 down vote accepted

Here's an implementation:

\documentclass{article}
\usepackage{xparse,l3sort}
\ExplSyntaxOn
\NewDocumentCommand{\interfaceItem}{mmmmm}
 {
  \seq_put_right:Nn \l_commusoft_interfaces_seq {#1}
  \cs_new:cpn { commusoft_interface_#1: } {
    \subsection{#1}\label{interfaceItem:#1}
    \paragraph{Paragraph 1}#2
    \paragraph{Paragraph 2}#3
    \paragraph{Paragraph 3}#4
    \paragraph{Paragraph 4}#5
  }
 }
\NewDocumentCommand{\printInterfaces}{ }
 {
  \seq_sort:Nn \l_commusoft_interfaces_seq
   {
    \string_compare:nnnTF {##1} {>} {##2} {\sort_reversed:} {\sort_ordered:}
   }
  \seq_map_inline:Nn \l_commusoft_interfaces_seq { \use:c { commusoft_interface_##1: } }
 }
\seq_new:N \l_commusoft_interfaces_seq
\prg_new_conditional:Npnn \string_compare:nnn #1 #2 #3 {TF}
  {
   \if_int_compare:w \pdftex_strcmp:D {#1}{#3} #2 \c_zero
    \prg_return_true:
   \else:
    \prg_return_false:
   \fi
  }
\ExplSyntaxOff

\interfaceItem{A}{A1}{A2}{A3}{A4}
\interfaceItem{C}{C1}{C2}{C3}{C4}
\interfaceItem{B}{B1}{B2}{B3}{B4}

\begin{document}

\printInterfaces

\end{document}

However, a simpler strategy can be easier:

\makeatletter
\newcommand{\interfaceItem}[5]{
   \@namedef{interface@\detokenize{#1}}{%
     \subsection{#1}\label{interfaceItem:#1}
     \paragraph{Paragraph 1}#2
     \paragraph{Paragraph 2}#3
     \paragraph{Paragraph 3}#4
     \paragraph{Paragraph 4}#5
    }
}
\newcommand{\printInterface}[1]{%
  \@nameuse{interface@\detokenize{#1}}%
}
\makeatother

You define your interfaces as before, in the preamble,

\interfaceItem{A}{A1}{A2}{A3}{A4}
\interfaceItem{C}{C1}{C2}{C3}{C4}
\interfaceItem{B}{B1}{B2}{B3}{B4}

and then say

\printinterface{A}

\printinterface{B}

\printinterface{C}

Sorting a list of short commands is easier than sorting big chunks of code.

share|improve this answer

TeX is Turing complete language so I am sure the problem can be "easily" solved. However, this is best done (at least on Unix) with the standard Unix tools. Create a directory interfaces in which you would create a separate .tex file for the description of each interface. Something like

z-interface.tex
a-interface.tex
q-interface.tex
b-interface.tex

Now do something like

ls interfaces > interface-names.txt

which will create interface-names.txt file with interfaces sorted in alphabetical order. Using awk you can easily add \input TeX command in front of each file name. Like

awk '{print "\\input",$1}' interface-names.txt > interface-names.tex

Now just put the following line

\input interface-names.tex

into your main .tex file and you will have all sub sections in proper order as long as main .tex file and interface .tex files are in the same directory.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.