8

I'm new to the whole TeX world and have finally managed creating my own tcolorbox code box:

sample box

Now I would like to caption my code boxes like regular listings.
It should say something like "Code-Snippet 1: Sample Box" below the tcolorbox, similar to this figure:

caption

How would I do this? Is there a way to \caption the tcblisting? Any help would by greatly appreciated and I've added a minimal working example of my box as well:

\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}
  • Do you want floating listings? – Ignasi Apr 20 '16 at 17:18
  • I'd prefer floating listings but as a beginner I have to tackle one problem at the time. If you can provide a floating and captioned solution you'd totally save my day. – Raito Apr 20 '16 at 17:20
  • ngerman is actually a little bit outdated. Use \usepackage[ngerman]{babel} rather – user31729 Apr 20 '16 at 17:22
  • 1
    have you tried \usepackage{caption} and then \captionof? – naphaneal Apr 20 '16 at 17:23
  • @naphaneal: That would not work, I fear. – user31729 Apr 20 '16 at 17:28
7

update

enter image description here

I don't think it's possible to use a \caption without much redefinitions. Here's a version with blend into=listings, which must be done in \AtBeginDocument or after \begin{document}.

The title= will then be used as the long caption text, the short one is given by list text=... -- See the example, please.

To move the caption to the bottom, the title has to be detached and the title box reconfigured.

\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 defines listing remove caption=true, i.e use this as an option and provide a caption argument, i.e. define the \newtcblisting with a mandatory argument`.

\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}

enter image description here

  • Thank you Christian, this has been helpful already. But with your example the caption is inside of the box and there seems no way to move it. I'd prefer having it below the box, so it looks just like usual listings and figures. I've updated my sample picture. – Raito Apr 20 '16 at 17:26
  • @Raito: That's why we want to have the clear question right from the beginning, not updates :-( Now, all my efforts are useless – user31729 Apr 20 '16 at 17:32
  • I stated that I wanted the caption below the tcolorbox right from the very beginning. I'm sorry if that wasn't clear enough - I added the better picture when I saw that you misunderstood me. :( Still learned from your example, so not useless at all. – Raito Apr 20 '16 at 17:38
  • @Raito: See the update, please – user31729 Apr 20 '16 at 17:49
  • @Raito: You're welcome -- and sorry about missing the below feature in the first trial – user31729 Apr 20 '16 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.