在另一篇文章中,beamer 样式文本框,我看到了一个盒子,我有兴趣制作它。但是,我想在书中使用它,而且我想前要写的标题:定义(或示例),然后是章节编号,最后是标题。
例如:定义 2.1:好(当我在第二章时,我在这里给出了好的第一个定义)
代码是啥?
另外我们如何在目录后添加定义列表?
答案1
新的定义列表是使用 LaTeX 的内核创建的\@starttoc
,其行为方式与标准 LoF 和 LoT 相同,但使用扩展名为 的文件.ldf
。框使用 定义mdframed
:
\documentclass{book}
\usepackage{xcolor}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{chngcntr}
\usepackage{lipsum}
\newcounter{exa}[chapter]
\counterwithin{exa}{chapter}
\makeatletter
\newcommand\listdefinitionname{List of Definitions}
\newcommand\listofdefinitions{%
\if@twocolumn
\@restonecoltrue\onecolumn
\else
\@restonecolfalse
\fi
\chapter*{\listdefinitionname}%
\@mkboth{\MakeUppercase\listdefinitionname}%
{\MakeUppercase\listdefinitionname}%
\@starttoc{ldf}%
\if@restonecol\twocolumn\fi
}
\makeatother
\mdfdefinestyle{mystyle}{%
linecolor=green!40!black,
outerlinewidth=1pt,%
frametitlerule=true,
frametitlefont=\sffamily\bfseries\color{white},%
frametitlerulewidth=1pt,frametitlerulecolor=green!40!black,%
frametitlebackgroundcolor=green!40!black,
backgroundcolor=green!5,
innertopmargin=\topskip,
roundcorner=5pt
}
\newmdenv[style=mystyle]{exa}
\newenvironment{example}[1]
{\stepcounter{exa}%
\addcontentsline{ldf}{figure}{#1}%
\begin{exa}[frametitle=Definition~\theexa: #1]}
{\end{exa}}
\begin{document}
\listofdefinitions
\chapter{Test Chapter}
\begin{example}{The Title}
\lipsum[1]
\end{example}\par\bigskip
\begin{example}{Another Title}
\lipsum[1]
\end{example}
\end{document}
新定义列表的图片:
框架定义的外观图像:
样式和设置可以重复使用来定义类似的环境和列表:
\documentclass{book}
\usepackage{xcolor}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{chngcntr}
\usepackage{lipsum}
\newcounter{exa}[chapter]
\newcounter{defi}[chapter]
\counterwithin{exa}{chapter}
\counterwithin{defi}{chapter}
\makeatletter
\newcommand\listdefinitionname{List of Definitions}
\newcommand\listofdefinitions{%
\if@twocolumn
\@restonecoltrue\onecolumn
\else
\@restonecolfalse
\fi
\chapter*{\listdefinitionname}%
\@mkboth{\MakeUppercase\listdefinitionname}%
{\MakeUppercase\listdefinitionname}%
\@starttoc{ldf}%
\if@restonecol\twocolumn\fi
}
\newcommand\listexamplename{List of Definitions}
\newcommand\listofexamples{%
\if@twocolumn
\@restonecoltrue\onecolumn
\else
\@restonecolfalse
\fi
\chapter*{\listexamplename}%
\@mkboth{\MakeUppercase\listexamplename}%
{\MakeUppercase\listexamplename}%
\@starttoc{lex}%
\if@restonecol\twocolumn\fi
}
\makeatother
\mdfdefinestyle{mystyle}{%
linecolor=green!40!black,
outerlinewidth=1pt,%
frametitlerule=true,
frametitlefont=\sffamily\bfseries\color{white},%
frametitlerulewidth=1pt,frametitlerulecolor=green!40!black,%
frametitlebackgroundcolor=green!40!black,
backgroundcolor=green!5,
innertopmargin=\topskip,
roundcorner=5pt
}
\newmdenv[style=mystyle]{exa}
\newmdenv[style=mystyle]{defi}
\newenvironment{example}[1]
{\stepcounter{exa}%
\addcontentsline{lex}{figure}{#1}%
\begin{exa}[frametitle=Example~\theexa: #1]}
{\end{exa}}
\newenvironment{definition}[1]
{\stepcounter{defi}%
\addcontentsline{ldf}{figure}{#1}%
\begin{defi}[frametitle=Example~\thedefi: #1]}
{\end{defi}}
\begin{document}
\listofdefinitions
\listofexamples
\chapter{Test Chapter}
\begin{example}{The title of an example}
\lipsum[1]
\end{example}\par\bigskip
\begin{definition}{The title of a definition}
\lipsum[1]
\end{definition}
\end{document}
答案2
解决方案tcolorbox
(利用使用 tocloft 格式化新的定义列表对于新目录):
\documentclass{book}
\usepackage{tcolorbox}
\usepackage{lipsum}
\usepackage{tocloft}
\usepackage{etoolbox}
\newcounter{good}
\setcounter{good}{0}
\newcommand{\listgoodname}{List of Goods}
\newlistof{goods}{good}{\listgoodname}
\newenvironment{good}[1]{
\stepcounter{good}
\addcontentsline{good}{figure}
{\protect\numberline{\thechapter.\thegood}#1}\par%
\begin{tcolorbox}[colback=green!5,colframe=green!40!black,title={Definition \thechapter.\thegood\space #1}]
}{\end{tcolorbox}}
\makeatletter
\preto\chapter{\addtocontents{good}{\protect\addvspace{10\p@}}}%
\makeatother
\begin{document}
\tableofcontents
\listofgoods
\chapter{One}
\chapter{Two}
\section{First}
\begin{good}{A nice heading}
\lipsum[2]
\end{good}
\begin{good}{Good}
\lipsum[2]
\end{good}
\begin{good}{Good again}
\end{good}
\end{document}
结果(索引):
和盒子: