2

I want to do an enumerate that is formatted in a way if it has multiple items, but in a different way if it has a single item. Something like this:

A list with multiple items:

#1 Item1
#2 Item2
#3 Item3

A list with a single item:

* SingleItem

I'm currently using enumitem package to format my lists, so it would be nice to use it, but any other solution would be welcome.

  • 2
    Welcome to TeX.SX! I am struggling with my pupils saying them that a list with just one item should not be formatted as a list. – user31729 Apr 18 '15 at 19:34
  • Do you know in advance, how many items the enumeration will have? – user31729 Apr 18 '15 at 19:39
  • Well, I could count or even force a different format for a single item, but I'm looking for something more scalable and elegant. :) – ricardoaraujo Apr 18 '15 at 20:03
  • See my possible solution – user31729 Apr 18 '15 at 20:04
  • I'd agree that a list with one item should not be a list... but the list may grow in later revisions and I'd not like to change the code structure when that happens. – ricardoaraujo Apr 18 '15 at 20:06
3

A quick and dirty trial solution, looping over a list using etoolbox macros and releasing it a specialized enumerate environment

Description:

\IntelligentEnumerate is a wrapper with three arguments:

  1. The first is optional, containing the label and other setups for the enumerate optional argument
  2. The first contains the list items as a comma-separated list (mandatory)
  3. The 3rd one is optional again, containing the options for the single item 'list'

\documentclass[11pt,a4paper]{article}

\usepackage{enumitem}

\usepackage{xparse}

\usepackage{etoolbox}


\def\myitemlist{}

\newcounter{somecounter}

\newcommand{\additemtolist}[1]{%
\stepcounter{somecounter}%
\listgadd{\myitemlist}{#1}%
}


\NewDocumentCommand{\IntelligentEnumerate}{+O{label={\#\arabic*}}+m+O{label={\textasteriskcentered}}}{%
  \setcounter{somecounter}{0}
  \forcsvlist{\additemtolist}{#2}%
  \ifnumgreater{\value{somecounter}}{1}{%
    \renewcommand*{\do}[1]{%
    \item ##1
    }
    \begin{enumerate}[#1]
      \dolistloop{\myitemlist}
    \end{enumerate}
  }{%
    \begin{enumerate}[#3]
    \item  #2
    \end{enumerate}
  }%   
  \def\myitemlist{}% 'Clear' the list at the end 
}



\begin{document}

\IntelligentEnumerate{First, Second, Third}


\IntelligentEnumerate[label={\Roman*}]{First, Second, Third}


\IntelligentEnumerate{This is a single Item}

\IntelligentEnumerate{This is a single Item}[label={$\int$}]



\end{document}

enter image description here

| improve this answer | |
  • If you want to have \par like stuff, you have to hide the content in a macro and enter this macro in the csv - list – user31729 Apr 18 '15 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.