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?

  • 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 '15 at 3:16
6

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

  • How do I refer to different examples in this case? For example, what should be Latex code for "as shown in Example 1.2"? – Qasim Chaudhari Aug 28 '15 at 4:41
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

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.