11

How can I avoid the line break at the begin of an enumerate environment? I've noticed that theorem-style environments succeed in doing so. I'm using the enumitem package and my concrete example is:

\textbf{Example:} % don't want a line break here!
\begin{enumerate}
  \item test1
  \item test2
\end{enumerate}

I tried wrapping the enumerate in a vbox but that just shifted the entire list up...

lockstep
  • 250,273
Jonathan
  • 2,310
  • What behavior do you want? You want Example: 1. test 1 and then on the next line 2. test2? Should the 1 and 2 be aligned? – TH. May 01 '11 at 19:37
  • @TH No, I only want to influence the first line. The rest of the list should be formated as usual. – Jonathan May 01 '11 at 19:55

2 Answers2

10

Here's an attempt to imitate the theorem environments from the AMS classes:

\documentclass{article}

%\makeatletter
\newenvironment{myexample}
{\trivlist \item[\hskip\labelsep\textbf{Example:}]}
%  {\trivlist \item[]\setbox\@labels\hbox{\textbf{Example: }}}
  {\endtrivlist}
%\makeatother

\begin{document}
\thispagestyle{empty}

Here's some text. Here's some text. Here's some text. Here's some
text. Here's some text. Here's some text. Here's some text. Here's
some text. Here's some text. Here's some text. Here's some
text. Here's some text. Here's some text. Here's some text.
\begin{myexample}
  \begin{enumerate}
  \item Test 1
  \item Test 2
  \end{enumerate}
\end{myexample}
Here's some text. Here's some text. Here's some text. Here's some
text. Here's some text. Here's some text. Here's some text. Here's
some text. Here's some text. Here's some text. Here's some
text. Here's some text. Here's some text. Here's some text.

\end{document}

enter image description here

  • What's the purpose of the \@labels box trick here? Without an \everypar mechanism as in amsthm, it is equivalent to \item[\hskip\labelsep\textbf{Example:}]. – Philippe Goutet May 02 '11 at 12:44
  • @Philippe Goutet: Yes, they seem equivalent; I was just being lazy and copying the mechanism used by list environments (which includes theorem environments). This really does use \everypar, since that's how the box \@labels is put onto the page. list environments accumulate the labels from nested list environments into \@labels, and \everypar dumps that onto the page. – Phil Hirschhorn May 02 '11 at 17:19
  • precisely. As \item takes care of stacking automatically, just as well use it. What I meant about amsthm is that goes further with \everypar trickery: when the theorem is not followed by a list, it unboxes the label in order to allow long theorem titles without going into the margin (as they would with a simple \item[...]). – Philippe Goutet May 02 '11 at 22:11
  • @Jonathan, @Philippe Gautet: I just edited the answer to use the line suggested by Philippe; it's better that way. – Phil Hirschhorn May 03 '11 at 00:17
1

You can fool the first item label to typeset whatever you want. Something like this:

\documentclass{article}

\newcommand*\fixitem[1]{\item[]
  \refstepcounter{enumi}\hskip-\labelwidth\hskip-\labelsep
  #1 \labelenumi}

\begin{document}

\noindent\textbf{Example:} don't want a line break here, see below!

\begin{enumerate}
  \fixitem{\textbf{Example:}} test1
  \item test2
\end{enumerate}

\end{document}

The outcome is:

result of TeX run