How to create boxed, captioned listing?

For a quick reference guide in which I want to include many small listings, each with a caption, How do I group them together, and prevent the code from being broken up? I'm sure there is some windows/orphan control, but one issue is, if code is too long, then there is no way of not breaking it up, so ideally there should be some logic to prevent code breakup (and certainly keep the caption with the code) while allowing it if the code is too big.

The following code is based on the solution suggested by Christian Hupfer. The look is beautiful, but it displays a title Listing0 and listing1, and in the proposed solution the (subtitle?) comes out on top while in mine it comes out on the bottom. I would like to eliminate the subtitle and set the title to the desired text, but I do not see how to do this.

Here is what it looks like:

\documentclass[12pt]{article}
\usepackage{listings}
\usepackage[letterpaper, portrait, margin=0.8in]{geometry}
\usepackage[usenames,dvipsnames]{color}
\title{\bf Test Title }
\definecolor{lgray}{rgb}{0.9,0.9,0.9}

\usepackage[most]{tcolorbox}

\lstset{
backgroundcolor=\color{lgray},
%   basicstyle=\footnotesize \ttfamily \color{black} \bfseries,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
deletekeywords={...},
escapeinside={\%*}{*)},
frame=single,
keywordstyle=\textbf,
morekeywords={BRIEFDescriptorConfig,string,TiXmlNode,DetectorDescriptorConfigContainer,istringstream,cerr,exit},
identifierstyle=\color{black},
stringstyle=\color{blue},
language=Java,
numbers=right,
numbersep=5pt,
numberstyle=\tiny\color{black},
rulecolor=\color{black},
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=1,
tabsize=5,
title=\lstname,
}

\newtcblisting{mycode}[2][]{%
listing options={language={Java}},
listing only,listing remove caption=false,
breakable,
colback=yellow!20!white,
coltitle=black,
colbacktitle=white!60!black,
title={\lstlistingname~#2},
title after break={\raggedleft\lstlistingname\ \thelstlisting~ -- continued},
arc = 0pt, auto outer arc, #1
}

\newcommand{\java}{\begin{lstlisting}[language=Java]}
\usepackage{multicol}
\begin{document}
\begin{multicols}{2}

\begin{tcblisting}{listing options={language={Java},caption={Draws one red triangle}},
listing only,listing remove caption=false,
breakable,
colback=yellow!20!white,
coltitle=black,
colbacktitle=white!60!brown,
title={\lstlistingname\ \thelstlisting},
title after break={\raggedleft\lstlistingname\ \thelstlisting~ -- continued},
arc = 0pt, auto outer arc
}
void setup() {
size(600, 400, OPENGL);
}

void draw() {
background(0);
translate(width/2, height/2);
fill(255,0,0);
beginShape();
vertex(-200,200);
vertex(200,200);
vertex(0, -200);
endShape();
}
\end{tcblisting}

%\lstinputlisting[language=java,caption=A third java listing]{openglprocessing/test.java}
\end{multicols}

\end{document}

• Perhaps you should try first: How to create a minimal working example (MWE) ;-) – user31729 Jun 7 '15 at 18:04
• Maybe a boxed and captioned MWE? That would be killing two birds with a stone :o) – Bernard Jun 7 '15 at 18:26
• You'll find examples of this in the tcolorbox package, for example. – cmhughes Jun 7 '15 at 19:34
• The problem with these suggestions is you are assuming that I know what to do. This question is asking what packages are best to use. I searched and found caption options for listing, and perhaps that is the best one. But I don't know, because there are many packages in latex, and no obvious way to know which one is the most appropriate for the task. I will prepare an MWE using the listing package and you can critique/fix it. – Dov Jun 7 '15 at 20:45
• I'll look later on regarding your edit – user31729 Jun 11 '15 at 3:30

In my opinion, the easiest way is to use tcolorbox and its listings library, which is loaded with \usepackage[most]{tcolorbox}

Similar to the listings package it enables \newtcblistings -- user-defined listing environments which are massively configurable.

I shown two examples for this -- a direct usage of \begin{tcblisting} and \newtcblisting.

