Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have pretty regular and large descriptions in enumerated items.

\begin{enumerate}
\item Producers
  \begin{description}
    \item[ Vision: ] abc ... 
    \item[ Contribution:]  def ... 
    ...
  \end{description}
\end{enumerate}

What I expect is to have myitem command that accepts multiple, but not not determined in total number of parameters like this:

\begin{enumerate}
  \myitem{Producers}
    {Vision:}{abc ...}
    {Contribution:}{def ...}
    {...}{...}
    ...
\end{enumerate}

How can I define command in this case?

\renewcommand{myitem}{...}
share|improve this question

4 Answers 4

up vote 5 down vote accepted

A modification of cgnieder's that catches also loners.

\documentclass{article}
\makeatletter
\newcommand\myitem[1]{%
  \item #1
  \@ifnextchar\bgroup{\begin{description}\@myitem@desc}{}
}
\newcommand{\@myitem@desc}[1]{%
  \item[#1]
  \@ifnextchar\bgroup{\@myitem@desc@item}{\@myitem@warning\end{description}}
}
\newcommand{\@myitem@desc@item}[1]{%
  #1
  \@ifnextchar\bgroup{\@myitem@desc}{\end{description}}
}
\newcommand\@myitem@warning{\@latex@warning{Lonely label without text}}
\makeatother
\begin{document}

\begin{enumerate}
  \myitem{Producers}
    {Vision:}{abc ...}
    {Contribution:}{def ...}
    {...}{...}
  \myitem{Example}
    {A}{a}
    {B}{b}
    {C}{c}
    {D}{d}
  \myitem{Example}
    {A loner}
  \myitem{Example}
    {A}{a}
\end{enumerate}

\end{document}

However, I'd better see a different syntax:

\documentclass{article}
\newcommand{\myitem}[2]{%
  \item #1
  \begin{description}
  #2
  \end{description}
}
\newcommand{\II}[2]{%
  \item[#1] #2
}

\begin{document}

\begin{enumerate}
  \myitem{Producers}{
    \II{Vision:}{abc ...}
    \II{Contribution:}{def ...}
    \II{...}{...}
  }
  \myitem{Example}{
    \II{A}{a}
    \II{B}{b}
    \II{C}{c}
    \II{D}{d}
  }
  \myitem{Example}{
    \II{A}{a}
  }
\end{enumerate}

\end{document}

Not much more difficult to type, surely cleaner and more structured.

share|improve this answer

This code is short but not for the faint of heart (1). Its key tool is the command \xintApplyUnbraced from package xint which applies a given macro to each successive braced thing of a list of such braced things, such as {stuff}{more}{yet}{other}.

(1) but edited version is simpler and better.

The sole effect of \xintApplyUnbraced here is to insert \myitemhelpera commands everywhere; and this macro is defined (thanks to TeX delimited macros) to fetch the data 2 by 2, producing a description item with its heading and description content.

\documentclass{article}
\usepackage{xint}

% The space in \myitemhelper will be swallowed during
% the action of \xintApplyUnbraced, and prevents further
% expansion (at a too premature time)

% edited. This is simpler and moreover allows use of [ and ] in the 
% contents of the description texts
\def\myitemhelper #1{ \myitemhelpera {#1}}
\def\myitemhelpera #1\myitemhelpera #2{\item [#1]#2}

% earlier, more complicated and less efficient method:
% \def\myitemhelper #1{ \myitemhelpera[#1]}
% \def\myitemhelpera {\let\myitemhelpera\myitemhelperb \item}
% \def\myitemhelperb [#1]{#1\let\myitemhelpera\myitemhelperA}
% \let\myitemhelperA\myitemhelpera


% \xintApplyUnbraced just has the effect here to insert
%  \myitemhelpera in-between all the braced pairs

\newcommand{\myitem}[2]{\item #1
  \begin{description}
  \xintApplyUnbraced\myitemhelper {#2}
  \end{description}}

\begin{document}
\begin{enumerate}
\myitem{Producers}{% This percent sign is optional
    {Vision:}{abc ...}
    {Contribution:}{def ...}
    {Anticipation:}{g[h]k ...}
    {Participation:}{lmn ...}
    {Conclusion:}{not [yet].}% This percent sign is optional
  }
    \myitem{Example}{%
    {A}{a}
    {B}{b}
    {C}{c}
    {D}{d}%
  }
  \myitem{Example}{%
    {A}{a}%
  }
 \end{enumerate}
\end{document}

variable-number-of-param

share|improve this answer

No guarantees but this works with the syntax outlined in the question:

\documentclass{article}
\makeatletter
% place \item and check if a description follows:
\newcommand\myitem[1]{%
  \item #1\@myitem@check@description
}
% if an opening brace follows start description:
\def\@myitem@check@description{%
  \@ifnextchar\bgroup
    {\@myitem@startdescription}
    {}%
}
% start description environment, place the first item
% and see if another item follows:
\def\@myitem@startdescription#1#2{%
  \begin{description}%
  \item[#1] #2%
  \@myitem@check@description@item
}
% end description environment:
\def\@myitem@enddescription{%
  \end{description}%
}
% if an item follows place it and check if there is
% another one; else end the description
\def\@myitem@check@description@item{%
  \@ifnextchar\bgroup
    {\@myitem@description@item}
    {\@myitem@enddescription}%
}
% place item and check for the next one:
\def\@myitem@description@item#1#2{%
  \item[#1] #2%
  \@myitem@check@description@item
}

\makeatother
\begin{document}

\begin{enumerate}
  \myitem{Producers}
    {Vision:}{abc ...}
    {Contribution:}{def ...}
    {...}{...}
  \myitem{Example}
    {A}{a}
    {B}{b}
    {C}{c}
    {D}{d}
  \myitem{Example}
  \myitem{Example}
    {A}{a}
\end{enumerate}

\end{document}

enter image description here

share|improve this answer

The readarray package has a \getargsC macro that gets each item from a space-separated list and puts them into \argi, \argii, \argiii, etc., and the total number of arguments into \narg. That can be exploited for your problem.

EDITED to make \myitem have two arguments, the first being the enumerate item, and the rest being the description items.

\documentclass{article}
\usepackage{readarray}
\usepackage{ifthen}
\newcounter{index}
\newcommand\myitem[2]{%
  \getargsC{#2}%
  \setcounter{index}{0}%
  \begin{enumerate}%
    \item #1%
    \begin{description}%
    \whiledo{\theindex<\narg}{%
      \stepcounter{index}%
      \item[\csname arg\roman{index}\endcsname]%
      \stepcounter{index}%
      \csname arg\roman{index}\endcsname%
    }%
    \end{description}%
  \end{enumerate}%
}
\begin{document}
\myitem{Producers}{{Vision:} {abc...} {Contribution:} {def} {...} {...}}
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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