按行的字母顺序排列表格

按行的字母顺序排列表格

我有一张这样的桌子:

\documentclass[10pt,spanish]{article}
\usepackage[T1]{fontenc}
\usepackage{selinput}
\usepackage{multirow}
\usepackage{tabularx}
\SelectInputMappings{%
  aacute={á},
  ntilde={ñ},
  Euro={€}
}
\usepackage{babel}
\usepackage [left=1.5cm, right=1.5cm, top=1.5cm, bottom=1.5cm]{geometry}
\usepackage{longtable}

\begin{document}

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

\end{document}

我总是在这个表中添加新单词。我想根据Spanish列的值按字母顺序(AZ)自动对行进行排序。

我真的很喜欢它的制作方式这里。我尝试对我的桌子做同样的事情但是失败了。

请问你能帮帮我吗?

答案1

概念验证,使用来自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}

在此处输入图片描述

答案2

R/Sweave/knitr 方法:

1)表格内容是一个容易制作的SPUK.csv文件,格式如下:

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

2)然后在 R 中相当简单将其读取为数据框,对其进行排序,并使用库将数据框打印为 LaTeX 表xtable

对于每页都有标题的长表,您将需要使用一些选项(我承认,这并不容易),但这里有一个 R 代码可以做到这一点:

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) 

如果结果 100% 令人满意,并且您不需要做任何修改,则knitr无需每次都输入 R,然后将结果复制并粘贴到 LaTeX 文档中。只需将上述代码插入 LateX 代码一次即可:

\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} 

如果您不知道如何使用 编译它knitr,请将其另存为filename.Rnw,使用 Rstudio 打开它并按下Compile PDF按钮。结果:

姆韦

答案3

我建议你读一下 Kees van der Laan 的两篇论文,他已经去世了,他的文章是《BL​​Ue 中的排序》(https://pdfs.semanticscholar.org/64ed/fe77b0760edc9bc8169087665795efd5505e.pdf)和“在 TeX 中排序”(https://www.tug.org/TUGboat/tb14-3/tb40laan-sort.pdf)。两者都包含丰富的思想和大量的宏,用于使用 TeX 对数字、单词或任何内容进行排序,但请注意,它们的内容相当技术性,需要非常非常仔细地阅读!

相关内容