6

I'm using the package tcolorbox to create a file with examples Latex, I have reviewed the options tcblisting environment and use these:

  • text outside listing
  • listing outside text
  • listing above text
  • text above listing

But sometimes I want to show the example does not fit as I desire, and this comes my question: can you use an option to add a graphic instead of formatted text?. Something like "graphic" option from package "showexpl". regards, Pablo EDIT: Like this enter image description here

9
  • 2
    For this scenario, use listing and comment or comment and listing together with comment=\includegraphics{something}. – Thomas F. Sturm Feb 17 '14 at 11:09
  • Using both scenarios, the image is on the left and right, but inside the box. My idea is to replace the formatted text (in four cases) for a picture and this is out of the box (just the source code remains in a colored box), ie, put the image up, down, left and right. – Pablo González L Feb 17 '14 at 17:55
  • 2
    @ThomasF.Sturm I think that the feature requested in this question might be an interesting addition to 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). – Gonzalo Medina Feb 17 '14 at 21:00
  • It would be a great idea that was included in the package "tcolorbox", it is quite useful to show examples with different types of fonts, or slide, or capture images from programs (for example written in GTK). – Pablo González L Feb 17 '14 at 22:36
  • @GonzaloMedina (and Pablo Ganzález): I agree with you :-) Yesterday, when I wrote the comment, I thought that this would be a valuable addition and I started coding already ;-) Currently, I made all missing comment outside/above/etc listing options. For the comment, a picture (or anything) can be used. – Thomas F. Sturm Feb 18 '14 at 7:01
3

Starting with tcolorbox version 2.71 (2014/02/21), all missing comment outside listing e.a. options are implemented. A comment is considered as quite anything, especially a picture. For pictures, there is the new option image comment which simply sets a picture as comment.

A comment outside a tcolorbox (left/right/below/above) is always considered as the lower part of the box content. Thus, in the following example, center lower is centering a picture which is actually above the listing.

\documentclass{article}
\usepackage[most]{tcolorbox}

\newtcblisting{mylisting}[1]{%
  colback=red!5!white,colframe=red!75!black,
  image comment={width=2.5cm}{example-image-a.pdf},
  #1}

\begin{document}


\section{listing outside image}

\begin{mylisting}{listing outside comment,righthand width=2.5cm}
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{mylisting}

\section{image outside listing}

\begin{mylisting}{comment outside listing,lefthand width=2.5cm}
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{mylisting}

\section{listing above image}

\begin{mylisting}{listing above comment,center lower}
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{mylisting}

\section{image above listing}

\begin{mylisting}{comment above listing,center lower}
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{mylisting}

\clearpage
\section{listing side image}

\begin{mylisting}{listing side comment,lower separated=false,
  righthand width=2.5cm}
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{mylisting}


\section{image side listing}

\begin{mylisting}{comment side listing,lower separated=false,
  lefthand width=2.5cm}
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{mylisting}


\section{listing and image}

\begin{mylisting}{listing and comment,bicolor,colbacklower=white,center lower}
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{mylisting}


\section{image and listing}

\begin{mylisting}{comment and listing,bicolor,colback=white,
  colbacklower=red!5!white,center upper}
\Large\TeX{} \LaTeX{}\par
\LaTeX{} \TeX{}
\end{mylisting}

\end{document}

enter image description here enter image description here

5

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}

enter image description here

12
  • Thanks for the answer, now go out step using the classic "minipage" (read my comments and see if you can give me a hand). – Pablo González L Feb 17 '14 at 17:57
  • @PabloGonzález I am not sure if I understand what you are trying to achieve. Do you want the image side-by side with the code box? Perhaps we could speak in Spanish to clarify things ;-) – Gonzalo Medina Feb 17 '14 at 17:59
  • Gracias Gonzalo, el ingles no es mi fuerte, ahora si me explico, deseo convertir las siguientes opciones (si es posible ) text outside listing, listing outside text, listing above text, text above listing en image outside listing, listing outside image, listing above image, image above listing. Quiero dejar en una caja coloreada solo el código fuente, y en vez de mostrar el código formateado, mostrar una imagen (fuera de la caja de colores). Espero haberme explicado. Saludos – Pablo González L Feb 17 '14 at 18:05
  • @PabloGonzález Ah, ahora ya me queda claro. Gracias. Agregar la imagen es buena idea, así todo el mundo tendrá claro el objetivo. Vamos a ver qué se nos ocurre :-) – Gonzalo Medina Feb 17 '14 at 18:33
  • @PabloGonzález please see my updated answer. I defined the four desired options. – Gonzalo Medina Feb 17 '14 at 21:32

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.