0

I'd like to create a tcolorbox that looks like "example" sections in many textbooks, for example, something like the attached screenshot.

n

I found one sample code from an online source, which uses mdframed. Yet I'd like to include as less packages as possible, thus wondering if there is ways to rewrite using tcolorbox. Thank you!

The sample code is:

\newlength{\framedinnerleftmargin}
\newlength{\framedinnertopmargin}
\newlength{\framedreversedinnerleftmargin}
\setlength{\framedinnerleftmargin}{\widthof{Theoreme 10.10.10}+2em}
\setlength{\framedreversedinnerleftmargin}{\widthof{Theoreme 10.10.10}+1em}
\setlength{\framedinnertopmargin}{1em}

% first argument: label in upper left corner,
% second argument: background color
\newenvironment{boxedtext}[2]{\begin{mdframed}[%
hidealllines=true,%
backgroundcolor=#2,%
innertopmargin=\framedinnertopmargin,%
innerleftmargin=\framedinnerleftmargin,%
innerrightmargin=1em%
]%
\tabto{-\framedreversedinnerleftmargin}\textbf{#1}\tabto*{0em}%
}% begin code
{\hskip 0pt\\\hspace*{\fill}\end{mdframed}\vspace{1em}} % end code

\newenvironment{summary}[0]{\begin{center}\begin{minipage}[c]{\summarywidth}\begin{spacing}{0.9}\footnotesize} % begin code
{\end{spacing}\end{minipage}\end{center}} % end code

\newcounter{question}

% optional! if you want it to start at zero
% with every new chapter/section/etc.
\numberwithin{question}{section}

\newenvironment{question}[0]
{\refstepcounter{question}\vspace{1em plus 1em}\begin{boxedtext}{Question \thequestion.}{blue!7}}%\setlength{\parskip}{0em}}
{\end{boxedtext}\vspace{-1em plus 1em}}

\newenvironment{question*}[0]
{\vspace{1em plus 1em}\begin{boxedtext}{Question.}{blue!7}}
{\end{boxedtext}\vspace{-1em plus 1em}}

1 Answer 1

2

Something to start with. Refer to the documentation of tcolorbox for more customisation.

\documentclass{article}

\usepackage{blindtext}
\usepackage[skins]{tcolorbox}

\begin{document}

\newtcolorbox[auto counter,number within=section]{myexample}[2][]{%
    coltitle=black,
    title={{\bfseries Example~\thetcbcounter.}\quad#2},
    colback=black!5,
    colframe=black!10,
    coltext=blue,
    #1
}

\begin{myexample}[]{Example Title}
    Content. \blindtext[1]
\end{myexample}

\end{document}

enter image description here

You must log in to answer this question.

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