8

I found this link which explains how to add index number to example in tcolorbox. However, I have been unable to modify it to include chapter reference numbering (e.g., Example 1.1, or 4.3, etc.). Any hints?

1
  • Please provide some basic code in the form of a minimal example (or see here). You are more likely to attract a useful answer that way because you have shown that you are willing to help those who would help you.
    – jon
    Sep 6, 2015 at 3:16

2 Answers 2

7

Updated 2015-08-29 to include a label, as requested by @Qasim.


Use tcolorbox's number freestyle option along with auto counter and number within=chapter, as in the following MWE. You can also add a label by using an optional (first) argument to \newtcolorbox (the second mandatory argument will be the title).

\documentclass{book}
\usepackage{tcolorbox}
    \tcbuselibrary{skins,breakable}
\usepackage{lipsum}

\newtcolorbox[auto counter, number within=chapter, number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}}]{myexample}[2][]{%
    enhanced,
    breakable,
    fonttitle=\bfseries,
    title=Example~\thetcbcounter: #2,
    #1
}

\begin{document}

\chapter{First chapter}

\begin{myexample}[label=exfirst]{First example}
  \lipsum[4]
\end{myexample}

\begin{myexample}{Second example}
  \lipsum[4]
\end{myexample}

\chapter{Second chapter}

\begin{myexample}[label=exthird]{Third example}
  \lipsum[4]
\end{myexample}

I can now refer to example~\ref{exfirst} and example~\ref{exthird}.
\end{document}


Output: Output for above code




If you would also like the title to be optional, you could use the following approach:

\documentclass{book}
\usepackage{etoolbox}
\usepackage{tcolorbox}
    \tcbuselibrary{skins,breakable}
\usepackage{lipsum}

\tcbset{mytitle/.style={title={Example~\thetcbcounter\ifstrempty{#1}{}{: #1}}}}
\newtcolorbox[auto counter, number within=chapter, number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}}]{myexample}[1][]{%
    enhanced,
    breakable,
    fonttitle=\bfseries,
    mytitle={},
    #1
}

\begin{document}

\chapter{First chapter}

\begin{myexample}[mytitle={First example}, label=exfirst]
  \lipsum[4]
\end{myexample}

\begin{myexample}
  \lipsum[4]
\end{myexample}

\chapter{Second chapter}

\begin{myexample}[mytitle={Third example}, label=exthird]
  \lipsum[4]
\end{myexample}

I can now refer to example~\ref{exfirst} and example~\ref{exthird}.
\end{document}


Output: Output for above code

2
  • How do I refer to different examples in this case? For example, what should be Latex code for "as shown in Example 1.2"?
    – QMC
    Aug 28, 2015 at 4:41
  • @QMC \ref{exfirst} should give you the number for the first example assuming that you put label=exfirst in options the for \begin{myexample}.
    – Galen
    Jun 21, 2021 at 18:35
5

You need to redefine the exa counter to achieve what you want:

\documentclass{book}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\newcounter{exa}
\renewcommand\theexa{\thechapter.\arabic{exa}} 
\tcbmaketheorem{Examples}{Example}{
breakable,
fonttitle=\bfseries,
}{exa}{ex}

\begin{document}
\chapter{Foo}
\begin{Examples}{Additional description}{test}
  \lipsum[4]
\end{Examples}
\end{document}

enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .