在一本包含图表、表格和列表的书中,我发现所有列表以及一些表格和图表在 tcolorbox 中呈现时看起来会更好。这本书有常见的目录:
\tableofcontents{}
\listoffigures{}
\listoftables{}
\lstlistoflistings{}
但是 tcolorboxes 有自己的标题(并且可以定义自己的计数器),因此为图形、表格和列表添加通常的标题和标签似乎是多余的(并且没有吸引力)。
所以我的问题是,当
- tcolorbox 是专门用来做的吗?
- 有时会使用 tcolorbox,而在其他情况下则使用普通列表包和常规表格/图形环境?
编辑:这是一个 MWE(改编自 TFSturm),其中包含图形、表格和列表以及带有标签和计数器的 tcolorbox 定理和列表。如何让它们出现在文件开头的 ToC 部分中?
\documentclass{article}
\usepackage{tikz}
\usepackage{here}
\usepackage[listings,theorems]{tcolorbox}
\tcbset{noparskip}
\begin{document}
\tableofcontents{}
\listoffigures{}
\listoftables{}
\lstlistoflistings{}
\pagebreak{}
%----------------------------------------------------------
\section{Colored boxes}
\begin{tcolorbox}[colback=red!5,colframe=red!75!black]
My box.
\end{tcolorbox}
\begin{tcolorbox}[colback=blue!5,colframe=blue!75!black,title=My title]
My box with my title.
\end{tcolorbox}
%----------------------------------------------------------
\section{\LaTeX-Examples}
\begin{tcblisting}{colback=red!5,colframe=red!75!black}
This is a \LaTeX\ example:
$\displaystyle\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}$.
\end{tcblisting}
%----------------------------------------------------------
\section{Theorems}
\newcounter{mytheorem}[section]
\def\themytheorem{\thesection.\arabic{mytheorem}}
\tcbmaketheorem{theo}{Theorem}{fonttitle=\bfseries\upshape, fontupper=\slshape,
arc=0mm, colback=blue!5,colframe=blue!75!black}{mytheorem}{theorem}
\begin{theo}{Summation of Numbers}{summation}
For all natural number $n$ it holds:\\[2mm]
$\displaystyle\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}$.
\end{theo}
We have given Theorem \ref{theorem:summation} on page \pageref{theorem:summation}.
\pagebreak{}
%----------------------------------------------------------
\section{Tables, Listings and Figures}
%----------------------------------------------------------
This is table 1
%----------------------------------------------------------
\begin{table}[H]
\centering
\begin{tabular}{ll}
Header1 & Header2 \\ \hline
Cell1 & Cell2 \\
\end{tabular}
\caption{My Table No1}
\label{tab:tab-1}
\end{table}
%----------------------------------------------------------
This is table 2
\begin{table}[H]
\centering
\begin{tabular}{ll}
Header3 & Header4 \\ \hline
Cell3 & Cell4 \\
\end{tabular}
\caption{My Table No2}
\label{tab:tab-1}
\end{table}
%----------------------------------------------------------
This is figure 1
%----------------------------------------------------------
\begin{figure}[H]
\centering
\begin{tikzpicture}
\draw (0,0) node {A} --
(2,0) node {B};
\end{tikzpicture}
\caption{Figure No1}
\label{fig:fig-1}
\end{figure}
%----------------------------------------------------------
This is figure 2
%----------------------------------------------------------
\begin{figure}[H]
\centering
\begin{tikzpicture}
\draw (0,0) node {C} --
(2,0) node {D};
\end{tikzpicture}
\caption{Figure No2}
\label{fig:fig-2}
\end{figure}
\begin{lstlisting}[caption={Java Logic 1},label=java1]
public class Main
{
public Main(String args[])
{
// Java logic in listing 1
}
}
\end{lstlisting}
%\label{code:Listing-1}
\begin{lstlisting}[caption={Java Logic 2},label=java2]
public class Main
{
public Main(String args[])
{
// More Java logic in listing 2
}
}
\end{lstlisting}
%\label{code:Listing-2}
\end{document}
输出如下:
答案1
在下面的解决方案中,我使用\@starttoc
命令来启动文件\jobname.thm
% make a List of Theorems using the
% \@starttoc command which creates \jobname.thm
\newcommand\listtheoremname{List of Theorems}
\newcommand\listoftheorems{%
\section*{\listtheoremname}\@starttoc{thm}}
theorem
然后从中复制命令的定义tcbtheorems.code.tex
并添加
\addcontentsline{thm}{subsection}{\protect\numberline{\csname the#2\endcsname} #3}, % new bit
将定理编号和描述添加到\jobname.thm
% copied from tcbtheorems.code.tex and added
% \addcontentsline...
\tcbset{
theorem/.code args={#1#2#3#4}{%
\refstepcounter{#2}\label{#4}%
\addcontentsline{thm}{subsection}{\protect\numberline{\csname the#2\endcsname} #3}, % new bit
\pgfkeysalso{title={\setlength{\hangindent}{\widthof{#1~\csname the#2\endcsname:\ }}\hangafter=1 \mbox{#1~\csname the#2\endcsname:\ }#3}}}%
}
我还创建了一个addtolol
代表add to list of listings
% add tcblistings to \jobname.lol (list of listings)
\tcbset{
addtolol/.code={\addcontentsline{lol}{subsection}{\kvtcb@title}},
}
可用作任何tcolorbox
和朋友环境中的密钥
\begin{tcolorbox}[colback=blue!5,colframe=blue!75!black,title=My title,addtolol]
我的实现中有一个问题是addtolol
关键必须放在title
键之后;如果有人对如何修复此问题有建议,请告诉我。
以下是该解决方案的屏幕截图。
完成 MWE
\documentclass{article}
\usepackage{tikz}
\usepackage{float} % here.sty is considered obsolete (according to documentation)
\usepackage[listings,theorems]{tcolorbox}
\tcbset{noparskip}
\makeatletter
% make a List of Theorems using the
% \@starttoc command which creates \jobname.thm
\newcommand\listtheoremname{List of Theorems}
\newcommand\listoftheorems{%
\section*{\listtheoremname}\@starttoc{thm}}
% copied from tcbtheorems.code.tex and added
% \addcontentsline...
\tcbset{
theorem/.code args={#1#2#3#4}{%
\refstepcounter{#2}\label{#4}%
\addcontentsline{thm}{subsection}{\protect\numberline{\csname the#2\endcsname} #3}, % new bit
\pgfkeysalso{title={\setlength{\hangindent}{\widthof{#1~\csname the#2\endcsname:\ }}\hangafter=1 \mbox{#1~\csname the#2\endcsname:\ }#3}}}%
}
% add tcblistings to \jobname.lol (list of listings)
\tcbset{
addtolol/.code={\addcontentsline{lol}{subsection}{\kvtcb@title}},
}
\makeatother
% moved new theorem declaration to the preamble
\newcounter{mytheorem}[section]
\def\themytheorem{\thesection.\arabic{mytheorem}}
\tcbmaketheorem{theo}{Theorem}{fonttitle=\bfseries\upshape, fontupper=\slshape,
arc=0mm, colback=blue!5,colframe=blue!75!black}{mytheorem}{theorem}
\begin{document}
\tableofcontents{}
\listoffigures{}
\listoftables{}
\lstlistoflistings{}
\listoftheorems % new bit
\pagebreak{}
%----------------------------------------------------------
\section{Colored boxes}
\begin{tcolorbox}[colback=red!5,colframe=red!75!black,title=My title]
My box.
\end{tcolorbox}
\begin{tcolorbox}[colback=blue!5,colframe=blue!75!black,title=My title,addtolol]
My box with my title.
\end{tcolorbox}
%----------------------------------------------------------
\section{\LaTeX-Examples}
\begin{tcblisting}{colback=red!5,colframe=red!75!black,title=Some title,addtolol}
This is a \LaTeX\ example:
$\displaystyle\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}$.
\end{tcblisting}
%----------------------------------------------------------
\section{Theorems}
\begin{theo}{Summation of Numbers}{summation}
For all natural number $n$ it holds:\\[2mm]
$\displaystyle\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}$.
\end{theo}
We have given Theorem \ref{theorem:summation} on page \pageref{theorem:summation}.
\pagebreak{}
%----------------------------------------------------------
\section{Tables, Listings and Figures}
%----------------------------------------------------------
This is table 1
%----------------------------------------------------------
\begin{table}[H]
\centering
\begin{tabular}{ll}
Header1 & Header2 \\ \hline
Cell1 & Cell2 \\
\end{tabular}
\caption{My Table No1}
\label{tab:tab-1}
\end{table}
%----------------------------------------------------------
This is table 2
\begin{table}[H]
\centering
\begin{tabular}{ll}
Header3 & Header4 \\ \hline
Cell3 & Cell4 \\
\end{tabular}
\caption{My Table No2}
\end{table}
%----------------------------------------------------------
This is figure 1
%----------------------------------------------------------
\begin{figure}[H]
\centering
\begin{tikzpicture}
\draw (0,0) node {A} --
(2,0) node {B};
\end{tikzpicture}
\caption{Figure No1}
\label{fig:fig-1}
\end{figure}
%----------------------------------------------------------
This is figure 2
%----------------------------------------------------------
\begin{figure}[H]
\centering
\begin{tikzpicture}
\draw (0,0) node {C} --
(2,0) node {D};
\end{tikzpicture}
\caption{Figure No2}
\label{fig:fig-2}
\end{figure}
\begin{lstlisting}[caption={Java Logic 1},label=java1]
public class Main
{
public Main(String args[])
{
// Java logic in listing 1
}
}
\end{lstlisting}
%\label{code:Listing-1}
\begin{lstlisting}[caption={Java Logic 2},label=java2]
public class Main
{
public Main(String args[])
{
// More Java logic in listing 2
}
}
\end{lstlisting}
%\label{code:Listing-2}
\end{document}
答案2
自 2.40 版tcolorbox
(2013/07/15)以来,集成了对框列表、列表等的支持。我采用了 cmhughes 的优秀答案并将其更新为最新版本。我的建议是保持 cmhughes 的答案不变,因为它是 2.40 之前版本的正确答案,可能会存在很长时间。其次,如果人们想要或需要,比较这两个答案可能有助于将旧代码适应新功能。因此,我试图保持与原始答案非常接近;下面的所有代码\pagebreak{}
都没有改变。
\documentclass{article}
\usepackage{tikz}
\usepackage{float}
\usepackage[listings,theorems]{tcolorbox}
\makeatletter
% add tcblistings to \jobname.lol (list of listings)
\tcbset{
addtolol/.style={list entry={\kvtcb@title},add to list={lol}{subsection}},
}
\makeatother
\newtcbtheorem[auto counter,number within=section,list inside=thm]{theo}{Theorem}%
{fonttitle=\bfseries\upshape, fontupper=\slshape,
arc=0mm, colback=blue!5,colframe=blue!75!black}{theorem}
\begin{document}
\tableofcontents{}
\listoffigures{}
\listoftables{}
\lstlistoflistings{}
\tcblistof[\section*]{thm}{List of Theorems}
\pagebreak{}
%----------------------------------------------------------
\section{Colored boxes}
\begin{tcolorbox}[colback=red!5,colframe=red!75!black,title=My title]
My box.
\end{tcolorbox}
\begin{tcolorbox}[colback=blue!5,colframe=blue!75!black,title=My title,addtolol]
My box with my title.
\end{tcolorbox}
%----------------------------------------------------------
\section{\LaTeX-Examples}
\begin{tcblisting}{colback=red!5,colframe=red!75!black,title=Some title,addtolol}
This is a \LaTeX\ example:
$\displaystyle\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}$.
\end{tcblisting}
%----------------------------------------------------------
\section{Theorems}
\begin{theo}{Summation of Numbers}{summation}
For all natural number $n$ it holds:\\[2mm]
$\displaystyle\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}$.
\end{theo}
We have given Theorem \ref{theorem:summation} on page \pageref{theorem:summation}.
\pagebreak{}
%----------------------------------------------------------
\section{Tables, Listings and Figures}
%----------------------------------------------------------
This is table 1
%----------------------------------------------------------
\begin{table}[H]
\centering
\begin{tabular}{ll}
Header1 & Header2 \\ \hline
Cell1 & Cell2 \\
\end{tabular}
\caption{My Table No1}
\label{tab:tab-1}
\end{table}
%----------------------------------------------------------
This is table 2
\begin{table}[H]
\centering
\begin{tabular}{ll}
Header3 & Header4 \\ \hline
Cell3 & Cell4 \\
\end{tabular}
\caption{My Table No2}
\end{table}
%----------------------------------------------------------
This is figure 1
%----------------------------------------------------------
\begin{figure}[H]
\centering
\begin{tikzpicture}
\draw (0,0) node {A} --
(2,0) node {B};
\end{tikzpicture}
\caption{Figure No1}
\label{fig:fig-1}
\end{figure}
%----------------------------------------------------------
This is figure 2
%----------------------------------------------------------
\begin{figure}[H]
\centering
\begin{tikzpicture}
\draw (0,0) node {C} --
(2,0) node {D};
\end{tikzpicture}
\caption{Figure No2}
\label{fig:fig-2}
\end{figure}
\begin{lstlisting}[caption={Java Logic 1},label=java1]
public class Main
{
public Main(String args[])
{
// Java logic in listing 1
}
}
\end{lstlisting}
%\label{code:Listing-1}
\begin{lstlisting}[caption={Java Logic 2},label=java2]
public class Main
{
public Main(String args[])
{
// More Java logic in listing 2
}
}
\end{lstlisting}
%\label{code:Listing-2}
\end{document}