5

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 Answer 1

5

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}

enter image description here

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}
3
  • Not working for me. Adding #2, #1 creates the problem. The error is missing number, treated as zero.
    – QMC
    Aug 28, 2015 at 6:02
  • @QasimChaudhari Are you compiling the code as such without modifying? If you still get the error, please try updating your tcolorbox package.
    – user11232
    Aug 28, 2015 at 6:07
  • Harish, it worked very well now. Thanks! Actually I had a \vspace{2mm} immediately after starting the box, and I had headings with square brackets [ ] as answered in my previous question. I have changed these two things (put a line break after starting the box, and use { } for headings) to solve the problem.
    – QMC
    Aug 28, 2015 at 7:14

You must log in to answer this question.

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