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.

Note: Comma delimited lists 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?

share|improve this question
1  
This looks like something similar (minus the and): Comma-separated list –  Qrrbrbirlbel Dec 21 '12 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 '12 at 7:45

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this answer

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.

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.