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 defined a new float environment for examples

\usepackage{float
\newfloat{example}{htp}{example}
\floatname{example}{Example}

that I the use throughout the document

\begin{example}
...
\caption{yet another example}
\end{example}

with automatic numbering and a table of examples

\listof{example}{List of Examples}

Now I have some long examples that I wish not to put in a float but give as subsections:

\subsection*{A long example}
...

How can I make this subsection to get included in the numbering of examples (with title "Example X: A long example") and appear in the list of examples?


P.S.: Claudio Fiandrino has provided a boilerplate which I adopted for my document the following way (looks like the solution depends on you document style, I use scrbook):

\newcommand{\subsectionexample}[1]{
  \stepcounter{example}
  \subsection*{Example \theexample: #1}
  \addcontentsline{example}{example}{\protect\numberline{\theexample}#1}
}

\subsectionexample{A long example}
....
share|improve this question

1 Answer 1

up vote 5 down vote accepted

The following should work:

\documentclass[a4paper,11pt]{article}
\usepackage{graphicx,mwe,lipsum}
\usepackage{float}
\newfloat{example}{htp}{example}
\floatname{example}{Example}

\begin{document}
\listof{example}{List of Examples}

\begin{example}
\centering
\includegraphics[scale=0.5]{example-image}
\caption{another example}
\end{example}

\subsection{A long example}
\stepcounter{example} %advance the counter of the example float
\addcontentsline{example}{subsection}{\theexample \hskip.66cm A long example} %add to the list of examples
\lipsum[1]
\begin{example}
\centering
\includegraphics[scale=0.5]{example-image}
\caption{yet another example}
\end{example}
\end{document}

Result:

enter image description here

share|improve this answer
1  
Thanks! I had to slightly modify the solution, as included in my question. –  Jakob Oct 1 '12 at 8:17
    
The number in the lists is usually formatted using \numberline. Then \theexample \hskip.66cm A long example becomes \protect\numberline{\theexample}A long example without magic number (.66cm). –  Heiko Oberdiek Oct 1 '12 at 9:12
    
@HeikoOberdiek: I wasn't aware of this, thus my merely attempt using .66cm. Thanks! :) –  Claudio Fiandrino Oct 1 '12 at 11:27

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.