Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to take this tcolorbox created by Gonzalo Medina here Typesetting a definition but have each usage of the box index starting from 1, 2, ... which allows a title input.

Currently my set up looks like:

enter image description here

So it says Example Problem (number) (what the problem is about)

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

\newtcolorbox{Examples}{
      breakable,
      fonttitle=\bfseries,
      title={Example Problem (indexing number here) rest of title:}
}
\begin{document}
\begin{Examples}
  \lipsum[4]
\end{Examples}
\end{document}

enter image description here

Can this something like this be done?

Edit 1:

Following Gonzalo Medina's first code, the first letter is being cut off in the box.

enter image description here

That should be Calculate not alculate. So this was caused by having

\begin{examples}{Synodic Period of the Earth/Mars Orbits} 

without {test}. Does {test} always have to end it?

Edit 2

The colon wont appear after the descriptions

\documentclass{article}
\usepackage{tikz}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\usepackage{standalone}
\usepackage{capt-of}
\usepackage[pdfencoding = auto, psdextra, bookmarksdepth = 4]{hyperref}
\usepackage[noabbrev]{cleveref}

\tcbset{
  theorem/.style args = {#1#2#3#4}{%                                                
    step and label = {#2}{#4},%                                                     
    title = {\mbox{#1~\csname the#2\endcsname\ }#3:}}
}
\newcounter{exa}
\tcbmaketheorem{examples}{Example Problem}{
  breakable,
  fonttitle = \bfseries,
}{exa}{ex}

\begin{document}
\begin{examples}{example}{exa:test}
  \lipsum[4]
  \par\medskip
  \begin{minipage}{\linewidth}
    \centering
    \includestandalone{line}
    \captionof{figure}[line]{line}
    \label{line}
  \end{minipage}
\end{examples}
\cref{line}
\end{document}

enter image description here

share|improve this question

1 Answer 1

up vote 7 down vote accepted

In this case, you can use \tcbmaketheorem to define the environment:

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

\newcounter{exa}
\tcbmaketheorem{Examples}{Example}{
breakable,
fonttitle=\bfseries,
}{exa}{ex}

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

enter image description here

To get the exact style in the question, the theorem style can be redefined:

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

\tcbset{
  theorem/.style args={#1#2#3#4}{%
    step and label={#2}{#4},%
    title={\mbox{#1~\csname the#2\endcsname\ }#3:}}
}
\newcounter{exa}
\tcbmaketheorem{Examples}{Example}{
breakable,
fonttitle=\bfseries,
}{exa}{ex}

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

enter image description here

The syntax for \tcbmaketheorem is:

\tcbmaketheorem{<name>}{<display name>}{<options>}{<counter>}{<prefix>}

the meaning of the first four arguments is clear. <prefix>, together with the <marker> used in the second mandatory argument of the invocation of the environment:

\begin{<name>}{<additional description>}{<marker>}
content
\end{<name>}

will be used to produce a string for cross references having the form <prefix>:<marker> for cross-referencing. For example, with the settings of my example above:

\tcbmaketheorem{Examples}{Example}{
breakable,
fonttitle=\bfseries,
}{exa}{ex}

and

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

the declared prefix was ex and the particular invocation has test as marker, so you can cross-reference the example using \ref{ex:test}.

Floats are not allowed inside the environment; instead you can use a minipage and provide captions with the help of \captionof (from the caption or capt-of packages). A little example:

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{capt-of}
\usepackage[nopar]{lipsum}

\tcbset{
  theorem/.style args={#1#2#3#4}{%
    step and label={#2}{#4},%
    title={\mbox{#1~\csname the#2\endcsname\ }#3:}}
}
\newcounter{exa}
\tcbmaketheorem{Examples}{Example}{
breakable,
fonttitle=\bfseries,
}{exa}{ex}

\begin{document}

\begin{Examples}{Additional description}{exa:test}
\lipsum[4]\par\medskip
\begin{minipage}{\linewidth}
\centering
\includegraphics[height=3cm]{example-image-a}
\captionof{figure}{the description of the image}
\label{fig:testa}
\end{minipage}\par\medskip
\lipsum[4]
\end{Examples}

\end{document}

enter image description here

Update:

With the changes introduced in tcolorbox version 2.40 (2013/07/15), the change for the style above won't produce the desired result for the title. With this version, the following code has to be used:

\documentclass{article}
\listfiles
\usepackage{tikz}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\usepackage{standalone}
\usepackage{capt-of}
\usepackage[pdfencoding = auto, psdextra, bookmarksdepth = 4]{hyperref}
\usepackage[noabbrev]{cleveref}

\makeatletter
\def\tcb@theo@title#1#2#3{%
  \ifdefempty{#2}{\setbox\z@=\hbox{#1}}{\setbox\z@=\hbox{#1~#2}}%
  \def\temp@a{#3}%
  \ifx\temp@a\@empty\relax%
    \unhbox\z@:%
  \else%
    \setbox\z@=\hbox{\unhbox\z@\ }%
    \hangindent\wd\z@%
    \hangafter=1%
    \mbox{\unhbox\z@}#3:%
  \fi%
}
\makeatother
\newcounter{exa}
\tcbmaketheorem{examples}{Example Problem}{
  breakable,
  fonttitle = \bfseries,
}{exa}{ex}

\begin{document}
\begin{examples}{Additional description}{test}
  \lipsum[4]
  \par\medskip
  \begin{minipage}{\linewidth}
    \centering
    \includestandalone{line}
    \captionof{figure}[line]{line}
    \label{line}
  \end{minipage}
\end{examples}
\cref{line}
\end{document}

New Update:

The author of tcolorbox has introduced some modifications to tcbtheorems.code.tex in version 2.41 (2013/07/23), so now all the examples in my answers work.

share|improve this answer
1  
@dustin It will be used as a marker for cross-references. In some minutes I can add some explanations to the code; it's dinner time here ;-) –  Gonzalo Medina Jul 21 '13 at 1:23
1  
@dustin not exactly; I'll add an example in some seconds. In the meantime I've added an explanation of the use of the second mandatory argument, for labelling. –  Gonzalo Medina Jul 21 '13 at 2:15
1  
@dustin answer updated with the code required using the newest version of tcolorbox. –  Gonzalo Medina Jul 21 '13 at 20:40
1  
@GonzaloMedina With tcolorbox version 2.41 (2013/07/23) both of your codes (will) work. I switched \tcbmaketheorem back to its old v2.33 behaviour since I considered backward compatibilty broken. Nevertheless, your new code also works with exception of \includestandalone{line} where a file line.tex seems to be missing. –  Thomas F. Sturm Jul 23 '13 at 9:45
1  
@dustin the previous comments might be of interest for you. With the newest version of tcolorbox, the previous solution will again work (the newest one will also do the job). –  Gonzalo Medina Jul 23 '13 at 13:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.