3

With the package easylist, we can create nested lists easily:

\documentclass{article}
\usepackage[at]{easylist}
\begin{document}
\begin{easylist}
    @ First     % -> 1. First
    @@ Second   % -> 1.1. Second
    @@ Third    % -> 1.2. Third
    @ Fourth    % -> 2. Fourth
\end{easylist}
\end{document}

It also supports a style looking like enumerate with \begin{easylist}[enumerate]. However, it does not use the enumerate environment internally; for example, style changes with the package enumitem have no effect.

So is there a way to write nested lists with easylist syntax using the enumerate environment?

This thread is related and I am looking for a solution mixing the first answer (which uses itemize internally, but whose syntax is different) and the second answer (which has a similar syntax, but does not use enumerate) with + being replaced by @.

B. W.
  • 105
  • 9

2 Answers2

3

This approach uses tokcycle to examine the tokens of the environment without executing them, but adding them to a token list. If it comes across @ tokens, it counts the number of contiguous @ tokens and compares that number to the list level. If it is greater, it inserts a new level of enumerate into the token list, if it is lesser, it ends a level of enumerate in the token list. After any sequence of @ tokens, \item is added to the token list.

At the conclusion of the examination, the constructed token list is inserted into the environment. A closeout \end{enumerate} is provided.

\documentclass{article}
\usepackage{environ,tokcycle}
\newcounter{listlevel}
\newcounter{atcnt}
\Characterdirective{%
 \ifx @#1
  \stepcounter{atcnt}\gdef\atcounting{T}
 \else
  \if T\atcounting
    \ifnum\value{atcnt}>\value{listlevel}\relax
      \stepcounter{listlevel}%
      \addcytoks{\begin{enumerate}}%
    \else
      \ifnum\value{atcnt}<\value{listlevel}\relax
        \addtocounter{listlevel}{-1}%
        \addcytoks{\end{enumerate}}%
      \fi
    \fi
    \setcounter{atcnt}{0}%
    \gdef\atcounting{F}%
    \addcytoks{\item #1}%
  \else
    \addcytoks{#1}%
  \fi
 \fi
}
\NewEnviron{easylist}{%
  \setcounter{listlevel}{0}%
  \expandafter\tokcyclexpress\expandafter{\BODY}%
  \the\cytoks\end{enumerate}
}
\begin{document}
\begin{easylist}
    @ First     % -> 1. First
    @@ Second   % -> 1.1. Second
    @@@ next level a   % -> 1.1. Second
    @@@ next level b   % -> 1.1. Second
    @@ Third    % -> 1.2. Third
    @ Fourth    % -> 2. Fourth
\end{easylist}
\end{document}

enter image description here

2

easylist does its own list environment independent of LaTeX's so you can't really integrate one into the other without rewriting pretty much the whole thing. Here's a different implementation that does only half the job of easylist: making @ mean \item and multiple @ mean nested lists—the half you want. The new command here, \easyitem, does no formatting at all, so the other half, the layout, is left to the surrounding list environment, so you can use the enumerate package or enumitem or any other you prefer for formatting.

\easyitem[<char>] makes <char> (which should be one single character, defaults to @) an active token that does the list bookkeeping. \easyitem changes the catcode of <char> (locally), so naturally the list will not work in the argument to another command (much like a verbatim command). The \easyitem command is supposed to be used inside a list environment, and it will use that same list environment for deeper levels. So

\begin{enumerate}
  \easyitem[@]
  @ one
  @@ two
\end{enumerate}

is equivalent to

\begin{enumerate}
  \item one
  \begin{enumerate}
    \item two
  \end{enumerate}
\end{enumerate}

and if you had used itemize or description or any other, it would do the same thing with those environments.

Here's the code in a filecontents environment, and an example below:

\begin{filecontents}{easyitem.sty}
\ifdefined\AtEndEnvironment
\else\RequirePackage{etoolbox}\fi
\newcount\easyitem@level
\newcount\easyitem@currlevel
  \easyitem@currlevel=1
\newif\if@ineasyitem
\newif\if@ineasyitemend
\newif\if@easyitembalancing
\newcommand\easyitem[1][@]{%
  \edef\easyitem@char{\unexpanded{#1}}%
  \@ineasyitemtrue
  \global\@easyitembalancingfalse
  \AtEndEnvironment{\@currenvir}{\easyitem@final@balance}
  \expandafter\easyitem@active\expandafter{\easyitem@char}%
  \ignorespaces}
\def\easyitem@active#1{%
  \begingroup
    \lccode`\~=`#1
    \lowercase{\endgroup
  \let~\easyitem@activeitem}%
  \catcode`#1=\active}
\def\easyitem@activeitem{%
  \global\easyitem@level=0
  \easyitem@item}
\def\easyitem@item{%
  \global\advance\easyitem@level by 1
  \futurelet\easy@let\easyitem@count}
\def\easyitem@count{%
  \ifx\easy@let\easyitem@activeitem
    \expandafter\expandafter
    \expandafter\easyitem@item
    \expandafter\@gobble
  \else
    \expandafter\easyitem@do
  \fi}
\def\easyitem@do{%
  \ifcase
       \ifnum\easyitem@currlevel=\easyitem@level 0\fi
       \ifnum\easyitem@currlevel>\easyitem@level 1\fi
       \ifnum\easyitem@currlevel<\easyitem@level 2\fi
       \relax
     \expandafter\item
  \or\expandafter\easyitem@decr
  \or\expandafter\easyitem@incr
  \fi}
\def\easyitem@decr{%
  \expandafter\easyitem@repeat\the\numexpr
    \easyitem@currlevel-\easyitem@level;\easyitem@decr\end}
\def\easyitem@incr{%
  \expandafter\easyitem@repeat\the\numexpr
    \easyitem@level-\easyitem@currlevel;\easyitem@incr\begin}
\def\easyitem@final@balance{%
  \if@ineasyitem \if@ineasyitemend \else
    \global\easyitem@level=1
    \global\@easyitembalancingtrue
    \easyitem@decr
  \fi\fi}
\def\easyitem@repeat#1;#2#3{%
  \ifnum#1=0
    \if@easyitembalancing \expandafter\@gobbletwo \fi
    \expandafter\item
  \else
    \ifx#3\end \@ineasyitemendtrue \fi
    \expandafter#3\expandafter{\@currenvir}%
    \ifx#3\begin
      \advance\easyitem@currlevel by 1
      \ifnum#1>1 \item\relax \fi
    \fi
    \expandafter#2%
  \fi}
\end{filecontents}
\documentclass{article}
\usepackage{easyitem}
\usepackage{enumitem}
\usepackage{color}
\begin{document}

\begin{enumerate}[label={\color{red}\Roman*}] \easyitem[@] @ First % -> 1. First @@ Second % -> 1.1. First @@ Third % -> 1.2. Third @ Fourth % -> 2. Fourth \end{enumerate}

\end{document}

The example above makes:

enter image description here