In a question I asked before, I implemented method 2 by feculededentier. However, now I am facing difficulty in referring to different examples in this case. For example, what should be the Latex code for "as shown in Example 1.2"?
1 Answers
You have to slightly change the definition. Make it 2 arguments with first one as optional:
\newtcolorbox[auto counter, number within=chapter, number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}}]{myexample}[2][]{%
enhanced,
breakable,
fonttitle=\bfseries,
title=Example~\thetcbcounter: #2,
#1
}
And then use the label=<name> key like
\begin{myexample}[label=second]{Second example}
Now you can use Example~\ref{second} to refer to these boxes.
Full example (taken and modified from the answer linked in the question)
\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=first]{First example}
\lipsum[4]
\end{myexample}
\begin{myexample}[label=second]{Second example}
\lipsum[4]
\end{myexample}
\chapter{Second chapter}
\begin{myexample}[label=third]{Third example}
\lipsum[4]
\end{myexample}
From Example~\ref{second} we get some idea.
\end{document}
cleveref version:
You can load cleveref package and use crefname in the options to reduce typing.
\documentclass{book}
\usepackage{cleveref}
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable}
\usepackage{lipsum}
\newtcolorbox[auto counter, number within=chapter,
crefname={example}{examples},
Crefname={Example}{Examples},
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=first]{First example}
\lipsum[4]
\end{myexample}
\begin{myexample}[label=second]{Second example}
\lipsum[4]
\end{myexample}
\chapter{Second chapter}
\begin{myexample}[label=third]{Third example}
\lipsum[4]
\end{myexample}
\Cref{second} gives us some idea as seen from \cref{first}.
\end{document}

tcolorboxpackage. – Aug 28 '15 at 06:07