注本文非原创,来自:http://www.latexstudio.net/archives/4482
mdframed宏包可以轻松搞定一个带框的段落样式,而且可以自动跨页。另外,宏包还有很强的定制设计部分,之前,我们分享的一个定理环境就是用的该宏包进行制作的。本文将比较详细的介绍该宏包的使用。
在导言区,引入mdframed宏包且可以直接定义定理,引理,证明,推论这些环境。引入该宏包的时候,推荐选择TikZ的方法来进行框架绘制。
- \usepackage[framemethod=TikZ]{mdframed}
接下来,我们可以定义一个带计数器的环境了,比如,我们比较熟悉的定理环境,我们通常希望这些定理环境能够和小节的序号关联起来,并且能够在小节标题序号变化的时候能够重置。那么我们可以做如下:
- \newcounter{theo}[section]\setcounter{theo}{0}
- \renewcommand{\thetheo}{\arabic{section}.\arabic{theo}}
按照一般的环境定义,我们可以定义如下环境:
- \newenvironment{theo}[2][]{%
- \refstepcounter{theo}
-
- % Code for box design goes here.
-
- \begin{mdframed}[]\relax}{%
- \end{mdframed}}
用mdframed设计的定理,计数器的部分是放在一个box里的,因此有时box会有显示定理的具体名称,有时只是单纯的定理序号,因此我们还需要判断不同情况的box设计。我们可以用\ifstringempty{#1}来判断定理里面是否有内容。我们的代码可以改写为:
- \newcounter{lem}[section]\setcounter{lem}{0}
- \renewcommand{\thelem}{\arabic{section}.\arabic{lem}}
- \newenvironment{lem}[2][]{%
- \refstepcounter{lem}%
- \ifstrempty{#1}%
- {\mdfsetup{%
- frametitle={%
- \tikz[baseline=(current bounding box.east),outer sep=0pt]
- \node[anchor=east,rectangle,fill=green!20]
- {\strut Lemma~\thelem};}}
- }%
- {\mdfsetup{%
- frametitle={%
- \tikz[baseline=(current bounding box.east),outer sep=0pt]
- \node[anchor=east,rectangle,fill=green!20]
- {\strut Lemma~\thetheo:~#1};}}%
- }%
- \mdfsetup{innertopmargin=10pt,linecolor=green!20,%
- linewidth=2pt,topline=true,%
- frametitleaboveskip=\dimexpr-\ht\strutbox\relax
- }
- \begin{mdframed}[]\relax%
- \label{#2}}{\end{mdframed}}
同样的方法,我们可以设计证明环境的样式如下:
- \newcounter{prf}[section]\setcounter{prf}{0}
- \renewcommand{\theprf}{\arabic{section}.\arabic{prf}}
- \newenvironment{prf}[2][]{%
- \refstepcounter{prf}%
- \ifstrempty{#1}%
- {\mdfsetup{%
- frametitle={%
- \tikz[baseline=(current bounding box.east),outer sep=0pt]
- \node[anchor=east,rectangle,fill=red!20]
- {\strut Proof~\theprf};}}
- }%
- {\mdfsetup{%
- frametitle={%
- \tikz[baseline=(current bounding box.east),outer sep=0pt]
- \node[anchor=east,rectangle,fill=red!20]
- {\strut Proof~\thetheo:~#1};}}%
- }%
- \mdfsetup{innertopmargin=10pt,linecolor=red!20,%
- linewidth=2pt,topline=true,%
- frametitleaboveskip=\dimexpr-\ht\strutbox\relax
- }
- \begin{mdframed}[]\relax%
- \label{#2}}{\end{mdframed}}
通常证明结束的地方,有个结束符,那么我们可以载入amsthm宏包,定义如下:
- % Load package
- \usepackage{amsthm}
-
- % Change last line of above code
- \begin{mdframed}[]\relax%
- \label{#2}}{\qed\end{mdframed}}
完整的代码和效果如下:
- \documentclass{article}
- \usepackage[framemethod=TikZ]{mdframed}
- \usepackage{amsthm}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %Theorem
- \newcounter{theo}[section] \setcounter{theo}{0}
- \renewcommand{\thetheo}{\arabic{section}.\arabic{theo}}
- \newenvironment{theo}[2][]{%
- \refstepcounter{theo}%
- \ifstrempty{#1}%
- {\mdfsetup{%
- frametitle={%
- \tikz[baseline=(current bounding box.east),outer sep=0pt]
- \node[anchor=east,rectangle,fill=blue!20]
- {\strut Theorem~\thetheo};}}
- }%
- {\mdfsetup{%
- frametitle={%
- \tikz[baseline=(current bounding box.east),outer sep=0pt]
- \node[anchor=east,rectangle,fill=blue!20]
- {\strut Theorem~\thetheo:~#1};}}%
- }%
- \mdfsetup{innertopmargin=10pt,linecolor=blue!20,%
- linewidth=2pt,topline=true,%
- frametitleaboveskip=\dimexpr-\ht\strutbox\relax
- }
- \begin{mdframed}[]\relax%
- \label{#2}}{\end{mdframed}}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %Lemma
- \newcounter{lem}[section] \setcounter{lem}{0}
- \renewcommand{\thelem}{\arabic{section}.\arabic{lem}}
- \newenvironment{lem}[2][]{%
- \refstepcounter{lem}%
- \ifstrempty{#1}%
- {\mdfsetup{%
- frametitle={%
- \tikz[baseline=(current bounding box.east),outer sep=0pt]
- \node[anchor=east,rectangle,fill=green!20]
- {\strut Lemma~\thelem};}}
- }%
- {\mdfsetup{%
- frametitle={%
- \tikz[baseline=(current bounding box.east),outer sep=0pt]
- \node[anchor=east,rectangle,fill=green!20]
- {\strut Lemma~\thetheo:~#1};}}%
- }%
- \mdfsetup{innertopmargin=10pt,linecolor=green!20,%
- linewidth=2pt,topline=true,%
- frametitleaboveskip=\dimexpr-\ht\strutbox\relax
- }
- \begin{mdframed}[]\relax%
- \label{#2}}{\end{mdframed}}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %Proof
- \newcounter{prf}[section]\setcounter{prf}{0}
- \renewcommand{\theprf}{\arabic{section}.\arabic{prf}}
- \newenvironment{prf}[2][]{%
- \refstepcounter{prf}%
- \ifstrempty{#1}%
- {\mdfsetup{%
- frametitle={%
- \tikz[baseline=(current bounding box.east),outer sep=0pt]
- \node[anchor=east,rectangle,fill=red!20]
- {\strut Proof~\theprf};}}
- }%
- {\mdfsetup{%
- frametitle={%
- \tikz[baseline=(current bounding box.east),outer sep=0pt]
- \node[anchor=east,rectangle,fill=red!20]
- {\strut Proof~\thetheo:~#1};}}%
- }%
- \mdfsetup{innertopmargin=10pt,linecolor=red!20,%
- linewidth=2pt,topline=true,%
- frametitleaboveskip=\dimexpr-\ht\strutbox\relax
- }
- \begin{mdframed}[]\relax%
- \label{#2}}{\qed\end{mdframed}}
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %Examples
- \begin{document}
- \section{Theorem and lemma examples with title}
- \begin{theo}[Pythagoras' theorem]{thm:pythagoras}
- In a right triangle, the square of the hypotenuse is equal to the sum of the squares of the catheti.
- $a^2+b^2=c^2$
- \end{theo}
- In mathematics, the Pythagorean theorem, also known as Pythagoras' theorem (see theorem \ref{thm:pythagoras}), is a relation in Euclidean geometry among the three sides of a right triangle.
- \begin{lem}[B\'ezout's identity]{lem:bezout}
- Let $a$ and $b$ be nonzero integers and let $d$ be their greatest common divisor. Then there exist integers $x$ and $y$ such that:
- $ax+by=d$
- \end{lem}
- This is a reference to Bezout's lemma \ref{lem:bezout}
- \section{Theorem and proof examples without title}
- \begin{theo}{thm:theorem1}
- There exist two irrational numbers $x$, $y$ such that $x^y$ is rational.
- \end{theo}
- \begin{prf}{prf:proof1}
- If $x=y=\sqrt{2}$ is an example, then we are done; otherwise $\sqrt{2}^{\sqrt{2}}$ is irrational, in which case taking $x=\sqrt{2}^{\sqrt{2}}$ and $y=\sqrt{2}$ gives us:
- $\bigg(\sqrt{2}^{\sqrt{2}}\bigg)^{\sqrt{2}}=\sqrt{2}^{\sqrt{2}\sqrt{2}}=\sqrt{2}^{2}=2.$
- \end{prf}
- \end{document}