The prior (referenced by the OP) answer can be used with a slight syntax variation. Namely, the inner prenumerate
s need to me enclosed in their own group.
I also added an optional argument to prenumerate
to suppress the trailing auto-punctuation, since punctuation following an inner prenumerate
falls on a subsequent line all by itself.
\documentclass{article}
\usepackage{listofitems,environ}
\NewEnviron{prenumerate}[2][\relax]{%
\ignoreemptyitems
\setsepchar{,}%
\readlist\listorder{#2}%
\setsepchar{\item||\par\item}%
\expandafter\readlist\expandafter*\expandafter
\prenumlist\expandafter{\expandafter\item\expandafter\relax\BODY}%
\begin{enumerate}
\foreachitem\x\in\listorder[]{%
\expandafter\expandafter\expandafter\item
\prenumlist[\the\numexpr\x+1\relax]%
\ifx\relax#1\relax\ifnum\xcnt=\listlen\listorder[]\relax.\else;\fi\fi%
}%
\end{enumerate}
}
\begin{document}
\newcommand\preordB{3,1,4,2}
To nest prenumerate environments, group the inner environments.
Also, optional argument provided to suppress trailing auto-punctuation.
\begin{prenumerate}[x]{2,1}
\item Examples.
{\begin{prenumerate}{4,3,1,2}
\item Example 1
\item Example 2
\item Example 3
\item Example 4
\end{prenumerate}}
\item Examples ordered differently.
{\begin{prenumerate}{\preordB}
\item Example 1
\item Example 2
\item[$\bullet$] Example 3
\item Example 4
\end{prenumerate}}
\end{prenumerate}
\end{document}

SUPPLEMENT
User PaulSmith asks if the prenumerate
environment can be reworked with a deferred output; that is, saved for later use. With a lot of \expandafter
s, the answer is yes.
I name this environment renumerate
and it takes an extra mandatory argument, a unique macro name used to store internal list data. For example, here I provide the name \z
:
\begin{renumerate}[x]{2,1}{\z}
\item The first
\item the second
\end{renumerate}
Later, when the list is desired, it is to be recalled with the macro \zlist
, essentially tacking the word list
onto the end of the unique macro name provided.
In the MWE, I use a normal prenumerate
. Then I create 2 renumerate
s, and later present the second one first, followed by the first one (using the resume
key of enumitem
).
\documentclass{article}
\usepackage{listofitems,environ}
\NewEnviron{prenumerate}[2][\relax]{%
\ignoreemptyitems
\setsepchar{,}%
\readlist\listorder{#2}%
\setsepchar{\item||\par\item}%
\expandafter\readlist\expandafter*\expandafter
\prenumlist\expandafter{\expandafter\item\expandafter\relax\BODY}%
\begin{enumerate}
\foreachitem\x\in\listorder[]{%
\expandafter\expandafter\expandafter\item
\prenumlist[\the\numexpr\x+1\relax]%
\ifx\relax#1\relax\ifnum\xcnt=\listlen\listorder[]\relax.\else;\fi\fi%
}%
\end{enumerate}
}
\makeatletter
\NewEnviron{renumerate}[3][\relax]{%
\edef\tmp{\expandafter\@gobble\string#3}%
\ignoreemptyitems
\setsepchar{,}%
\greadlist#3{#2}%
\setsepchar{\item||\par\item}%
\expandafter\greadlist\expandafter*\csname prenum\tmp\expandafter
\endcsname\expandafter{\expandafter\item\expandafter\relax\BODY}%
\def\tempA{\begin{enumerate}\foreachitem\x\in#3[]}%
\def\tempB{\expandafter\expandafter\expandafter\item}%
\def\tempC{\csname prenum}%
\expandafter\gdef\csname \tmp list\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter
\endcsname\expandafter\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter\expandafter{%
\expandafter\expandafter\expandafter\expandafter\expandafter
\expandafter\expandafter\tempA\expandafter\expandafter\expandafter
\expandafter\expandafter\expandafter\expandafter{\expandafter
\expandafter\expandafter\tempB\expandafter
\tempC\tmp[\the\numexpr\x+1\relax]\endcsname%
\ifx\relax#1\relax\ifnum\xcnt=\listlen#3[]\relax.\else;\fi\fi%
}%
\end{enumerate}
}%
}
\makeatother
\begin{document}
Here is one that is not deferred:
\newcommand\preordB{3,1,4,2}
To nest prenumerate environments, group the inner environments.
Also, optional argument provided to suppress trailing auto-punctuation.
\begin{prenumerate}[x]{2,1}
\item Examples.
{\begin{prenumerate}{4,3,1,2}
\item Example 1
\item Example 2
\item Example 3
\item Example 4
\end{prenumerate}}
\item Examples ordered differently.
{\begin{prenumerate}{\preordB}
\item Example 1
\item Example 2
\item[$\bullet$] Example 3
\item Example 4
\end{prenumerate}}
\end{prenumerate}
\hrulefill
\begin{renumerate}[x]{2,1}{\z}
\item The first
\item the second
\end{renumerate}
Saved the first renumerate
\hrulefill
\begin{renumerate}[x]{2,1}{\zz}
\item Examples.
{\begin{prenumerate}{4,3,1,2}
\item Example 1
\item Example 2
\item Example 3
\item Example 4
\end{prenumerate}}
\item Examples ordered differently.
{\begin{prenumerate}{\preordB}
\item Example 1
\item Example 2
\item[$\bullet$] Example 3
\item Example 4
\end{prenumerate}}
\end{renumerate}
Saved the second renumerate.
\hrulefill
Now we present the 2nd
\zzlist
Now here is the first:\setlist{resume}
\zlist
\end{document}
