Important remark
According to the package author, as of version 2.71 of tcolorbox
, the code for listings and changed several internal commands. The user interface is unchanged but the example code below with the internal macros may not work anymore. In any case, from version 2.71 on, the commands below are no longer needed, since the missing options have been incorporated to the package.
In the follwing example code I defined four new options that allow to insert an existing image corresponding to some listing; the four options are
listing above image
image above listing
listing outside image
image outside listing
The first one places the code above the image; the second one, places the image above the code; the third and fourth ones, place the code side-by-side with the image (listing to the right, image to the left, and vice versa).
The \tcbuselistingimage
command is used to declare the image; the optional argument will be passed to the optional argument of \includegraphics
, and the mandatory argument receives the name of the image file.
The code:
\documentclass[dvipsnames]{article}
\usepackage{tcolorbox}
\usepackage{bera}
\tcbuselibrary{most}
\definecolor{mygray}{RGB}{238,238,238}
\definecolor{keywords}{HTML}{8A4A0B}
\definecolor{myblue}{RGB}{20,105,176}
\lstdefinestyle{mystyle}{language=[LaTeX]Tex,
basicstyle=\normalfont\ttfamily,
texcsstyle=*\color{keywords},
stepnumber=1,
showstringspaces=false,
breaklines=true,
moretexcs={tikz,node},
literate=
*{\{}{{\textcolor{myblue}{\{}}}{1}
{\}}{{\textcolor{myblue}{\}}}}{1}
{[}{{\textcolor{myblue}{[}}}{1}
{]}{{\textcolor{myblue}{]}}}{1}
}
\makeatletter
\newcommand\tcbuselistingimage[2][]{%
\def\@tcbuselistingimage{\includegraphics[#1]{#2}}}%
\def\tcb@inputlisting@out@listingAndText{%
\tcb@inputlisting@out@prepare%
\tcbdimto\tcb@w@outside{\tcb@w@lower}%
\tcbdimto\tcb@w@inside{\tcb@width-\tcb@w@lower-\kvtcb@sbs@gap}%
\begin{tcb@sbsfloat}\begin{minipage}{\tcb@width}%
\tcb@minipage{\tcb@w@inside}%
\begin{tcolorbox}[width=\tcb@w@inside,nobeforeafter,nofloat,sidebyside=false]\tcbuselistinglisting\end{tcolorbox}%
\endminipage\hfill%
\iftcb@lowervisible\tcb@minipage{\tcb@w@outside}\color{\kvtcb@collower}\kvtcb@fontlower\kvtcb@before@lower\ignorespaces%
\begingroup\tcb@layer@inc\tcbuselistingtext\tcb@layer@dec\endgroup%
\unskip\kvtcb@after@lower\endminipage\fi%
\end{minipage}\end{tcb@sbsfloat}%
}
\def\tcb@inputlisting@out@listingAboveImage{%
\begin{tcolorbox}[after=]\tcbuselistinglisting\end{tcolorbox}
\iftcb@lowervisible\par\vskip-\parskip\vspace*{\kvtcb@middle}\noindent%
\tcb@minipage{\kvtcb@width}\centering\color{\kvtcb@collower}\kvtcb@fontlower\kvtcb@before@lower\ignorespaces%
\begingroup\tcb@layer@inc\@tcbuselistingimage\tcb@layer@dec\endgroup%
\unskip\kvtcb@after@lower\endminipage\fi%
\kvtcb@afterbox%
}
\def\tcb@inputlisting@out@ImageAboveListing{%
\kvtcb@beforebox\noindent%
\iftcb@lowervisible\tcb@minipage{\kvtcb@width}\centering\color{\kvtcb@collower}\kvtcb@fontlower\kvtcb@before@lower\ignorespaces%
\begingroup\tcb@layer@inc\@tcbuselistingimage\tcb@layer@dec\endgroup%
\unskip\kvtcb@after@lower\endminipage%
\par\vskip-\parskip\vspace*{\kvtcb@middle}\noindent%
\fi%
\begin{tcolorbox}[before=]\tcbuselistinglisting\end{tcolorbox}%
}
\def\tcb@inputlisting@out@listingAndImage{%
\tcb@inputlisting@out@prepare%
\tcbdimto\tcb@w@outside{\tcb@w@lower}%
\tcbdimto\tcb@w@inside{\tcb@width-\tcb@w@lower-\kvtcb@sbs@gap}%
\begin{tcb@sbsfloat}\begin{minipage}{\tcb@width}%
\tcb@minipage{\tcb@w@inside}%
\begin{tcolorbox}[width=\tcb@w@inside,nobeforeafter,nofloat,sidebyside=false]\tcbuselistinglisting\end{tcolorbox}%
\endminipage\hfill%
\iftcb@lowervisible\tcb@minipage{\tcb@w@outside}\color{\kvtcb@collower}\kvtcb@fontlower\kvtcb@before@lower\ignorespaces%
\begingroup\tcb@layer@inc\@tcbuselistingimage\tcb@layer@dec\endgroup%
\unskip\kvtcb@after@lower\endminipage\fi%
\end{minipage}\end{tcb@sbsfloat}%
}
\def\tcb@inputlisting@out@imageAndListing{%
\tcb@inputlisting@out@prepare%
\tcbdimto\tcb@w@outside{\tcb@w@upper}%
\tcbdimto\tcb@w@inside{\tcb@width-\tcb@w@upper-\kvtcb@sbs@gap}%
\begin{tcb@sbsfloat}\begin{minipage}{\tcb@width}%
\iftcb@lowervisible\tcb@minipage{\tcb@w@outside}\color{\kvtcb@collower}\kvtcb@fontlower\kvtcb@before@lower\ignorespaces%
\begingroup\tcb@layer@inc\@tcbuselistingimage\tcb@layer@dec\endgroup%
\unskip\kvtcb@after@lower\endminipage\fi\hfill%
\tcb@minipage{\tcb@w@inside}%
\begin{tcolorbox}[width=\tcb@w@inside,nobeforeafter,nofloat,sidebyside=false]\tcbuselistinglisting\end{tcolorbox}%
\endminipage%
\end{minipage}\end{tcb@sbsfloat}%
}
\tcbset{%
listing file/.store in=\kvtcb@listingfile,%
comment/.store in=\kvtcb@listingcomment,%
listing and text/.code={\tcb@set@listing@process{\tcb@listing@listingAndText}},%
text and listing/.code={\tcb@set@listing@process{\tcb@listing@textAndListing}},%
listing and comment/.code={\tcb@set@listing@process{\tcb@listing@listingAndComment}},%
comment and listing/.code={\tcb@set@listing@process{\tcb@listing@commentAndListing}},%
listing only/.code={\tcb@set@listing@process{\tcb@listing@listing}},%
text only/.code={\tcb@set@listing@process{\tcbuselistingtext}},%
text above listing/.code={\let\tcb@inputlisting\tcb@inputlisting@out@textAboveListing},%
image above listing/.code={\let\tcb@inputlisting\tcb@inputlisting@out@ImageAboveListing},%
listing above text/.code={\let\tcb@inputlisting\tcb@inputlisting@out@listingAboveText},%
listing above image/.code={\let\tcb@inputlisting\tcb@inputlisting@out@listingAboveImage},%
text side listing/.style={sidebyside,text and listing},%
listing side text/.style={sidebyside,listing and text},%
text outside listing/.code={\let\tcb@inputlisting\tcb@inputlisting@out@textAndListing},%
image outside listing/.code={\let\tcb@inputlisting\tcb@inputlisting@out@imageAndListing},%
listing outside text/.code={\let\tcb@inputlisting\tcb@inputlisting@out@listingAndText},%
listing outside image/.code={\let\tcb@inputlisting\tcb@inputlisting@out@listingAndImage},%
listing engine/.is choice,
}
\makeatother
\newtcblisting{ltxlisting}[1][]{
skin=bicolor,
boxrule=1mm,
fonttitle=\bfseries,
coltitle=black,
frame style={
draw=black,
color=Aquamarine,
},
colback=mygray,
listing options={style=mystyle},
#1
}
\begin{document}
\tcbuselistingimage[width=4cm]{codei}
\begin{ltxlisting}[listing above image]
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{ltxlisting}
\begin{ltxlisting}[listing outside image]
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{ltxlisting}
\tcbuselistingimage[width=5cm]{codeii}
\begin{ltxlisting}[image above listing]
\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikz\node[fill=red!30] {A Ti\textit{k}Z node};
\end{document}
\end{ltxlisting}
\begin{ltxlisting}[image outside listing]
\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikz\node[fill=red!30] {A Ti\textit{k}Z node};
\end{document}
\end{ltxlisting}
\end{document}

listing and comment
orcomment and listing
together withcomment=\includegraphics{something}
.tcolorbox
; in my answer below I gave the initial steps for the implementation, but surely you can highly improve the code :) (I am not particularly satisfied with the interface used for the image; I was in a hurry so I used an external command, but a key-value interface would be mush better).comment outside/above/etc listing
options. For the comment, a picture (or anything) can be used.