You can't use \def\x{\pairlists...}
(and using \def
in documents is not really recommended anyway).
Here's an implementation in expl3
. The command \mergelists
has four arguments:
#1
(optional, default =
) what to put in the middle of the items
#2
is the name for the new list
#3
is the first list to merge, either with a symbolic name or explicit
#4
is the second list to merge, either with a symbolic name or explicit
If the lists don't match by number of items, an error is raised.
\documentclass{article}
%\usepackage{xparse} % not needed for LaTeX 2020-10-01 or later
\ExplSyntaxOn
\NewDocumentCommand{\mergelists}{O{=}mmm}
{
\joaolourenco_mergelists:Nnnn #2 { #3 } { #4 } { #1 }
}
\seq_new:N \l__joaolourenco_first_seq
\seq_new:N \l__joaolourenco_second_seq
\clist_new:N \l__joaolourenco_temp_clist
\cs_new_protected:Nn \joaolourenco_mergelists:Nnnn
{
% #1 = name for the merged list
% #2 = first list
% #3 = second list
% #4 = separator
% check whether #2 is a symbolic name and fill in the sequence
\bool_lazy_and:nnTF { \tl_if_single_p:n { #2 } } { \token_if_cs_p:N #2 }
{
\seq_set_from_clist:NN \l__joaolourenco_first_seq #2
}
{
\seq_set_from_clist:Nn \l__joaolourenco_first_seq { #2 }
}
% check whether #3 is a symbolic name and fill in the sequence
\bool_lazy_and:nnTF { \tl_if_single_p:n { #3 } } { \token_if_cs_p:N #3 }
{
\seq_set_from_clist:NN \l__joaolourenco_second_seq #3
}
{
\seq_set_from_clist:Nn \l__joaolourenco_second_seq { #3 }
}
% check whether the lists have the same number of elements
\int_compare:nTF
{
\seq_count:N \l__joaolourenco_first_seq = \seq_count:N \l__joaolourenco_second_seq
}
{% true, do the real job
\__joao_lourenco_mergelists:Nn #1 { #4 }
}
{% false
\PackageError{example}{Lists~are~not~the~same~size}{Your~lists~have~different~size}
}
}
\cs_new_protected:Nn \__joao_lourenco_mergelists:Nn
{
\clist_clear:N \l__joaolourenco_temp_clist
\int_step_inline:nn { \seq_count:N \l__joaolourenco_first_seq }
{
\clist_put_right:Nx \l__joaolourenco_temp_clist
{
\seq_item:Nn \l__joaolourenco_first_seq { ##1 }
#2
\seq_item:Nn \l__joaolourenco_second_seq { ##1 }
}
}
\clist_clear_new:N #1
\clist_set_eq:NN #1 \l__joaolourenco_temp_clist
}
\ExplSyntaxOff
\begin{document}
\def\lista{1,2,3,4,5}
\def\listb{a,b,c,d,e}
\mergelists{\listx}{\lista}{\listb}
\texttt{\meaning\listx}
\mergelists[+]{\listy}{\lista}{a,b,c,d,e}
\texttt{\meaning\listy}
\mergelists{\listz}{1,2,3,4}{a,b,c,d}
\texttt{\meaning\listz}
\mergelists{\listerror}{1,2,3}{a,b}
\end{document}
The output on the console will be
! Package example Error: Lists are not the same size.
See the example package documentation for explanation.
Type H <return> for immediate help.
...
l.87 \mergelists{\listerror}{1,2,3}{a,b}
? h
Your lists have different size
The PDF file will have

With \bool_lazy_and:nnTF
we check that the argument is a single token which is a control sequence and, in this case, we assume it is the symbolic name for a list. Otherwise, the entire argument is the list.
The lists are converted to sequences; the number of items in each is compared. If they don't match, an error is raised. Otherwise, a clist is built from the items, with the separator in between.
Finally, the chosen control is assigned the value of the constructed clist.
\edef\y{\noexpand\pairitems[=]{\a}{\b}}
then you get the same output in the second line.minimal
class is for testing package loading dependencies. It defines almost zero commands/settings. For MWEs, use standard classes likebook
,article
, etc.\def
all three of\a
\b
and\c
are core latex commands defined in the format and this over-writes all three with no warning.