Order table by row alphabeticaly

A proof of concept, using an idea from https://tex.stackexchange.com/a/356777/4427

\documentclass{article}
\usepackage{geometry}
\usepackage{longtable}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\spanishverb}{mm}
 {
  \seq_gput_right:Nn \g_alex_spanishverbs_seq { \__alex_spanishverbs_entry:nn {#1}{#2} }
 }

\seq_new:N \g_alex_spanishverbs_seq
\tl_new:N  \g__alex_spanishverbs_tablebody_tl

\NewDocumentCommand{\sortverbs}{}
 {
  \cs_gset_eq:NN \__alex_spanishverbs_entry:nn \use_i:nn
  \seq_gsort:Nn \g_alex_spanishverbs_seq
   {
    \alex_spanishverbs_if_before:ffTF { ##1 } { ##2 }
     { \sort_return_same: }
     { \sort_return_swapped: }
   }
  \cs_gset_eq:NN \__alex_spanishverbs_entry:nn \alex_spanishverbs_print:nn
  \tl_gset:Nx \g__alex_spanishverbs_tablebody_tl
   {
    \seq_use:Nn \g_alex_spanishverbs_seq { \\ \hline }
   }
 }
\NewExpandableDocumentCommand{\printverbs}{}
 {
  \tl_use:N \g__alex_spanishverbs_tablebody_tl
 }

\prg_new_conditional:Nnn \alex_spanishverbs_if_before:nn { p,T,F,TF }
 {% I hope the LaTeX3 police won't catch me
  \int_compare:nTF { \pdftex_strcmp:D { #1 } { #2 } < 0 }
   {
    \prg_return_true:
   }
   {
    \prg_return_false:
   }
 }
\cs_generate_variant:Nn \alex_spanishverbs_if_before:nnTF { ff }

\cs_new:Nn \alex_spanishverbs_print:nn { #1 & #2 }
\ExplSyntaxOff

\begin{document}

\spanishverb{ser}{to be (essential/permanent quality)}
\spanishverb{haber}{to have (to do something, auxiliary verb)}
\spanishverb{estar}{to be (health, location, state)}
\spanishverb{tener}{to have}
\spanishverb{hacer}{to do, make}
\spanishverb{poder}{to be able, can}
\spanishverb{decir}{to say, tell}
\spanishverb{ir}{to go}
\spanishverb{ver}{to see}
\spanishverb{dar}{to give}
\spanishverb{saber}{to know (information)}
\spanishverb{querer}{to want, love}
\spanishverb{llegar}{to arrive, come, reach}
\spanishverb{pasar}{to pass, to spend (time), to happen}
\spanishverb{deber}{to owe, must, should, ought to}
\spanishverb{poner}{to put, place, set}
\spanishverb{parecer}{to seem, appear}
\spanishverb{quedar}{to stay, remain}
\spanishverb{creer}{to believe}
\spanishverb{hablar}{to speak}
\spanishverb{llevar}{to carry, bring}
\spanishverb{dejar}{to leave, abandon, to let, allow}
\spanishverb{seguir}{to follow, continue}
\spanishverb{encontrar}{to find, encounter}
\spanishverb{llamar}{to call, to name}
\sortverbs

\begin{longtable}{
 |
 p{\dimexpr.15\textwidth-2\tabcolsep-1.5\arrayrulewidth}
 |
 p{\dimexpr.85\textwidth-2\tabcolsep-1.5\arrayrulewidth}
 |
} 
\hline
Spanish & English \\ \hline
\printverbs \\ \hline
\end{longtable}

\end{document}

enter image description here


R/Sweave/knitr approach:

1) The table content is an easy to make SPUK.csv file, with this format:

Spanish    ; English                                       
ser        ; to be (essential/permanent quality)           
haber      ; to have (to do something, auxiliary verb)     
estar      ; to be (health, location, state) 
...              

2) Then is fairly simple in R read it as a data frame, sort it, and print the data frame as a LaTeX table with xtable library.

For a long table with headers on every page, you will need play with some options (not so easy, I admit) but here a R code that does this:

library(xtable)
options(xtable.booktabs = T)
SPUK <- read.table("SPUK.csv", header=T, sep=";")
add.to.row <- list(pos = list(0), command =NULL )
command <- paste0("\\midrule\n\\endhead\n",
"\\bottomrule\n",
"{\\footnotesize Continued on next page}\n",
"\\endfoot\n",
"\\endlastfoot\n")
add.to.row$command <- command

print(xtable(SPUK[order(SPUK$Spanish),] , 
      caption = "Spanish verbs"), 
      caption.placement = "top",
      add.to.row = add.to.row,
      tabular.environment = "longtable", 
      include.colnames = T,
      floating = F, include.rownames = F) 

If the result is 100% satisfactory, and you do not need make any any modification, using knitr you do not need to enter in R each time, then copy & paste the result in the LaTeX document. It will be enough insert the above code in the LateX code only one time:

\documentclass{article}
\usepackage{longtable,booktabs,lipsum}
\begin{document}
\lipsum[1-3]
<<xxx, results="asis", echo=F>>=
library(xtable)
options(xtable.booktabs = T)
SPUK <- read.table("SPUK.csv", header=T, sep=";")
add.to.row <- list(pos = list(0), command =NULL )
command <- paste0("\\midrule\n\\endhead\n",
"\\bottomrule\n",
"{\\footnotesize Continued on next page}\n",
"\\endfoot\n",
"\\endlastfoot\n")
add.to.row$command <- command
print(xtable(SPUK[order(SPUK$Spanish),] , 
      caption = "Spanish verbs"), 
      caption.placement = "top",
      add.to.row = add.to.row,
      tabular.environment = "longtable", 
      include.colnames = T,
      floating = F, include.rownames = F)   
@

\lipsum[4-5]
\end{document} 

If you do not know how compile this with knitr, save it as filename.Rnw, open it with Rstudio and push the Compile PDF buttom. Result:

mwe


I suggest that you read two papers by the (unfortunately) late Kees van der Laan, viz "Sorting in BLUe" (https://pdfs.semanticscholar.org/64ed/fe77b0760edc9bc8169087665795efd5505e.pdf) and "Sorting within TeX" (https://www.tug.org/TUGboat/tb14-3/tb40laan-sort.pdf). Both contain rich ideas and lots of macros for sorting numbers, words or whatever using TeX, but be warned that their contents is rather technical and needs very, very careful reading !

Tags:

Sorting

Tables