4

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}
....

1 Answer 1

5

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

3
  • 1
    Thanks! I had to slightly modify the solution, as included in my question.
    – Jakob
    Oct 1, 2012 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). Oct 1, 2012 at 9:12
  • @HeikoOberdiek: I wasn't aware of this, thus my merely attempt using .66cm. Thanks! :) Oct 1, 2012 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.