1

I'm looking to create a command that I can give a variable number of inputs, and it will place them into an itemized list. Something like

\myitemize{First item}{Second item}{Third item}

would produce

While something like

\myitemize{First item}{Second item}

would produce

I'm also not picky on formatting. It could be something like

\myitemize[First item, second item, third item]{3}

I just don't want to have to manually change the \begin{itemize} \end{itemize} to include another line and another \item. Thanks!

2
  • I do not understand why "invent" new syntax for the list itemize if existed is so deliberated designed ... (clear and easy to use)
    – Zarko
    Aug 8, 2019 at 8:51
  • This is good if you like to obfuscate your code.
    – egreg
    Aug 8, 2019 at 10:02

1 Answer 1

4

The first approach is implemented in Macro that takes a variable number of arguments, and creates a table with one row per argument. Be sure to read all the comments there. In particular, this is possibly not a great markup style.

The second approach is easy to implement with expl3 (you don't even need to specify the number of elements), for instance like this:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\seq_new:N \l__ryanj_tmp_seq

\cs_new_protected:Npn \ryanj_output_items:n #1
  {
    \seq_set_from_clist:Nn \l__ryanj_tmp_seq {#1}
    \seq_use:Nn \l__ryanj_tmp_seq { \item }
  }

\NewDocumentCommand \mystrangeitemize { m }
  {
    \begin{itemize}
    \item
    \ryanj_output_items:n {#1}
    \end{itemize}
  }

\ExplSyntaxOff

\begin{document}
  \mystrangeitemize{First item, Second item, Third item,
                    {Item, containing, commas.}}
\end{document}

\mystrangeitemize could also be implemented this way:

\NewDocumentCommand \mystrangeitemize { m }
  {
    \begin{itemize}
    \ryanj_output_items:n { {}, #1 }
    \end{itemize}
  }

(the added empty item causes the first \item command to be output).

In both cases, if you want to be able to declare items containing multiple paragraphs (\par tokens must then be acceptable in the argument of \mystrangeitemize), replace m with +m in \NewDocumentCommand \mystrangeitemize { m }.

Screenshot

etoolbox's \docsvlist can also be used for this purpose, and you may also possibly see someone post a solution based on listofitems. :-)

1
  • Such a nice suggestion...
    – MadyYuvi
    Aug 8, 2019 at 8:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .