我的代码
\documentclass[12pt,a4paper,oneside]{book}
\usepackage{amsmath}
\usepackage[top=2cm,bottom=2cm,left=2cm,right=2cm]{geometry}
\usepackage{tikz}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
%%%%%%%%
\newtheorem{ex}{Example}
%%%%%%%%
\AtBeginEnvironment{ex}{
\begin{tcolorbox}[colframe=black,colback=blue!10,breakable,boxrule=0.5pt,sharp corners]
}
\AtEndEnvironment{ex}{
\end{tcolorbox}
}
\begin{document}
\begin{ex}
\lipsum[1]
\end{ex}
\begin{ex}
\lipsum[2]
\end{ex}
\begin{tcolorbox}[enhanced,title=Example 1,
colframe=blue!50!black,colback=blue!10!white,colbacktitle=blue!5!yellow!10!white,
fonttitle=\bfseries,coltitle=black,attach boxed title to top center=
{yshift=-0.25mm-\tcboxedtitleheight/2,yshifttext=2mm-\tcboxedtitleheight/2},
boxed title style={boxrule=0.5mm,
frame code={ \path[tcb fill frame] ([xshift=-4mm]frame.west)
-- (frame.north west) -- (frame.north east) -- ([xshift=4mm]frame.east)
-- (frame.south east) -- (frame.south west) -- cycle; },
interior code={ \path[tcb fill interior] ([xshift=-2mm]interior.west)
-- (interior.north west) -- (interior.north east)
-- ([xshift=2mm]interior.east) -- (interior.south east) -- (interior.south west)
-- cycle;} }]
\lipsum[1]
\end{tcolorbox}
\end{document}
答案1
您可以newtcbtheorem
像以下代码一样声明环境。有关此命令的更多信息,请参阅文档theorem
中的部分tcolorbox
或更新部分这个答案
\documentclass[12pt,a4paper,oneside]{book}
\usepackage{amsmath}
\usepackage[top=2cm,bottom=2cm,left=2cm,right=2cm]{geometry}
\usepackage{tikz}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\newtcbtheorem[number within=chapter]{ex}{Example}{%
enhanced,
colframe=blue!50!black, colback=blue!10!white, colbacktitle=blue!5!yellow!10!white,
fonttitle=\bfseries, coltitle=black,
attach boxed title to top center = {%
yshift=-0.25mm-\tcboxedtitleheight/2,yshifttext=2mm-\tcboxedtitleheight/2},
boxed title style={boxrule=0.5mm,
frame code={ \path[tcb fill frame] ([xshift=-4mm]frame.west)
-- (frame.north west) -- (frame.north east) -- ([xshift=4mm]frame.east)
-- (frame.south east) -- (frame.south west) -- cycle; },
interior code={ \path[tcb fill interior] ([xshift=-2mm]interior.west)
-- (interior.north west) -- (interior.north east)
-- ([xshift=2mm]interior.east) -- (interior.south east) -- (interior.south west)
-- cycle;}}
}{ex}
\begin{document}
\begin{ex}{A nice example}{1}
\lipsum[1]
\end{ex}
\begin{ex}{}{}
As you can see in theorem~\ref{ex:1}
\lipsum[2]
\end{ex}
\end{document}