
쉼표로 구분된 두 목록(길이가 같다고 가정)의 항목을 다른 목록에 연결하는 방법은 무엇입니까?
질문하기 전에 최선을 다해 답변을 찾았으니... 중복이 아니길 바랍니다!
예 를 들어 목록에 주어 \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.
그리고 조판
\unexpanded
edef를 사용하여 결과를 저장하더라도 목록의 용어가 확장되지 않도록 보호하려면 을 사용하십시오 . 명령이 지정된 토큰을 정의하도록 호출 순서를 변경했습니다.\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
전에 쉼표로 구분된 항목 목록을 보유하는 매크로를 확장하려면 몇 가지 인수 교환 트릭을 수행해야 합니다 .\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}
물론 이 모든 것은 두 개의 빈 목록의 경우를 고려하지 않습니다.
또한 쉼표로 구분된 목록 항목 주위의 공백 토큰은 처리되지 않습니다.
또한 공백/빈 목록 항목에 대한 특별한 처리는 없습니다.
답변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}