4

Note: Delete an element from a comma delimited list is not a duplicate.

What I want to do is have a list environment of items, like itemize and enumerate, but I want the output to be delimited by commas instead of new lines.

Take this example from http://en.wikibooks.org/wiki/LaTeX/List_Structures :

\begin{list_type}

  \item The first item
  \item The second item
  \item The third etc \ldots

\end{list_type}

I want the list type to output something that looks like this:

The first item, The second item, The third etc ...

With line breaks only when an item hits the right edge of the page. So if the right-hand margin were right after "third," we'd have,

The first item, The second item,
The third etc ...

instead of

The first item, The second item, The third
etc ...

Am I approaching this wrong in thinking that it should be a list structure? How can I do this with LaTeX?

2
  • 1
    This looks like something similar (minus the and): Comma-separated list Dec 21, 2012 at 7:12
  • 2
    Are your items fairly short in length? For example, what should happen to an item that spans more than one line? What about the layout around a page break?
    – Werner
    Dec 21, 2012 at 7:45

2 Answers 2

6

Building on Ulrike's code, here is how to box up the items so that they do not spread over a line break, as requested in the question:

Sample output

\documentclass{report}

\newlength{\runindent}
\setlength{\runindent}{\parindent}
\newenvironment{runlist}
{\par\renewcommand\item{\ifvmode\hspace{\runindent}\else\unskip,\egroup\ \fi
\hbox\bgroup}\raggedright}
{\egroup\par}

%%%%%%%%%%%%%%
% Following line for demonstration purposes only
\setlength{\textwidth}{6cm}
%%%%%%%%%%%%%%

\begin{document}

Some text for context to fill out space before the list and to show
the line lengths.
\begin{runlist}
  \item The first item
  \item The second item
  \item The third etc \ldots
\end{runlist}
Some text for context to fill out space after the list and to show
the line lengths.

\end{document}

The basic idea is to box up each item in an \hbox. The \hspace command serves two purposes, firstly it forces latex in to horizontal mode, otherwise the first item will be treated as a box to be put on its own line, and secondly it gives you control over the initial indentation of the list via the new length \runindent. (Note that you can't use \parindent directly, as this gets set to zero internally before arriving at this point.) Finally the correct line breaking is achieved by setting the item boxes raggedright.

5

A simple solution can be something like this:

\documentclass{report}
\newenvironment{runlist}
 {\par\renewcommand\item{\unskip\ifvmode\else, \fi}}
 {\par}


\begin{document}
some text
\begin{runlist}
  \item The first item
  \item The second item
  \item The third etc \ldots
\end{runlist}
some text
\end{document}

But the details can get tricky. E.g. if you want to suppress the comma in some cases you need an optional argument and so on.

You must log in to answer this question.

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