[Tex/LaTex] Sorting table entries automatically

automationsortingtables

As one part of my thesis, I have more than 20 tables. I need to sort their entries alphabetically.

The tables are in following format:

\begin{table}
  \label{tab:first}
  \begin{tabular}{lllllll}
    Language  & Syllable &  IPA & VIS & VDS & CS &  CVS \\ \hline
    Malayalam & k        & k    & \Mal{ഈ}\normalfont   & \Mal{ീ}\normalfont  iː & \Mal{ക} \normalfont k   & \Mal{കീ}\normalfont    kiː \\
    Tamil        & ~        & ~    & ~   & ~   & ~  & ~    \\
    Kannada        & ~        & ~    & ~   & ~   & ~  & ~    \\
  \end{tabular}
  \caption{Table contains syllable in IPA, VIS-Vowel Independent Syllable, VDS-Vowel Dependent Symbol, CS-Consonant Symbol, and CVS  Consonant with Vowel Symbol.}
\end{table}

I need to sort the tables according to the content of first column. Any specific package or macro for that purpose.

Best Answer

The following should do the job (script and test file below):

pdflatex test.tex
python ltx-table_sort.py
pdflatex test.tex

test.tex

\RequirePackage{filecontents}
\begin{filecontents*}{table0_unsorted.tex}
  \begin{tabular}{lllllll}
    Language  & Syllable & IPA & VIS & VDS & CS &  CVS \\ \hline
    Malayalam & & & & & & \\
    Tamil     & & & & & & \\
    Kannada   & & & & & & \\
  \end{tabular}
\end{filecontents*}

\begin{filecontents*}{table1_unsorted.tex}
  \begin{tabular}{lllllll}
    Language  & Syllable & IPA & VIS & VDS & CS &  CVS \\ \hline
    Malayalam & & & & & & \\
    Tamil     & & & & & & \\
    Kannada   & & & & & & \\
  \end{tabular}
\end{filecontents*}

\documentclass{article}

\begin{document}
\IfFileExists{./table0.tex}{%
  \begin{table}
  \label{tab:first}
    \input table0\relax
  \caption{Table contains syllable in IPA, VIS-Vowel Independent Syllable, VDS-Vowel Dependent Symbol, CS-Consonant Symbol, and CVS  Consonant with Vowel Symbol.}
\end{table}   
}{}

\IfFileExists{./table1.tex}{%
  \begin{table}
  \label{tab:first}
    \input table1\relax
  \caption{Table contains syllable in IPA, VIS-Vowel Independent Syllable, VDS-Vowel Dependent Symbol, CS-Consonant Symbol, and CVS  Consonant with Vowel Symbol.}
\end{table}   
}{}
\end{document}

ltx-table_sort.py

#!/usr/bin/env python
import os

for fn in os.listdir('.'):
    if os.path.isfile(fn):
        if fn.startswith('table'):
            if '_unsorted' in fn:
                fno = fn[:-13]+'.tex'
                file_input = open( fn, 'r')
                file_output = open( fno, 'w')

                arr_tmp = []

                for line in file_input:
                    if line.lstrip().startswith('\\begin{tabular}'):
                        str_begin = line
                    elif line.lstrip().startswith('Language'):
                        str_lang = line.lstrip()
                    elif line.lstrip().startswith('\\end{tabular}'):
                        str_end = line
                    else:
                        arr_tmp.append( line.lstrip() )

                file_output.write( str_begin+str_lang )

                for a in sorted(arr_tmp):
                    file_output.write( a )

                file_output.write( str_end )
                file_input.close()
                file_output.close()
Related Question