作为我论文的一部分,我有 20 多个表格。我需要按字母顺序对它们的条目进行排序。
表格格式如下:
\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}
我需要根据第一列的内容对表格进行排序。任何特定的包或宏都可以用于此目的。
答案1
以下内容应该可以完成这项工作(下面的脚本和测试文件):
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()
答案2
您可以尝试导入您的表格这里并将其复制粘贴到电子表格应用程序中,进行排序,然后反向执行所有操作。