我想创建一个tcolorbox
看起来像许多教科书中的“示例”部分,例如类似于所附的屏幕截图。
我从中找到了一段示例代码在线来源,它使用mdframed
。但我想包含尽可能少的包,因此想知道是否有办法使用 重写tcolorbox
。谢谢!
示例代码为:
\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
开始吧。请参阅文档以tcolorbox
了解更多自定义内容。
\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}