# Is there any package similar to verbatim but with support for icons?

I am looking for a package that provides verbatim environments but also supports different background color, frame style and icons (tips, error, warning etc.) shown on the upper-left corner.

-
Take a look at the listings package ctan.org/pkg/listings – Ronny Jan 22 '13 at 7:42
Thanks. I tried that one, it does supports different background color and different frame style. But it seems that it doesn't support the icon. I am looking for something like this postimage.org/image/d2ph3mzpt – user1285419 Jan 22 '13 at 7:53
You can escape from a listing to LaTeX using a self-specified Character (so it does not interfere with your usual code) and include (by doing that) any image inside or besides the Code. I'll try something like that in the evening. – Ronny Jan 22 '13 at 8:46

The fancyvrb package currently does not support the use of icons, and frame styles are limited. However, it is possible to combine the provided SaveVerbatim environment with the \fcolorbox command (provided by the xcolor package) to get some interesting results.

## Declarations

The strategy is to first save parsed data (text) to a storage bin using the SaveVerbatim environment. Next the \printmacro and \printmacroi commands use the parsed data saved to the last storage bin. The icons are included using \includegraphics.

\documentclass{article}

\usepackage{calc}
\usepackage{fancyvrb}
\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}
\definecolor{LightGray}{RGB}{193,193,193}
\definecolor{LightYellow}{RGB}{253,247,172}

%%%%% \printmacro

