# Customize the caption with a background

Hi there, i have some problems with modifying a caption which belongs to a listing.

The current result is shown in picture.

What i would like to change, can be seen in the second picture.

As you can hopefully see, I'd like to place Listing 1: Nicht implementierte Funktion at the left margin. Furthermore I'd like to write the caption in a colorbox. (in the second picture the caption Listing 1: Nicht implementierte Funktion has also a grey background) This colorbox should have the size from the left to the right margin (same as the listing)

My code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{color}
\definecolor{bg}{rgb}{0.85,0.85,0.85}
\usepackage[most]{tcolorbox}

\tcbset{
frame code={}
center title,
left=0pt,
right=0pt,
top=0pt,
bottom=0pt,
colback=gray!70,
colframe=white,
width=\dimexpr\textwidth\relax,
enlarge left by=0mm,
boxsep=0pt,
arc=0pt,outer arc=0pt,
}

\usepackage[newfloat]{minted}
\usepackage{caption}

\newenvironment{code}{\captionsetup{type=listing}}{}

\begin{document}
\begin{code}
\captionof{listing}{Nicht implementierte Funktion}
\label{code:comp area listing}
\begin{minted}[linenos, bgcolor=bg]{python}
def komplizierte_funktion(a, b):
raise NotImplementedError('Noch nicht implementiert.')
\end{minted}
\end{code}
\end{document}


I'd probably define a new tcblisting. You are using tcolorbox and minted already. You can combine their powers and of course still add labels and reference them:

\documentclass{article}
\usepackage[minted,most]{tcolorbox}
\definecolor{bg}{rgb}{0.85,0.85,0.85}

% blend into = listings' can only be used after begin document

\begin{document}

\newtcblisting[blend into = listings]{code}[1][]{
listing engine = minted ,
listing only ,
minted language = python ,
minted options = { linenos } ,
colback = bg ,
coltitle = black ,
colframe = gray!70 ,
fonttitle = \large ,
left = 6mm,
lefttitle = 0mm,
leftrule = 0pt,
rightrule = 0pt,
bottomrule = 0pt,
arc = 0pt,
#1
}

\begin{code}[title=Nicht implementierte Funktion,label=code:comp area listing]
def komplizierte_funktion(a, b):
raise NotImplementedError('Noch nicht implementiert.')
\end{code}

See Listing~\ref{code:comp area listing} as an example for\dots

\end{document}


• Super! Thanks a lot! Is it possible to change the size of the title text Nicht implementierte Funktion? – Domi1908 Dec 22 '20 at 14:20
• @Domi1908 yes, sure. I modified the example adding an example for fonttitle and also added a label and a reference – cgnieder Dec 22 '20 at 15:31

Have a look to my proposal:

\documentclass[a4paper,oneside]{article}
\usepackage{a4wide}
\usepackage[icelandic]{babel}
\usepackage{minted}

\usemintedstyle{perldoc}
\setlength\partopsep{-\topsep}

\usepackage{caption,newfloat}
\usepackage{xcolor}
\definecolor{bg}{rgb}{0.85,0.85,0.85}

\DeclareFloatingEnvironment[fileext=lol]{lstlisting}[Listing][List of Listings]
\captionsetup[lstlisting]{box=colorbox,boxcolor=gray, justification=raggedright,singlelinecheck=false, labelfont=bf, font=it}

\begin{document}
\begin{lstlisting}
\caption{This is the title I want for my code}
\vspace{-0.65cm}
\begin{minted}[linenos, bgcolor=bg]{python}
def komplizierte_funktion(a, b):
raise NotImplementedError('Noch nicht implementiert.')
\end{minted}
\end{lstlisting}
\end{document}


• This looks great. Is it possible also to write the line numbers INSIDE the box? – Domi1908 Dec 22 '20 at 14:04

Just add more options to \captionsetup: justification= to specify justification. singlelinecheck=0 as without it the caption is re-centered afterwards.

\newenvironment{code}{\captionsetup{type=listing,justification=RaggedRight,singlelinecheck=0}}{}