![为 tcolorbox \tcblisting 添加标题](https://linux22.com/image/332460/%E4%B8%BA%20tcolorbox%20%5Ctcblisting%20%E6%B7%BB%E5%8A%A0%E6%A0%87%E9%A2%98.png)
我对整个TeX
世界都很陌生,最终成功创建了自己的tcolorbox
代码框:
现在我想为我的代码框添加标题,就像常规列表一样。
它应该显示类似“代码片段 1:示例框”的内容以下tcolorbox
,类似这样的图:
我该怎么做?有办法吗\caption
?tcblisting
任何帮助都将不胜感激,我还添加了我的盒子的最小工作示例:
\documentclass[a4paper,11pt,twoside]{report}
\usepackage{ngerman}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[fleqn]{amsmath}
\usepackage[scaled=.95]{inconsolata}
\usepackage{tcolorbox}
\tcbuselibrary{listings, breakable, skins}
\definecolor{bg}{rgb}{0.85,0.85,0.85}
\renewcommand*\thelstnumber{\makebox[3em][r]{\ifnum\value{lstnumber}<10 0\fi\the\value{lstnumber}}}
\newtcblisting{ccode}
{colback=bg,
colframe=black!70,
enhanced,
listing only,
overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
rectangle ([xshift=5.1mm]frame.north west);\end{tcbclipinterior}},
listing options={numbers=left, numberstyle=\tiny, basicstyle=\small\ttfamily, xleftmargin=0.6em, language = c, aboveskip=\smallskipamount, belowskip=\smallskipamount}
}
\begin{document}
\begin{ccode}
#include <stdio.h>
int main()
{
stub;
}
\end{ccode}
\end{document}
答案1
更新
我认为不可能使用\caption
不进行大量重新定义的。这是一个带有 的版本blend into=listings
,必须在 中\AtBeginDocument
或 之后完成\begin{document}
。
然后将title=
用作长标题文本,短标题文本由list text=...
- 请参阅示例。
要将标题移至底部,必须分离标题并重新配置标题框。
\documentclass[a4paper,11pt,twoside]{report}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[fleqn]{amsmath}
\usepackage[scaled=.95]{inconsolata}
\usepackage{tcolorbox}
\tcbuselibrary{listings, breakable, skins}
\definecolor{bg}{rgb}{0.85,0.85,0.85}
\renewcommand*\thelstnumber{\makebox[3em][r]{\ifnum\value{lstnumber}<10 0\fi\the\value{lstnumber}}}
\AtBeginDocument{%
\newtcblisting[blend into=listings]{ccode}[1][]{%
colback=bg,
colframe=black!70,
enhanced,
listing only,
overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
rectangle ([xshift=5.1mm]frame.north west);\end{tcbclipinterior}},
listing remove caption=false,
listing options={numbers=left, numberstyle=\tiny,
basicstyle=\small\ttfamily,
xleftmargin=0.6em,
language = c,
aboveskip=\smallskipamount,
belowskip=\smallskipamount,
},
coltitle=black,
attach boxed title to bottom center={yshift=-10pt},
boxed title style={enhanced jigsaw, colback=white, sharp corners, boxrule=0pt},
#1
}
}
\begin{document}
\lstlistoflistings
\begin{ccode}[list text={C-Programm!},title={Ein primitives C - Programm!}]
#include <stdio.h>
int main()
{
stub;
}
\end{ccode}
\end{document}
tcolorbox
定义listing remove caption=true
,即使用它作为选项并提供标题参数,即\newtcblisting
用强制参数定义。
\documentclass[a4paper,11pt,twoside]{report}
%\usepackage{ngerman}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[fleqn]{amsmath}
\usepackage[scaled=.95]{inconsolata}
\usepackage{tcolorbox}
\tcbuselibrary{listings, breakable, skins}
\definecolor{bg}{rgb}{0.85,0.85,0.85}
\renewcommand*\thelstnumber{\makebox[3em][r]{\ifnum\value{lstnumber}<10 0\fi\the\value{lstnumber}}}
\newtcblisting{ccode}[2][]
{colback=bg,
colframe=black!70,
enhanced,
listing only,
overlay={\begin{tcbclipinterior}\fill[black!25] (frame.south west)
rectangle ([xshift=5.1mm]frame.north west);\end{tcbclipinterior}},
listing remove caption=false,
listing options={numbers=left, numberstyle=\tiny, basicstyle=\small\ttfamily, xleftmargin=0.6em, language = c, aboveskip=\smallskipamount, belowskip=\smallskipamount, caption={#2}},#1
}
\begin{document}
\begin{ccode}{Ein primitives C - Programm!}
#include <stdio.h>
int main()
{
stub;
}
\end{ccode}
\end{document}