\newcommand{\printmacro}[1][mc]{% mc = label of storange bin
%
{\setlength{\fboxrule}{3.0\fboxrule}% thickness of frame
\setlength{\fboxsep}{1.5\fboxsep}% separation between text and frame
%
\noindent%
\fcolorbox{LightGray}{LightYellow}{% frame and background color
%
\begin{minipage}[c]{\linewidth - 2.0\fboxrule - 2.0\fboxsep}
\protect\UseVerbatim[fontsize=\footnotesize]{#1} % font size of text in verbatim
\end{minipage}}%
}%
}

%%%% \printmacroi

% \macroicon is used by \printmacroi to print the icon and some horizontal space
\newcommand{\macroicon}{%
\hspace{1em}%
\parbox[c]{0.75cm}{\includegraphics[keepaspectratio=true,width=0.75cm]{light-bulb-icon}}%
\hspace{1em}%
}

% \newmacroicon provides a quicker way to renew \macroicon mid-document
\newcommand{\newmacroicon}[3][1em]{%
\renewcommand{\macroicon}{%
\hspace{#1}%
\parbox[c]{#2}{\includegraphics[keepaspectratio=true,width=#2]{#3}}%
\hspace{#1}%
}}

\newlength\macroiconwidth

\newcommand{\printmacroi}[1][mc]{%
%
{\setlength{\fboxrule}{3.0\fboxrule}%
\setlength{\fboxsep}{1.5\fboxsep}%
%
\noindent%
\fcolorbox{LightGray}{LightYellow}{%
%
\macroicon%
\settowidth{\macroiconwidth}{\macroicon}%
%
\begin{minipage}[c]{\linewidth - \macroiconwidth - 2.0\fboxrule - 2.0\fboxsep}
\protect\UseVerbatim[fontsize=\footnotesize]{#1}
\end{minipage}}%
}%
}


## Usage

Next I show how to use \printmacro, \printmacroi and \newmacroicon mid-document:

\begin{document}

Here's the first example with no icon:

\begin{SaveVerbatim}{mc}
\usepackage{microtype}
\end{SaveVerbatim}
\printmacro

\noindent
The second and third examples contain icons. The \verb+\newmacroicon+ command
is used midway to change the type of icon used:

\begin{SaveVerbatim}[commandchars=\|]{mc}
\(|color(OliveGreen)DeclareMicrotypeSet){basictext}
{ encoding = {OT1,T1,T2A,LY1,OT4,QX,T5},
family = {rm*,sf*},
series = {md*},
size = {normalsize,footnotesize,small}
}
\end{SaveVerbatim}
\printmacroi

Now a different icon will be used:

\newmacroicon{0.75cm}{warning-icon}

\begin{SaveVerbatim}{mc}
\UseMicrotypeSet[protrusion]{basictext}
\end{SaveVerbatim}
\printmacroi

\end{document}


## Tips

• You must always place \printmacro (no icon) or \printmacroi on the line after \end{SaveVerbatim}.
• To change the default font size of text in verbatim, go to the line \protect\UseVerbatim[fontsize=\footnotesize]{#1} and use \small, or \normalsize, etc. instead of \footnotesize.
• The thickness of the frame and separation between text and frame are set by \fboxrule, and \fboxsep, respectively. You can either use explicit values, for example \setlength{\fboxrule}{0.5cm}, or use multiples of the default dimensions, e.g., \setlength{\fboxrule}{5.0\fboxrule} gives 5 times the default frame thickness.

• When declaring the command \macroicon in the preamble(!), you must ensure the image file you use (e.g., light-bulb.png} is either in the same directory as your main document, or in a visible path.

• You can change the icon mid-document with the \newmacroicon command. For example, \newmacroicon[0.5cm]{2cm}{exclamation-mark} sets the horizontal space before and after the icon to 0.5cm, sets the width of the icon to 2cm, and loads a new icon (the image file exclamation-mark). Alternatively, you can use \newmacroicon{2cm}{exclamation-mark} (this way you don't need to specify the horizontal space).
• If you just want to type simple text in verbatim (that is, no extra colours, no fancy text formatting, etc.), simply use \begin{SaveVerbatim}{mc} ... \end{SaveVerbatim}, followed by \printmacro or \printmacroi on the next line. (Just like I did on the first example.)
• If you want to customise text in verbatim, then you need to pass special options to the SaveVerbatim environment, just like I did on my second and third examples. Options are passed inside the square brackets. You'll need to consult the package documentation of fancyvrb for more information on what you can do.
• At the moment, all \printmacro and \printmacroi boxes have the same frame style. And you need to call \newmacroicon whenever you wish to change the icon. If you prefer to have a separate command for each type of message, you need new declarations. For example, the following command can be used only for error messages, without the need to ever call \newmacroicon:

(this time the entire box is placed inside the center environment)

% \printmacroe prints text in verbatim with an error icon
\newcommand{\printmacroe}[1][mc]{%
%
{\setlength{\fboxrule}{3.0\fboxrule}%
\setlength{\fboxsep}{1.5\fboxsep}%
%
\begin{center}%
\fcolorbox{Red}{LightYellow}{%
%
\hspace{1em}%
\parbox[c]{0.75cm}{\includegraphics[keepaspectratio=true,width=0.75cm]{error-icon}}%
\hspace{1em}%
%
\settowidth{\macroiconwidth}{%
\hspace{1em}%
\parbox[c]{0.75cm}{\includegraphics[keepaspectratio=true,width=0.75cm]{error-icon}}%
\hspace{1em}}%
%
\begin{minipage}[c]{\linewidth - \macroiconwidth - 1.5cm}
\protect\UseVerbatim[fontsize=\footnotesize]{#1}
\end{minipage}}%
\end{center}
}%
}


-
A more elegant solution would exclude the need to call \printmacro or \printmacroi following the verbatim environment. However, the SaverVerbatim environment has the limitation that the line \end{SaveVerbatim} must occur as is in the document, not(!) inside a command or another environment. To understand the options passed to SaveVerbatim, consult the package documentation. – nnunes Jan 22 '13 at 10:36
– nnunes Jan 22 '13 at 10:59
That looks pretty complicated but works perfectly. But I try it on ... I don't understand how it works and almost can't understand even one command line above. But from my using it, It seems that the text was not really wrapped by the box. Since if you typing too long text, it won't wrap automatically. Well, this looks better, but give me pretty hard time to understand how to use :( – user1285419 Jan 22 '13 at 11:25
@user1285419 Text in verbatim is always printed exactly as it is typed. If you try using the standard \begin{verbatim} text \end{verbatim}, you always have to include line breaks manually. The SaveVerbatim used above works the same way. If you don't wish to make use of any special options, simply use begin{SaveVerbatim}{macrolabel} text \end{SaveVerbatim}. I will add some extra comments on my answer. – nnunes Jan 22 '13 at 11:30
Thanks. I will play around with your code. I think it gives me almost what I need. Just one question (don't be mad :) must I place \printmacroi every where after \end{SaveVerbatim}? Is that possible to wrap all need command to one, so I can only call one command to show a tips-box, call another command to show a warning box ... etc.? Thanks. – user1285419 Jan 22 '13 at 11:44

You could try and use a combination of existing packages, lets say TikZ and minted:

\documentclass{scrartcl}

\usepackage{minted}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{graphicx}

\usemintedstyle{monokai}
\definecolor{bg}{rgb}{0.35,0.35,0.35}

\begin{document}
\begin{tikzpicture}[node distance=1mm]
\node (code) {\inputminted[bgcolor=bg,frame=single,framerule=1pt]{python}{mplTest.py}};
\node [left=of code] {\includegraphics[width=1cm]{light-bulb-icon.png}};
\end{tikzpicture}
\end{document}


Which yields (with appropriate code & icon files):

You could then wrap these into newcommands for each type, i.e. warning, error, etc.

NOTE: minted needs the Python language interpreter and the Pygments library installed.

-
+1. Does this package work with TeX commands? – Sigur Jan 22 '13 at 11:14
@Sigur Yes it does. – Psirus Jan 22 '13 at 11:20
Is Pygments necessary only for Python codes or for every example? My minimal example from page 4 of minted documentation is not working. – Sigur Jan 22 '13 at 12:40
Pygments is needed regardless of which language the code you want to typeset is in. – Psirus Jan 22 '13 at 12:45

Another, IMHO, elegant solution based on the great, but still relatively unknown tcolorbox package.

This package has the advantage, that it has built-in support for verbatim material (especially listings) and PGF/TikZ-based decorations, which provides a lot of graphical flexibility. It already comes with a stunning number of options, float support, etc.

\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage{lmodern,beramono}

\usepackage[listings,skins]{tcolorbox}

\makeatletter
\tcbset{%
icon/.style 2 args={%
enlarge top by=4ex,
skin=freelance,
frame code={%
\tcb@drawframe@standard % standard implementation
\node[at=(frame.north west),top color=#1, bottom color=#1!70!black, draw=#1!60!black, circle, font=\bfseries\huge]{#2};}
},
warning/.style={icon={red}{!}},
question/.style={icon={green}{?}},
}
\makeatother

\begin{document}

\begin{tcblisting}{%
warning,
listing only,
title=This is source code in XML, fonttitle=\bfseries,
listing options={language=XML,columns=fullflexible,keywordstyle=\color{red}}
}
<?xml version="1.0"?>
<project name="Package tcolorbox" default="documentation" basedir=".">
<description>
Apache Ant build file (http://ant.apache.org/)
</description>
</project>
\end{tcblisting}

\begin{tcblisting}{%
question,
listing only,
title=This is source code in XML, fonttitle=\bfseries,
listing options={language=XML,columns=fullflexible,keywordstyle=\color{red}}
}
<?xml version="1.0"?>
<project name="Package tcolorbox" default="documentation" basedir=".">
<description>
Apache Ant build file (http://ant.apache.org/)
</description>
</project>
\end{tcblisting}

\end{document}


-

Another option would be bclogo. It's main use it to build a framed and colored box with logo and title. Next you will find an example.

\documentclass[a4paper]{article}
\usepackage[a4paper]{geometry}
\usepackage[T1]{fontenc}
\usepackage{lmodern,beramono}

\usepackage{listings}
\usepackage[tikz]{bclogo}

\lstset{
basicstyle=\small,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\ttfamily,
showstringspaces=false,
language=XML,%
columns=fullflexible,%
}

\begin{document}

\begin{bclogo}[couleur=green!30, logo=\bclampe]{}
\begin{lstlisting}
<?xml version="1.0"?>
<project name="Package tcolorbox" default="documentation" basedir=".">
<description>
Apache Ant build file (http://ant.apache.org/)
</description>
</project>
\end{lstlisting}
\end{bclogo}

\begin{bclogo}[couleur=red!30, logo=\bcdanger]{}
\begin{lstlisting}
<?xml version="1.0"?>
<project name="Package tcolorbox" default="documentation" basedir=".">
<description>
Apache Ant build file (http://ant.apache.org/)
</description>
</project>
\end{lstlisting}
\end{bclogo}

\end{document}


and its result:

-
This one looks pretty nice :) – user1285419 Jan 22 '13 at 17:19
Nice, I wasn't aware of this cute package! – Daniel Jan 22 '13 at 20:33

One way to do, what you want, is to escape from the listing using an escapechar= that is not used in the Code you're working with (I used | though thats not optimal for Java) and put an tikzpicture at the beginning of your code (you could extract that to commands line \warningicon or \infoicon and just put that command in the escaped part.

This would as an MWE look like

\documentclass[a4paper,12pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{listings,xcolor,tikz}

\begin{document}
\begin{lstlisting}frame=single,frameround=tttt,% rounded frame
basicstyle=\ttfamily,tabsize=3,% code style
backgroundcolor=\color{blue!25},%some background color
language=Java,escapechar=|] %language and your escape char
|\begin{tikzpicture}[overlay]
\node[anchor=south west,inner sep=0] (image) at (-1cm,0) {\includegraphics[width=1cm]{lightbulb.png}};
\end{tikzpicture}|
public class Main {
public static void main(String [] args) {
System.out.println("Hello world !");
}
}
\end{lstlisting}
\end{document}


Here in the escaped part we place an tikzpicture as overlay including a node that is 1cm left of the listing where we include the lightbulb. Afterwards we define the usual code we want.

Together with

\newcommand{\lightbulbicon}{%
\begin{tikzpicture}[overlay]
\node[anchor=south west,inner sep=0] (image) at (-1cm,0) {\includegraphics[width=1cm]{lightbulb.png}};
\end{tikzpicture}
}


The first line of an info-icon-code is just |\lightbulbicon|.

The complete result is something like

Where of course you also can specify other positions of the lightbulb/icon depending on the node anchor= and the position of the node relative to the listings environment.

Edit: I just noticed that there is an linebreak too much after the ending | of the TikZ-Picture, hence your first line of code should be (in total)

|\lightbulbicon|public class Main {


which removes the first empty line in the picture.

-
It looks pretty neat. But just wonder if there is any way to move the icon inside the box and without overlap with the text – user1285419 Jan 22 '13 at 9:32
Well, now, the overlay kind of removes the picture from the usual text flow. If you include it into the flow, it is inside, moving the first line of code down (so there is no flow) and “breaking” the backgroundcolor. – Ronny Jan 22 '13 at 9:36
The code doesn't fully work on my side. I can see the box but no icon shown if I put the |\lightbulbicon| inside the box, if I move it outside, it shows the icon there. – user1285419 Jan 22 '13 at 11:27
If you change the coordinates of the icon, i.e. to put the icon inside the box, the icon might be hidden behind the background color of the listing environment. – Ronny Jan 22 '13 at 16:26