複数のエントリを持つ文字列をソートする場合

複数のエントリを持つ文字列をソートする場合

\authors私の LaTeX ファイルでは、次のような内容を持つ変数を作成します。

[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

同じ姓の著者が2人いる場合は、オプションの引数を使用します。たとえば、

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

オプションの引数によってソート順が決まることに注意してください。ASCII@文字の前に来る を使用すると、2 人の 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によりソートの多言語サポートが可能です。

関連情報