我偶然发现了一个网站,它提供了漂亮的方框示例,用于在 LaTeX 中构建定理,并附带扩展生物体而不是 TikZ。
代码如下:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{boiboites}
\newboxedtheorem[boxcolor=orange, background=blue!5, titlebackground=blue!20,
titleboxcolor = black]{theo}{Théorème}{test}
\begin{document}
\begin{theo}[Loi des grands nombres]
Soit $(X_n)_{n\in \mathbb{N}}$ une suite de variables aléatoires réelles
indépendantes identiquement distribuées telles que $X_1 \in L^1$. Alors :
$$\frac{1}{n} \sum_{i=1}^n X_i \overset{\textnormal{p.s.}}{\longrightarrow}
\mathbb{E} (X_1) .$$
\end{theo}
\end{document}
如果有人能用 TikZ 代码制作出独立于定理、命题的计数器,请帮忙。此外,如果可能的话,使用不同的样式和颜色,例如定理的样式不同于命题或引理。
答案1
下面我展示了两种可能性;在这两种情况下我都定义了两个结构,一个用于定理,另一个用于引理(在颜色和头部的位置上有一些变化);其他结构的机制应该很清楚(代码包含一些注释)。
使用
mdframed
包及其framemethod=tikz
选项(因此使用 TikZ):\documentclass{book} \usepackage[utf8]{inputenc} \usepackage[framemethod=tikz]{mdframed} \usetikzlibrary{calc} \usepackage{chngcntr} \usepackage{lipsum} % counters \newcounter{theorem} \newcounter{lemma} \counterwithin{theorem}{chapter} \counterwithin{lemma}{chapter} % names for the structures \newcommand\theoname{Théorème} \newcommand\lemmname{Lemme} \makeatletter % mdf key for the eventual notes in the structures \def\mdf@@mynote{} \define@key{mdf}{mynote}{\def\mdf@@mynote{#1}} % style for theorems \mdfdefinestyle{mytheo}{ settings={\refstepcounter{theorem}}, linewidth=1pt, innertopmargin=1.5\baselineskip, roundcorner=10pt, backgroundcolor=blue!05, linecolor=orange, singleextra={ \node[xshift=10pt,thick,draw=blue,fill=blue!20,rounded corners,anchor=west] at (P-|O) % {\strut{\bfseries\theoname~\thetheorem}\ifdefempty{\mdf@@mynote}{}{~(\mdf@@mynote)}}; }, firstextra={ \node[xshift=10pt,thick,draw=blue,fill=blue!20,rounded corners,anchor=west] at (P-|O) % {\strut{\bfseries\theoname~\thetheorem}\ifdefempty{\mdf@@mynote}{}{~(\mdf@@mynote)}}; } } % style for lemmas \mdfdefinestyle{mylemm}{ settings={\refstepcounter{lemma}}, linewidth=1pt, innertopmargin=1.5\baselineskip, roundcorner=10pt, backgroundcolor=red!05, linecolor=red!70!black, singleextra={ \path let \p1=(P), \p2=(O) in node[thick,draw=green!40!black,fill=green!20,rounded corners] at (P-|0.5*\x2+0.5*\x1,0) % {\strut{\bfseries\lemmname~\thelemma}\ifdefempty{\mdf@@mynote}{}{~(\mdf@@mynote)}}; }, firstextra={ \path let \p1=(P), \p2=(O) in node[thick,draw=green!40!black,fill=green!20,rounded corners] at (P-|0.5*\x2+0.5*\x1,0) % {\strut{\bfseries\lemmname~\thelemma}\ifdefempty{\mdf@@mynote}{}{~(\mdf@@mynote)}}; } } % some auxiliary environments \newmdenv[style=mytheo]{theor} \newmdenv[style=mylemm]{lemm} % the actual environments \newenvironment{theorem}[1][] {\begin{theor}[mynote=#1]} {\end{theor}} \newenvironment{lemma}[1][] {\begin{lemm}[mynote=#1]} {\end{lemm}} \makeatother \begin{document} \chapter{Test chapter} \begin{theorem} \lipsum[4] \end{theorem} \begin{lemma}[Lemme de Zorn] \lipsum[4] \end{lemma} \begin{theorem}[Loi des grands nombres] \lipsum[4] \end{theorem} \end{document}
使用
tcolorbox
:\documentclass{book} \usepackage[utf8]{inputenc} \usepackage[most]{tcolorbox} \usepackage{chngcntr} \usepackage{lipsum} % counters \newcounter{theorem} \newcounter{lemma} \counterwithin{theorem}{chapter} \counterwithin{lemma}{chapter} % names for the structures \newcommand\theoname{Théorème} \newcommand\lemmname{Lemme} \makeatletter % environment for theorems \newtcolorbox{theorem}[1][]{ breakable, enhanced, colback=blue!05, colframe=orange, top=\baselineskip, enlarge top by=\topsep, overlay unbroken and first={ \node[xshift=10pt,thick,draw=blue,fill=blue!20,rounded corners,anchor=west] at (frame.north west) % {\refstepcounter{theorem}\strut{\bfseries\theoname~\thetheorem}\if#1\@empty\relax\relax\else~(#1)\fi}; } } % environment for lemas \newtcolorbox{lemma}[1][]{ breakable, enhanced, colback=red!05, colframe=red!70!black, top=\baselineskip, enlarge top by=\topsep, overlay unbroken and first={ \node[thick,draw=green!40!black,fill=green!20,rounded corners] at (frame.north) % {\refstepcounter{lemma}\strut{\bfseries\lemmname~\thelemma}\if#1\@empty\relax\relax\else~(#1)\fi}; } } \makeatother \begin{document} \chapter{Test chapter} \begin{theorem} \lipsum[4] \end{theorem} \begin{lemma}[Lemme de Zorn] \lipsum[4] \end{lemma} \begin{theorem}[Loi des grands nombres] \lipsum[4] \end{theorem} \end{document}
我个人认为使用独立计数器并不是最好的选择(至少对于读者来说);我会对结构使用相同的计数器。
答案2
也许您应该花点时间阅读找到样式文件的网页上提供的帮助;)
http://alexisfles.ch/en/latex/boiboites.html
http://snouffy.free.fr/blog-en/index.php/post/2010/01/30/Nice-boxes-for-your-theorems-with-tikz
如果你想要一个定义环境和一个定理环境,并且使用不同的计数器和颜色,你可以简单地这样写:
\newboxedtheorem[thcounter=section,boxcolor=orange, background=blue!5, titlebackground=blue!20,titleboxcolor = black, thcounter=section]{theorem}{Theorem}{somecounter}
\newboxedtheorem[thcounter=section,boxcolor=blue, background=green!5, titlebackground=purple!20,titleboxcolor = yellow, hcounter=section]{definition}{Definition}{anothercounter}
当然,你不能调整所有内容:例如,你不能将标题放在框的中间,就像 Gonzalo Medina 在他的回答中所做的那样,但你仍然可以做你要求的事情!
我可以补充一下,它已经在 TikZ 中了。
如果你想要其他设计的盒子,你可以查看texample.net这个包装的灵感来源于此。