1

I need 2 different Figure captions: Examples and Figures. If I use the \renewcommand, every time a figures appears, the counter won't stop and keep adding, like: Example 1. Figure 2. Example 3. Figure 4...

And I need: Example 1. Figure 1. Example 2. Figure 2.

Also, is it possible to make these 2 lists separate at the ToC?

Thank you.

Lotto
  • 63
  • 2
    Welcome to TeX.SE. Like playing the Lotto (Lottery, sorry, couldn't resist ;-)) the result is by chance and we can only guess what you're really trying to do. Please post your code here. And you're asking to different questions, actually –  Apr 05 '17 at 15:11
  • I would say you want to create a \newcounter{Example} and an associated \newenvironment{Example}, which internally calls \addtocounter{Example}{1}. – Manuel Weinkauf Apr 05 '17 at 15:22
  • If you don't know what is a minimal working example (MWE), see here: https://tex.meta.stackexchange.com/questions/228/ive-just-been-asked-to-write-a-minimal-example-what-is-that – CarLaTeX Apr 05 '17 at 15:22
  • 2
    @ManuelWeinkauf: \refstepcounter{Example} is to be preferred if cross-referencing is required, however –  Apr 05 '17 at 15:24
  • 1
    @ChristianHupfer Thanks, I did not know that. – Manuel Weinkauf Apr 05 '17 at 16:14

2 Answers2

3

The package float might do what you need.

\usepackage{float}
\floatstyle{ruled} % Defines how the new floats should look like.
\newfloat{example}{htbp}{loe} % Defines a new float environment, with captions written to a file with extension `loe` (list of examples).
\floatname{example}{Example} % Label for captions
...
\begin{document}
...
\begin{example}
  \centering
  This is an example.
  \caption{Some example}\label{ex:a}
\end{example}
...
See example~\ref{ex:a}.
...
\listof{example}{List of Examples} % Print a list of captions of all examples.

To let the list of figures (and tables) appear in the table of contents, you can add

% Add list of figures and tables to table of contents
\usepackage[nottoc]{tocbibind}

to the preamble. To add also the lists of new floats (like the list of examples) to the table of contents, it seems we have to patch the \listof command provided by the float package. Add the following lines immediately after \usepackage{float}.

\usepackage{xpatch}
% Add the lists of new floats to table of contents
\makeatletter
%\xpatchcmd\listof{\@starttoc}{\addcontentsline{toc}{section}{#2}\@starttoc}{}{}% For article class
\xpatchcmd\listof{\@starttoc}{\addcontentsline{toc}{chapter}{#2}\@starttoc}{}{}% For report and book class
\makeatother

Here is a complete example.

enter image description here

\documentclass{report}
\usepackage{graphicx}
 % Add list of figures and tables to table of contents
\usepackage[nottoc]{tocbibind}
\usepackage{float}
\usepackage{xpatch}
% Add the lists of new floats to table of contents
\makeatletter
%\xpatchcmd\listof{\@starttoc}{\addcontentsline{toc}{section}{#2}\@starttoc}{}{}% For article class
\xpatchcmd\listof{\@starttoc}{\addcontentsline{toc}{chapter}{#2}\@starttoc}{}{}% For report and book class
\makeatother
\floatstyle{ruled}
\newfloat{example}{htbp}{loe}
\floatname{example}{Example}
\begin{document}
\tableofcontents
\listoffigures
\listof{example}{List of Examples}
\chapter{The first chapter}
This document shows two figures, \ref{fig:a} and \ref{fig:b},
as well two examples, \ref{ex:a} and \ref{ex:b}.

\begin{figure}
  \centering
  \includegraphics[width=3cm]{example-image-a}
  \caption{Some figure}\label{fig:a}
\end{figure}
\begin{example}
  \centering
  This is an example for something.
  \caption{Some example}\label{ex:a}
\end{example}
\begin{figure}
  \centering
  \includegraphics[width=3cm]{example-image-b}
  \caption{Another figure}\label{fig:b}
\end{figure}
\begin{example}
  \centering
  Yet another example.
  \caption{Another example}\label{ex:b}
\end{example}
\end{document}
gernot
  • 49,614
  • This is very helpful. Thank you very much!

    I didn't use the \floatstyle{ruled} Hopefully this won't bring any problem to the document.

    – Lotto Apr 06 '17 at 03:11
  • @Lotto Which \floatstyle to use is a matter of taste/design. I just used it for demonstration purposes. The options are plain (the default if you omit \floatstyle), plaintop, boxed, and ruled. – gernot Apr 06 '17 at 06:43
2

Another approach using package tocbasic, which is used by all KOMA-classes.
With a KOMA-class, package float isn't the best choice.

\documentclass{article}
\usepackage{graphicx}
\usepackage{tocbasic}
\DeclareNewTOC[
    type=example,
    types=examples,
    float,
name=Example]{loe}
\setuptoc{loe}{totoc}
\begin{document}

\tableofcontents


\rule{.8\textwidth}{.4pt}

This document shows two figures, \ref{fig:a} and \ref{fig:b},
as well two examples, \ref{ex:a} and \ref{ex:b}.

\begin{figure}
    \centering
    \includegraphics[width=3cm]{example-image-a}
    \caption{Some figure}\label{fig:a}
\end{figure}
\begin{example}
    \centering
    This is an example for something.
    \caption{Some example}\label{ex:a}
\end{example}
\begin{figure}
    \centering
    \includegraphics[width=3cm]{example-image-b}
    \caption{Another figure}\label{fig:b}
\end{figure}
\begin{example}
    \centering
    Yet another example.
    \caption{Another example}\label{ex:b}
\end{example}
\listoffigures
\listofexamples
\end{document}
Johannes_B
  • 24,235
  • 10
  • 93
  • 248