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 have written a custom example environment for formatting mathematical worked examples. These are currently included in the sections they are related to, however are sometimes looking a bit cluttered.

I am considering grouping all the examples at the end of each chapter. Can you think of a simple way I can rewrite my custom environment so that it moves all of the text to the end of each chapter, displaying them in the order they were written in? (Kind of like some of the endnotes packages I suppose!)

Thanks!

EDIT - adding MWE requested in comments...

\documentclass{book}
\usepackage[english]{babel}
\usepackage{blindtext}


%%%%% SET UP THE ENVIRONMENT HERE %%%%%
\newcounter{examplecounter}
\setcounter{examplecounter}{0}
    \newenvironment{example}[1]{
        \refstepcounter{examplecounter}%
        \vspace{3mm}\hspace{3mm}\\%
        \noindent\rule{\textwidth}{0.07cm}\\%
        \textbf{Worked Example \arabic{examplecounter}: #1}\\
    }{
        \hfill\\%
        \noindent\rule{\textwidth}{0.07cm}
    }


\begin{document}
    \chapter{First Chapter}
        \blindtext

        \begin{example}{Some are several pages}
           \blindtext[10]
        \end{example}

        \section{First Section}
            \blindtext
            \begin{example}{Others are shorter}
                Second Example
            \end{example}
\end{document}
share|improve this question
2  
Maybe the endfloat package in conjuction with the newfloat package can help? –  Alan Munn May 20 '12 at 23:25
1  
Perhaps you can have write the contents to a file (see stackoverflow.com/questions/2115379/…) and \include, then reopen the file at the end of each chapter. –  Ethan Bolker May 21 '12 at 1:07
2  
Please add to your question a minimal, yet complete document showing the current definition of your example environment. I ask you, because having this definition, we can be sure that possible solutions will effectively work for your definition. –  Gonzalo Medina May 21 '12 at 2:22
    
Thanks Gonzola, I originally didn't have a MWE because it was a general query, but I'll add one –  will-hart May 21 '12 at 17:38
add comment

1 Answer

up vote 7 down vote accepted

Following Alan Munn's comment, here's one possible option using the newfloat, endfloat and caption packages: first, we declare a new floating environment example, and then we declare this environment as a delay-aware one.

The heading for each example can be obtained using a customized version of \caption with empty argument.

Some options were added to allow more than one object appearing in the same page and to prevent automatic generation of a list of examples at the beginning of each group of delayed examples; only one list is produced at the beginning of the document, using \listofexamples.

Using \processdelayedfloats, the examples can be processed at the end of every chapter and, thanks to a redefinition of \chapter provided by Werner, this is done automatically.

The code:

\documentclass{report}
\usepackage{caption}
\usepackage{newfloat}
\usepackage[nomarkers]{endfloat}

% the delayed floats will be automatically processed at the end of each chapter
\let\oldchapter\chapter
\renewcommand\chapter{%
  \processdelayedfloats%
  \oldchapter}
\AtEndDocument{\processdelayedfloats}

\DeclareFloatingEnvironment{example}
\DeclareDelayedFloat{example}{Examples}

 % we allow several floats per page
\renewcommand\efloatseparator{\mbox{}}
% no automatic list of examples; it will be manually generated with \listofexamples
\SetupDelayedFloat{example}{nolist}

% captions flushed to the left margin and boldfaced
\captionsetup[example]{singlelinecheck=off,labelfont=bf,skip=.4\baselineskip}

\begin{document}
\listofexamples

\chapter{Test Chapter One}
\begin{example}
\caption{}
Test example one.
\end{example}

\begin{example}
\caption{}
Test example two.
\end{example}

\chapter{Test Chapter Two}
\begin{example}
\caption{}
Test example three.
\end{example}

\begin{example}
\caption{}
Test example four.
\end{example}

\end{document}

And the document obtained:

enter image description here

enter image description here

enter image description here

Noe that additional information has been added to the original question, it is clear that my previous suggestion won't be useful, since some examples might contain page breaks and floating objects don't admit them.

A possible solution now is to use the collect environment from the collect package; below is an example of its usage.

We declare the exa collection and define a new environment example which uses the collect environment; the two mandatory arguments of collect are used for the formatting (see remark below). The etoolbox package was used to patch \includecollection, so that this command will produce a heading in the form of an unnumbered chapter.

\documentclass[openany]{book}% openany just for the example
\usepackage{etoolbox}
\usepackage{collect}
\usepackage{lipsum}

\definecollection{exa}
\newcounter{examplecounter}[chapter]

\makeatletter
\newenvironment{example}[1]
  {\@nameuse{collect}{exa}{%
    \refstepcounter{examplecounter}
    \vspace{3mm}%
    \noindent\rule{\textwidth}{0.07cm}\par\nobreak\noindent
    \textbf{Worked Example \arabic{examplecounter}: #1}\par
    }{%
    \par\nobreak\vskip.1\baselineskip\noindent\rule{\textwidth}{0.07cm}%
    }
  }%
  {\@nameuse{endcollect}}
\makeatother

\pretocmd{\includecollection}{\chapter*{Examples for Chapter~\thechapter}}{}{}

\begin{document}

\chapter{First Chapter}
\lipsum[2]

\begin{example}{Some are several pages}
\lipsum[1-6]
\end{example}

\section{First Section of Chapter One}
\lipsum[2]

\begin{example}{Others are shorter}
\lipsum[2]
\end{example}

\includecollection{exa}

\chapter{Second Chapter}
\lipsum[2]

\begin{example}{Another short example}
\lipsum[2]
\end{example}

\section{First Section of Chapter Two}
\lipsum[2]

\begin{example}{Another long example}
\lipsum[1-6]
\end{example}

\includecollection{exa}

\end{document}

Images of the output:

enter image description here

enter image description here

enter image description here

I left the horizontal rules of the original definition, but if the examples will be collected at the end of every chapter in a section of their own, the rules are superfluous and can be removed.

share|improve this answer
    
Hey Gonzalo, thanks for your detailed answer! I'll give this a go and see how it looks - although I'm not sure how this will handle multiple page examples? –  will-hart May 21 '12 at 17:55
1  
@mecharius You're welcome! My code won't allow page breaks inside the example environment, since the environment is treated as a floating object and floats don't admit page breaks. Now that you've provided some more information, a convenient solution can be designed. –  Gonzalo Medina May 21 '12 at 18:42
    
@mecharius I've updated my answer with a solution allowing page breaks; I am working on some improvements, so I will be updating the answer in the next hours. –  Gonzalo Medina May 21 '12 at 19:09
    
Great, thanks - didn't really require the MWE, but should have mentioned originally that the examples "could extend over multiple pages"... In any event, its good to have solutions for both cases! Thanks again. –  will-hart May 21 '12 at 19:54
    
@mecharius right; all that was needed was the info about multiple pages ;-). I am glad I could help. –  Gonzalo Medina May 21 '12 at 20:05
add comment

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.