The important feature is the breakable option.

For a detailed described, especially for the styles and the possible tcbinputlisting I refer to the tcolorbox manual, chapter 13, of the current version 3.60

\documentclass{article}

\usepackage[most]{tcolorbox}

\newtcblisting{mycode}[2][]{%
listing options={language={C}},
listing only,listing remove caption=false,
breakable,
colback=yellow!20!white,
coltitle=black,
colbacktitle=white!60!black,
title={\lstlistingname~#2},
title after break={\raggedleft\lstlistingname\ \thelstlisting~ -- continued},
arc = 0pt, auto outer arc, #1
}

\begin{document}

\begin{tcblisting}{listing options={language={C},caption={The sum of integers}},
listing only,listing remove caption=false,
breakable,
colback=yellow!20!white,
coltitle=black,
colbacktitle=white!60!brown,
title={\lstlistingname\ \thelstlisting},
title after break={\raggedleft\lstlistingname\ \thelstlisting~ -- continued},
arc = 0pt, auto outer arc
}
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
long int k = 0;
long int sum = 0;

for ( k = 1 ; k <= 100 ; k++ )
{
sum += k;
printf("Current sum: %10d\n");
}

sum = 0;
/* Now the sum of squares */
for ( k = 1 ; k <= 10 ; k++ )
{
sum += (long int) pow(k,2);
printf("Current sum: %10d\n");
}

sum = 0;
/* Now the sum of cubes */
for ( k = 1 ; k <= 10 ; k++ )
{
sum += (long int) pow(k,3);
printf("Current sum: %10d\n");
}

/* And repeating the stuff to make the whole
stuff longer than necessary */

sum = 0;
for ( k = 1 ; k <= 100 ; k++ )
{
sum += k;
printf("Current sum: %10d\n");
}

sum = 0;
/* Now the sum of squares */
for ( k = 1 ; k <= 10 ; k++ )
{
sum += (long int) pow(k,2);
printf("Current sum: %10d\n");
}

sum = 0;
/* Now the sum of cubes */
for ( k = 1 ; k <= 10 ; k++ )
{
sum += (long int) pow(k,3);
printf("Current sum: %10d\n");
}
return(EXIT_SUCCESS);
}
\end{tcblisting}

\begin{mycode}[listing options={caption={Just a repeat}}]{Once again}
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
long int k = 0;
long int sum = 0;

for ( k = 1 ; k <= 100 ; k++ )
{
sum += k;
printf("Current sum: %10d\n");
}

sum = 0;
/* Now the sum of squares */
for ( k = 1 ; k <= 10 ; k++ )
{
sum += (long int) pow(k,2);
printf("Current sum: %10d\n");
}

sum = 0;
/* Now the sum of cubes */
for ( k = 1 ; k <= 10 ; k++ )
{
sum += (long int) pow(k,3);
printf("Current sum: %10d\n");
}

/* And repeating the stuff to make the whole
stuff longer than necessary */

sum = 0;
for ( k = 1 ; k <= 100 ; k++ )
{
sum += k;
printf("Current sum: %10d\n");
}

sum = 0;
/* Now the sum of squares */
for ( k = 1 ; k <= 10 ; k++ )
{
sum += (long int) pow(k,2);
printf("Current sum: %10d\n");
}

sum = 0;
/* Now the sum of cubes */
for ( k = 1 ; k <= 10 ; k++ )
{
sum += (long int) pow(k,3);
printf("Current sum: %10d\n");
}
return(EXIT_SUCCESS);
}
\end{mycode}

\end{document}


• It's gorgeous, and my thanks, but if ever there was proof that somone needs to reimagine publishing again, the notion that you need a 360 page manual for just a single component (tcblisting) and not even the only choice in that genre. The complexity is mind-boggling – Dov Jun 8 '15 at 12:02
• '@Dov: Nope, the tcblisting part is shorter. In fact, it's much shorter to cope with it ;-) – user31729 Jun 8 '15 at 13:01
• any idea about the two titles in the text box? – Dov Jun 9 '15 at 1:11
• @Dov: Which two titles? – user31729 Jun 9 '15 at 1:45