2つのカンマ区切りリストの項目を1つのリストに組み合わせる

2つのカンマ区切りリストの項目を1つのリストに組み合わせる

2 つのコンマ区切りリスト (長さが同じであると仮定) の項目を別のリストでペアにするにはどうすればよいでしょうか?

質問する前に答えをできるだけ探してみました…なので、重複していないことを願っています!

たとえば、リストに が与えられ \def\a{1,2,3,4,5} 、 が を定義する ような を \def\b{a,b,c,d,e} 定義したいとします。\parlists\def\c{\pairlists[=]{\a}{\b}}\c as {1=a,2=b,3=c,4=d,5=e}

私は TeX の専門家ではありませんが、それでも部分的に成功しました。リストがパラメータとして指定されている場合は機能しますが、リストがコマンドとに保存されている場合は機能しません\a\b以下の私の試みをご覧ください... どのような助けでも大歓迎です。ありがとうございます。

\documentclass{minimal}

\usepackage{xifthen}

\makeatletter
\def\@pairitems[#1]#2,#3\@nil#4,#5\@nil{%
% [#1][#2,#3][#4,#5]%
  \ifthenelse{\equal{#3}{}}{%
    \ifthenelse{\equal{#5}{}}%
      {#2#1#4}%
      {\PackageError{example}{Lists are not the same size}}%
  }{%
    \ifthenelse{\equal{#5}{}}%
      {\PackageError{example}{Lists are not the same size}}%
      {#2#1#4, \@pairitems[#1]#3\@nil#5\@nil}%
  }%
}
\def\pairitems[#1]#2#3{\@pairitems[#1]#2,\@nil#3,\@nil}
\makeatother


\begin{document}

\def\a{1,2,3,4,5}
\def\b{a,b,c,d,e}
\def\x{\pairitems[=]{1,2,3,4,5}{a,b,c,d,e}}
\def\y{\pairitems[=]{\a}{\b}}

\noindent
x: \x\\
y: \y\\

\end{document}

ここに画像の説明を入力してください

答え1

これは、LaTeX3 アプローチを使用したソリューションです。LaTeX はマクロ言語であることに留意してください。 を使用して、\meaningコマンドの定義を表示できます。 を定義すると\def\x{\pairitems{\a}{\b}}、 は文字通り、 の値ではなく に\xなります。の値を含める場合は、特別な処理が必要です。\pairitems{\a}{\b}\pairitems{\a}{\b}\x\pairitems{\a}{\b}

\documentclass{minimal}
\usepackage[T1]{fontenc}
\usepackage{expl3}
\usepackage{xparse}


\ExplSyntaxOn

\clist_new:N \l_doc_tmpa_clist
\clist_new:N \l_doc_tmpb_clist
\seq_new:N \l_doc_tmpa_seq

\msg_new:nnn {doc} {difflen} {two~comma~separated~lists~have~different~length}


\cs_set:Npn \doc_pair_items:nnn #1#2#3 {
    \clist_set:Nn \l_doc_tmpa_clist {#2}
    \clist_set:Nn \l_doc_tmpb_clist {#3}
    \seq_clear:N \l_doc_tmpa_seq
    
    \int_compare:nNnF {\clist_count:N \l_doc_tmpa_clist} = {\clist_count:N \l_doc_tmpb_clist} {
        \msg_error:nn {doc} {difflen}
    }
    
    \int_step_inline:nn {\clist_count:N \l_doc_tmpa_clist} {
        \seq_put_right:Nn \l_doc_tmpa_seq {
            \clist_item:Nn \l_doc_tmpa_clist {##1}
            #1
            \clist_item:Nn \l_doc_tmpa_clist {##1}
        }
    }
    
    \seq_use:Nn \l_doc_tmpa_seq {,}
}

\cs_generate_variant:Nn \doc_pair_items:nnn {nxx}
\cs_generate_variant:Nn \doc_pair_items:nnn {noo}

\newcommand{\pairitems}[3][=]{
    \doc_pair_items:nnn {#1} {#2} {#3}
}

\newcommand{\pairitemso}[3][=]{
    \doc_pair_items:noo {#1} {#2} {#3}
}

\newcommand{\pairitemsx}[3][=]{
    \doc_pair_items:nxx {#1} {#2} {#3}
}

\ExplSyntaxOff


\begin{document}

\par\pairitems{1,2,3,4,5}{a,b,c,d,e}
\par\pairitems[+]{1,2,3,4,5}{a,b,c,d,e}

\def\a{1,2,3,4,5}
\def\b{a,b,c,d,e}
\par\pairitems{\a}{\b}
\par\pairitemso{\a}{\b}

\def\x{\pairitemso{\a}{\b}}
\par\meaning\x
\edef\x{\noexpand\pairitemso{\a}{\b}}
\par\meaning\x

\end{document}

答え2

これにより端末出力が生成される

> \zc=macro:
->1=a, 2=b, 3=c, 4=d, 5=e.

およびタイプセット

ここに画像の説明を入力してください

edefを使用して結果を保存する場合でも、リスト内の用語が展開から保護されるようにすることに注意してください\unexpanded。コマンドが指定されたトークンを定義するように呼び出し順序を変更しました。\zc

\documentclass{article}

% don't break latex accent support by redefining \a \b or \c which are
% all core latex commands....

\def\za{1,2,3,4,5}
\def\zb{a,b,c,d,e}

\newcommand\pairlists[4][=]{%
 \edef#2{%
 \expandafter\expandafter\expandafter\xpairlists
 \expandafter#3\expandafter,\expandafter\relax#4,\relax#1\zstop
 }}

\def\xpairlists#1,#2\relax#3,#4\relax#5\zstop{%
   \unexpanded{#1#5#3}%
   \ifcat$\detokenize{#2}$%
     \expandafter\gobblezstop
   \fi
   , \xpairlists#2\relax#4\relax#5\zstop}

\def\gobblezstop#1\zstop{}

\pairlists[=]{\zc}{\za}{\zb}

\show\zc

\begin{document}
\zc
\end{document}

答え3

が実行される\expandafter前に、カンマで区切られた項目のリストを保持するマクロを展開するには、いくつかの -and-argument-exchanging-tricks を行う必要があります。\pairitems

\edef(コンマリストのコンマ区切り項目自体の展開もトリガーする)を使いたくない場合は、\romannumeral有効なTeXが収集されるまで展開をトリガーする(ab?)使用が可能です。⟨番号⟩-数量とTeXの場合-⟨番号⟩-数量は正でない値を表し、TeXを形成するトークンを黙って飲み込むだけです-⟨番号⟩-量。

\documentclass[a4paper, landscape]{article}

%===================[adjust margins/layout for the example]====================
\csname @ifundefined\endcsname{pagewidth}{}{\pagewidth=\paperwidth}%
\csname @ifundefined\endcsname{pdfpagewidth}{}{\pdfpagewidth=\paperwidth}%
\csname @ifundefined\endcsname{pageheight}{}{\pageheight=\paperheight}%
\csname @ifundefined\endcsname{pdfpageheight}{}{\pdfpageheight=\paperheight}%
\textwidth=\paperwidth
\oddsidemargin=1.5cm
\marginparsep=.2\oddsidemargin
\marginparwidth=\oddsidemargin
\advance\marginparwidth-2\marginparsep
\advance\textwidth-2\oddsidemargin
\advance\oddsidemargin-1in
\evensidemargin=\oddsidemargin
\textheight=\paperheight
\topmargin=1.5cm
\footskip=.5\topmargin
{\normalfont\global\advance\footskip.5\ht\strutbox}%
\advance\textheight-2\topmargin
\advance\topmargin-1in
\headheight=0ex
\headsep=0ex
\pagestyle{plain}
\parindent=0ex
\parskip=0ex 
\topsep=0ex
\partopsep=0ex
%==================[eof margin-adjustments]====================================

\makeatletter
\newcommand\Exchange[2]{#2#1}%
\newcommand\CheckWhetherNull[1]{%
  \ifcat Y\detokenize{#1}Y%
  \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}%
\@ifdefinable\@pairitems{%
  \long\def\@pairitems#1#2,#3\@nil#4,#5\@nil#6{%
    \CheckWhetherNull{#3}{%
      \CheckWhetherNull{#5}%
        {\z@#6#2#1#4}%
        {\z@\PackageError{example}{Lists are not the same size}}%
    }{%
      \CheckWhetherNull{#5}%
        {\z@\PackageError{example}{Lists are not the same size}}%
        {\@pairitems{#1}#3\@nil#5\@nil{#6#2#1#4,}}%
    }%
  }%
}%
\newcommand\pairitems[3]{\romannumeral\@pairitems{#1}#2,\@nil#3,\@nil{}}
\makeatother


\begin{document}

\newcommand*\one{1}
\newcommand*\two{2}
\newcommand*\three{3}
\newcommand*\four{4}
\newcommand*\five{5}
\newcommand*\MYa{a}
\newcommand*\MYb{b}
\newcommand*\MYc{c}
\newcommand*\MYd{d}
\newcommand*\MYe{e}

\newcommand*\ListA{\one,\two,\three,\four,\five}
\newcommand*\ListB{\MYa,\MYb,\MYc,\MYd,\MYe}
\newcommand*\PairedU{\pairitems{=}{\one,\two,\three,\four,\five}{\MYa,\MYb,\MYc,\MYd,\MYe}}
\newcommand*\PairedV{\expandafter\Exchange\expandafter{\expandafter{\ListB}}{\expandafter\Exchange\expandafter{\expandafter{\ListA}}{\pairitems{=}}}}
\expandafter\newcommand\expandafter*\expandafter\PairedW\expandafter{%
  \romannumeral0\Exchange{ }{\expandafter\expandafter\expandafter}\pairitems{=}{\one,\two,\three,\four,\five}{\MYa,\MYb,\MYc,\MYd,\MYe}%
}
\expandafter\newcommand\expandafter*\expandafter\PairedX\expandafter{%
  \romannumeral0%
  \expandafter\Exchange\expandafter{\expandafter{\ListB}}{%
    \expandafter\Exchange\expandafter{\expandafter{\ListA}}{%
      \Exchange{ }{\expandafter\expandafter\expandafter}\pairitems{=}%
    }%
  }%
}

\csname @ifdefinable\endcsname\PairedY{%
  \edef\PairedY{\pairitems{=}{\one,\two,\three,\four,\five}{\MYa,\MYb,\MYc,\MYd,\MYe}}%
}%

\csname @ifdefinable\endcsname\PairedZ{%
  \edef\PairedZ{\expandafter\Exchange\expandafter{\expandafter{\ListB}}{\expandafter\Exchange\expandafter{\expandafter{\ListA}}{\pairitems{=}}}}%
}%

\noindent{\ttfamily \string\PairedU:\\\meaning\PairedU}\\$\to$\PairedU\bigskip

\noindent{\ttfamily \string\PairedV:\\\meaning\PairedV}\\$\to$\PairedV\bigskip

\noindent{\ttfamily \string\PairedW:\\\meaning\PairedW}\\$\to$\PairedW\bigskip

\noindent{\ttfamily \string\PairedX:\\\meaning\PairedX}\\$\to$\PairedX\bigskip

\noindent{\ttfamily \string\PairedY:\\\meaning\PairedY}\\$\to$\PairedY\bigskip

\noindent{\ttfamily \string\PairedZ:\\\meaning\PairedZ}\\$\to$\PairedZ

\end{document}

ここに画像の説明を入力してください

もちろん、これらすべては 2 つの空のリストのケースを考慮していません。
また、コンマで区切られたリスト項目を囲むスペース トークンの処理はありません。
また、空白/空のリスト項目に対する特別な処理もありません。

答え4

OP にとってうまくいくように、構文を少し変えました。ここでは、\makepairlist[=]{\a}{\b}マクロで目的のペア リストを作成するために実行します\thepairlist。MWE では、トークン化解除されたマクロがすでに目的のリストに展開されていることを示しています。

\edef\c{\thepairlist}必要に応じて、または、さらに良い方法として をフォローアップすることもできます\let\c\thepairlist

\documentclass{article}
\usepackage{listofitems}
\newcommand\makepairlist[3][:]{%
  \readlist\ListA{#2}%
  \readlist\ListB{#3}%
  \def\thepairlist{}%
  \foreachitem\z\in\ListA[]{%
    \ifnum\zcnt=1\relax\else\edef\thepairlist{\thepairlist,}\fi
    \edef\thepairlist{\thepairlist\z#1\ListB[\zcnt]}%
  }%
}
\begin{document}
\def\a{1,2,3,4,5}
\def\b{a,b,c,d,e}
\makepairlist[=]{\a}{\b}
\thepairlist

\detokenize\expandafter{\thepairlist}
\end{document}

ここに画像の説明を入力してください

関連情報