對具有多個條目的字串進行排序

對具有多個條目的字串進行排序

在我的 LaTeX 檔案中,我建立了一個\authors可能包含內容的變數:

[Doe] John Doe; [Potter] Harry Potter; [Clinton] Bill Clinton; [Obama] Barack Obama; ...

這是動態創建的,所以我可以稍微改變一下(我有一個資料文件,可以從中檢索名字和姓氏)。有沒有辦法將這個可變文字轉換為有序(按姓氏)且顯示整齊的列表

Bill Clinton, John Doe, Barack Obama, Harry Potter,...

1)這可能嗎,2)我應該將這些初始資訊儲存在變數中還是應該將它們儲存在檔案或其他東西中?

答案1

expl3這是使用和 模組的實作l3sort

\documentclass{article}

\usepackage{xparse,l3sort,pdftexcmds}

\ExplSyntaxOn

\cs_set_eq:Nc \konewka_strcmp:nn { pdf@strcmp }

\NewDocumentCommand{\addauthor}{ o m m }
 {
  \IfNoValueTF{#1}
   {
    \konewka_add_author:nnn { #3 } { #2 } { #3 }
   }
   {
    \konewka_add_author:nnn { #1 } { #2 } { #3 }
   }
 }

\NewDocumentCommand{\printauthors}{ }
 {
  \konewka_print_authors:
 }

\seq_new:N \g_konewka_authors_id_seq
\seq_new:N \l__konewka_authors_full_seq

\cs_new_protected:Npn \konewka_add_author:nnn #1 #2 #3
 {
  \seq_gput_right:Nn \g_konewka_authors_id_seq { #1 }
  \prop_new:c { g_konewka_author_#1_prop }
  \prop_gput:cnn { g_konewka_author_#1_prop } { fname } { #2 }
  \prop_gput:cnn { g_konewka_author_#1_prop } { lname } { #3 }
 }

\cs_new_protected:Npn \konewka_print_authors:
 {
  \seq_gsort:Nn \g_konewka_authors_id_seq
   {
    \string_compare:nnnTF {##1} {>} {##2} {\sort_reversed:} {\sort_ordered:}
   }
  \seq_clear:N \l__konewka_authors_full_seq
  \seq_map_inline:Nn \g_konewka_authors_id_seq
   {
    \seq_put_right:Nx \l__konewka_authors_full_seq
     {
      \prop_item:cn { g_konewka_author_##1_prop } { fname }
      \c_space_tl
      \prop_item:cn { g_konewka_author_##1_prop } { lname }
     }
   }
  \seq_use:Nn \l__konewka_authors_full_seq { ,~ }
 }

\prg_new_conditional:Npnn \string_compare:nnn #1 #2 #3 {TF}
  {
   \if_int_compare:w \konewka_strcmp:nn {#1}{#3} #2 \c_zero
    \prg_return_true:
   \else:
    \prg_return_false:
   \fi
  }

\ExplSyntaxOff

\begin{document}

\addauthor{John}{Doe}
\addauthor{Harry}{Potter}
\addauthor[Uthor]{Archibald}{\"Uthor}
\addauthor{Bill}{Clinton}
\addauthor{Barack}{Obama}

\printauthors

\end{document}

作者添加有\addauthor{<first name(s)>}{<last name>};允許使用可選參數來處理特殊字元;這個可選參數將用於索引屬性清單和排序。

在此輸入影像描述

按字母順序對小節進行排序對於另一個應用程式\seq_sort:Nn

如果您有兩個具有相同姓氏的作者,請使用可選參數;例如

\addauthor[Doe@Jane]{Jane}{Doe}
\addauthor[Doe@John]{John}{Doe}

請注意,可選參數決定排序順序;使用@位於 ASCII 字母之前的 ,將保證兩位 Doe 作者將排序在“Doeb”之前。人們可能會對所有作者使用相同的想法,即使用「lname@fname」作為排序鍵,但這會帶來名字中特殊字元的問題。

如果排序鍵(姓氏或可選參數)已存在於資料庫中,則這裡的版本不會新增作者。

\documentclass{article}

\usepackage{xparse,l3sort,pdftexcmds}

\ExplSyntaxOn

\cs_set_eq:Nc \konewka_strcmp:nn { pdf@strcmp }

\NewDocumentCommand{\addauthor}{ o m m }
 {
  \IfNoValueTF{#1}
   {
    \konewka_add_author:nnn { #3 } { #2 } { #3 }
   }
   {
    \konewka_add_author:nnn { #1 } { #2 } { #3 }
   }
 }

\NewDocumentCommand{\printauthors}{ }
 {
  \konewka_print_authors:
 }

\seq_new:N \g_konewka_authors_id_seq
\seq_new:N \l__konewka_authors_full_seq

\msg_new:nnn { konewka/authors } { author~exists }
 {
  The ~ author ~ #1 ~ already ~ exists; ~ it ~ won't ~ be ~ added ~ again
 }

\cs_new_protected:Npn \konewka_add_author:nnn #1 #2 #3
 {
  \prop_if_exist:cTF { g_konewka_author_#1_prop }
   {
    \msg_warning:nnn { konewka/authors } { author~exists } { #1 }
   }
   {
    \seq_gput_right:Nn \g_konewka_authors_id_seq { #1 }
    \prop_new:c { g_konewka_author_#1_prop }
    \prop_gput:cnn { g_konewka_author_#1_prop } { fname } { #2 }
    \prop_gput:cnn { g_konewka_author_#1_prop } { lname } { #3 }
   }
 }

\cs_new_protected:Npn \konewka_print_authors:
 {
  \seq_gsort:Nn \g_konewka_authors_id_seq
   {
    \string_compare:nnnTF {##1} {>} {##2} {\sort_reversed:} {\sort_ordered:}
   }
  \seq_clear:N \l__konewka_authors_full_seq
  \seq_map_inline:Nn \g_konewka_authors_id_seq
   {
    \seq_put_right:Nx \l__konewka_authors_full_seq
     {
      \prop_item:cn { g_konewka_author_##1_prop } { fname }
      \c_space_tl
      \prop_item:cn { g_konewka_author_##1_prop } { lname }
     }
   }
  \seq_use:Nn \l__konewka_authors_full_seq { ,~ }
 }

\prg_new_conditional:Npnn \string_compare:nnn #1 #2 #3 {TF}
  {
   \if_int_compare:w \konewka_strcmp:nn {#1}{#3} #2 \c_zero
    \prg_return_true:
   \else:
    \prg_return_false:
   \fi
  }

\ExplSyntaxOff

\begin{document}

\addauthor{John}{Doe}
\addauthor{Harry}{Potter}
\addauthor[Uthor]{Archibald}{\"Uthor}
\addauthor{John}{Doe}
\addauthor{Bill}{Clinton}
\addauthor{Barack}{Obama}

\printauthors

\end{document}

運行此文件將產生警告,例如

*************************************************
* konewka/authors warning: "author exists"
* 
* The author Doe already exists; it won't be added again
*************************************************

如果您希望引發錯誤而不是發出警告,請更改\msg_warning:nnn為。\msg_error:nnn

答案2

為了方便說明,我展示如何使用實作合併排序的 OPmac 巨集在純 TeX 中解決此任務。

\input opmac

\def\sort{\begingroup\setprimarysorting\def\iilist{}\sortA}
\def\sortA#1#2{\ifx\relax#1\sortB\else
  \expandafter\addto\expandafter\iilist\csname,#1\endcsname
  \expandafter\preparesorting\csname,#1\endcsname
  \expandafter\edef\csname,#1\endcsname{{\tmpb}{#2}}%
  \expandafter\sortA\fi
}
\def\sortB{\def\message##1{}\dosorting
  \def\act##1{\ifx##1\relax\else \seconddata##1\sortC \expandafter\act\fi}%
  \gdef\tmpb{}\expandafter\act\iilist\relax
  \endgroup
}
\def\sortC#1&{\global\addto\tmpb{{#1}}}

\def\printauthors{\def\tmp{}\expandafter\printauthorsA\authors [] {} {}; }
\def\printauthorsA [#1] #2 #3; {%
   \ifx^#1^\expandafter\sort\tmp\relax\relax
           \def\tmp{}\expandafter\printauthorsB\tmpb\relax  
   \else\addto\tmp{{#1}{#2 #3}}\expandafter\printauthorsA\fi
}
\def\printauthorsB#1{\ifx\relax#1\else \tmp\def\tmp{, }#1\expandafter\printauthorsB\fi}

\def\authors{[Doe] John Doe; [Potter] Harry Potter; [Clinton] Bill Clinton;
             [Uthor] Archibald \"Uthor; [Obama] Barack Obama; }

Authors: \printauthors

\bye

排序是按[括號]內的資料進行的,但列印[括號]外的資料。 OPmac 可以支援多語言排序。

相關內容