# [Tex/LaTex] ny package similar to verbatim but with support for icons

framedverbatim

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.

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}
}%
}