我想为我的示例定义一个新的彩色环境。此环境顶部有两个并排的标题栏。一个用绿色填充,其中有一个单词“示例”。另一个用蓝色填充,我们在其中写入示例的编号,例如“1-2”(第 1 章,示例 2)。最后,我们在红色框中写入示例的主体。值得一提的是,此环境必须具有跨页分解的能力。
答案1
这是改进的第三个版本,使用mdframed
包和命令的定义\mymdtheorem
按照原来的模型进行\mdtheorem
,但允许标题使用两种颜色。
该环境可以在带星号(无编号)的版本上使用,也可以在不带附加说明的情况下使用:
\documentclass{book}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}
\colorlet{titlecolor}{green!20}
\colorlet{numbercolor}{blue!20}
\makeatletter
\DeclareDocumentCommand{\mymdtheorem}{ O{} m o m o }%
{\ifcsdef{#2}%
{\mdf@PackageWarning{Environment #2 already exits\MessageBreak}}%
{%
\IfNoValueTF {#3}%
{%#3 not given -- number relationship
\IfNoValueTF {#5}
{%#3+#5 not given
\@definecounter{#2}%
\expandafter\xdef\csname the#2\endcsname{\@thmcounter{#2}}
\newenvironment{#2}[1][]{%
\refstepcounter{#2}
\ifstrempty{##1}%
{\let\@temptitle\relax}%
{%
\def\@temptitle{\mdf@theoremseparator%
\mdf@theoremspace%
\mdf@theoremtitlefont%
##1}%
}
\begin{mdframed}[#1,frametitle={\strut #4\ \csname the#2\endcsname\@temptitle}]}%
{\end{mdframed}}%
\newenvironment{#2*}[1][]{%
\ifstrempty{##1}{\let\@temptitle\relax}{\def\@temptitle{:\ ##1}}
\begin{mdframed}[#1,frametitle={\strut#4\@temptitle}]}%
{\end{mdframed}}%
}%
{%#5 given -- reset counter
\@definecounter{#2}\@newctr{#2}[#5]%
\expandafter\xdef\csname the#2\endcsname{\@thmcounter{#2}}
\expandafter\xdef\csname the#2\endcsname{%
\expandafter\noexpand\csname the#5\endcsname \@thmcountersep
\@thmcounter{#2}}%
\newenvironment{#2}[1][]{%
\refstepcounter{#2}
\ifstrempty{##1}%
{\let\@temptitle\relax}%
{%
\def\@temptitle{\mdf@theoremseparator%
\mdf@theoremspace%
\mdf@theoremtitlefont%
##1}%
}
\begin{mdframed}[#1,frametitle={\strut%
\begin{tikzpicture}[overlay,baseline=-0.5ex]
\node [fill=titlecolor,anchor=west,inner xsep=10pt,inner ysep=6.5pt,xshift=-10pt] (heading) {#4};
\node at (heading.east) [anchor=west,inner xsep=10pt,inner ysep=6.5pt] {\ \csname the#2\endcsname\@temptitle};
\end{tikzpicture}}]%
}%
{\end{mdframed}}%
\newenvironment{#2*}[1][]{%
\ifstrempty{##1}%
{\let\@temptitle\relax}%
{%
\def\@temptitle{%
\mdf@theoremspace%
\mdf@theoremtitlefont%
##1}%
}
\begin{mdframed}[#1,frametitle={\strut%
\begin{tikzpicture}[overlay,baseline=-0.5ex]
\node [fill=titlecolor,anchor=west,inner xsep=10pt,inner ysep=6.5pt,xshift=-10pt] (heading) {#4};
\node at (heading.east) [anchor=west,inner xsep=10pt,inner ysep=6.5pt] {\@temptitle};
\end{tikzpicture}}]%
}%
{\end{mdframed}}%
}%
}%
{%#3 given -- number relationship
\global\@namedef{the#2}{\@nameuse{the#3}}%
\newenvironment{#2}[1][]{%
\refstepcounter{#3}
\ifstrempty{##1}%
{\let\@temptitle\relax}%
{%
\def\@temptitle{\mdf@theoremseparator%
\mdf@theoremspace%
\mdf@theoremtitlefont%
##1}%
}
\begin{mdframed}[#1,frametitle={\strut%
\begin{tikzpicture}[overlay,baseline=-0.5ex]
\node [fill=titlecolor,anchor=west,inner xsep=10pt,inner ysep=6.5pt,xshift=-10pt] (heading) {#4};
\node at (heading.east) [anchor=west,inner xsep=10pt,inner ysep=6.5pt] {\ \csname the#2\endcsname\@temptitle};
\end{tikzpicture}}]%
}%
{\end{mdframed}}%
\newenvironment{#2*}[1][]{%
\ifstrempty{##1}{\let\@temptitle\relax}{\def\@temptitle{:\ ##1}}
\begin{mdframed}[#1,frametitle={\strut%
\begin{tikzpicture}[overlay,baseline=-0.5ex]
\node [fill=titlecolor,anchor=west,inner xsep=10pt,inner ysep=6.5pt,xshift=-10pt] (heading) {#4};
\node at (heading.east) [anchor=west,inner xsep=10pt,inner ysep=6.5pt] {\@temptitle};
\end{tikzpicture}}]%
}%
{\end{mdframed}}%
}%
}%
}
\makeatother
\mdfdefinestyle{theoremstyle}{%
hidealllines,
frametitlerule=true,%
frametitlebackgroundcolor=numbercolor,
frametitlerulewidth=1pt,
innertopmargin=\topskip,
backgroundcolor=red!20,
splittopskip=1.2\topskip,splitbottomskip=.6\topskip,
}
\mymdtheorem[style=theoremstyle]{example}{Example}[chapter]
\begin{document}
\chapter{Test Chapter}
\begin{example}
\lipsum[2]
\end{example}\par\bigskip
\begin{example*}
\lipsum[2]
\end{example*}\par\bigskip
\begin{example}[A numbered example with a note]
\lipsum[2]
\end{example}\par\bigskip
\begin{example*}[An unnumbered example with a note]
\lipsum[2]
\end{example*}
\end{document}
其中一个框架的图像:
以下是获得的完整文档的图像(可以看出,环境允许分页):
使用firstextra
,middleextra
可以secondextra
生成“封闭框架”和连续文本(正如阿泽蒂纳在注释中)针对示例内部出现分页符的情况:
\documentclass{book}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}
\colorlet{titlecolor}{green!20}
\colorlet{numbercolor}{blue!20}
\makeatletter
\DeclareDocumentCommand{\mymdtheorem}{ O{} m o m o }%
{\ifcsdef{#2}%
{\mdf@PackageWarning{Environment #2 already exits\MessageBreak}}%
{%
\IfNoValueTF {#3}%
{%#3 not given -- number relationship
\IfNoValueTF {#5}
{%#3+#5 not given
\@definecounter{#2}%
\expandafter\xdef\csname the#2\endcsname{\@thmcounter{#2}}
\newenvironment{#2}[1][]{%
\refstepcounter{#2}
\ifstrempty{##1}%
{\let\@temptitle\relax}%
{%
\def\@temptitle{\mdf@theoremseparator%
\mdf@theoremspace%
\mdf@theoremtitlefont%
##1}%
}
\begin{mdframed}[#1,frametitle={\strut #4\ \csname the#2\endcsname\@temptitle}]}%
{\end{mdframed}}%
\newenvironment{#2*}[1][]{%
\ifstrempty{##1}{\let\@temptitle\relax}{\def\@temptitle{:\ ##1}}
\begin{mdframed}[#1,frametitle={\strut#4\@temptitle}]}%
{\end{mdframed}}%
}%
{%#5 given -- reset counter
\@definecounter{#2}\@newctr{#2}[#5]%
\expandafter\xdef\csname the#2\endcsname{\@thmcounter{#2}}
\expandafter\xdef\csname the#2\endcsname{%
\expandafter\noexpand\csname the#5\endcsname \@thmcountersep
\@thmcounter{#2}}%
\newenvironment{#2}[1][]{%
\refstepcounter{#2}
\ifstrempty{##1}%
{\let\@temptitle\relax}%
{%
\def\@temptitle{\mdf@theoremseparator%
\mdf@theoremspace%
\mdf@theoremtitlefont%
##1}%
}
\begin{mdframed}[#1,frametitle={\strut%
\begin{tikzpicture}[overlay,baseline=-0.5ex]
\node [fill=titlecolor,anchor=west,inner xsep=10pt,inner ysep=6.5pt,xshift=-10pt] (heading) {#4};
\node at (heading.east) [anchor=west,inner xsep=10pt,inner ysep=6.5pt] {\ \csname the#2\endcsname\@temptitle};
\end{tikzpicture}}]%
}%
{\end{mdframed}}%
\newenvironment{#2*}[1][]{%
\ifstrempty{##1}%
{\let\@temptitle\relax}%
{%
\def\@temptitle{%
\mdf@theoremspace%
\mdf@theoremtitlefont%
##1}%
}
\begin{mdframed}[#1,frametitle={\strut%
\begin{tikzpicture}[overlay,baseline=-0.5ex]
\node [fill=titlecolor,anchor=west,inner xsep=10pt,inner ysep=6.5pt,xshift=-10pt] (heading) {#4};
\node at (heading.east) [anchor=west,inner xsep=10pt,inner ysep=6.5pt] {\@temptitle};
\end{tikzpicture}}]%
}%
{\end{mdframed}}%
}%
}%
{%#3 given -- number relationship
\global\@namedef{the#2}{\@nameuse{the#3}}%
\newenvironment{#2}[1][]{%
\refstepcounter{#3}
\ifstrempty{##1}%
{\let\@temptitle\relax}%
{%
\def\@temptitle{\mdf@theoremseparator%
\mdf@theoremspace%
\mdf@theoremtitlefont%
##1}%
}
\begin{mdframed}[#1,frametitle={\strut%
\begin{tikzpicture}[overlay,baseline=-0.5ex]
\node [fill=titlecolor,anchor=west,inner xsep=10pt,inner ysep=6.5pt,xshift=-10pt] (heading) {#4};
\node at (heading.east) [anchor=west,inner xsep=10pt,inner ysep=6.5pt] {\ \csname the#2\endcsname\@temptitle};
\end{tikzpicture}}]%
}%
{\end{mdframed}}%
\newenvironment{#2*}[1][]{%
\ifstrempty{##1}{\let\@temptitle\relax}{\def\@temptitle{:\ ##1}}
\begin{mdframed}[#1,frametitle={\strut%
\begin{tikzpicture}[overlay,baseline=-0.5ex]
\node [fill=titlecolor,anchor=west,inner xsep=10pt,inner ysep=6.5pt,xshift=-10pt] (heading) {#4};
\node at (heading.east) [anchor=west,inner xsep=10pt,inner ysep=6.5pt] {\@temptitle};
\end{tikzpicture}}]%
}%
{\end{mdframed}}%
}%
}%
}
\makeatother
\mdfdefinestyle{theoremstyle}{%
hidealllines,
frametitlerule=true,%
frametitlebackgroundcolor=numbercolor,
frametitlerulewidth=1pt,
innertopmargin=\topskip,
backgroundcolor=red!20,
splittopskip=1.5\topskip,splitbottomskip=\topskip,
firstextra={%
\draw (P|-O) -- +(-\textwidth,0);
},
middleextra={%
\node[draw,anchor=west,yshift=0.7cm,fill=titlecolor] at (P-|O) {\bfseries Example~\theexample~Continued};
\draw (O-|P) -- +(\textwidth,0);
},
secondextra={%
\node[draw,anchor=west,yshift=0.7cm,fill=titlecolor,inner xsep=8pt] at (P-|O) {\bfseries Example~\theexample~Continued};
\draw (P-|O) -- +(\textwidth,0);
}
}
\mymdtheorem[style=theoremstyle]{example}{Example}[chapter]
\begin{document}
\chapter{Test Chapter}
\begin{example}
\lipsum[1-6]
\end{example}
\end{document}
由于彩色框架表示继续,因此这个继续文本可能是多余的,但我只是想说明一些强大的功能mdframed
。