[Tex/LaTex] Set color to output from verbatim

colorverbatim

I want to show the output from verbatim in blue color.

I have this:

\newcommand{\blockcode}[1]{\ttfamily\small#1}

{\blockcode \begin{verbatim} 
alias vi="vim"
\end{verbatim}}

which works fine.

But when I do this

\newcommand{\blockcode}[1]{\ttfamily\small{\color{blue}#1}}

it throws an error

! Argument of \begin has an
extra }.

Update

I tried

\documentclass{article}
\usepackage{verbatim,color,lipsum}

\newcommand{\blockcode}{\ttfamily\small\color{blue}}

\begin{document}
\lipsum*[2]
{\blockcode \begin{verbatim} 
alias vi="vim"
\end{verbatim}}

\lipsum*[3]
\end{document}

But the blockcode doesn't end… it flows on to the next element…

Update 2

If I do this:

\paragraph{Hello}
\begin{blockcode}
alias vi="vim"
\end{blockcode}

The Hello also becomes blue in color. Any way I can stop this?

Update 3

Again a roadblock…

\subsubsection*{Definition}
\begin{blockcode} 
:iabbrev <key> <expansion> 
\end{blockcode}

This creates too much gap between Definition and the code.

Best Answer

You have a number of better alternatives.

First. Package verbatim

\documentclass{article}
\usepackage{verbatim,color,lipsum}
\newenvironment{blockcode}
  {\leavevmode\small\color{blue}\verbatim}
  {\endverbatim}

\begin{document}
\lipsum*[2]
\begin{blockcode}
alias vi="vim"
\end{blockcode}
\lipsum*[3]
\end{document}

Pros: easy. Cons: not really customizable.

Second. Package fancyvrb

\documentclass{article}
\usepackage{fancyvrb,color,lipsum}

\DefineVerbatimEnvironment{blockcode}
  {Verbatim}
  {fontsize=\small,formatcom=\color{blue}}

\begin{document}
\lipsum*[2]
\begin{blockcode}
alias vi="vim"
\end{blockcode}
\lipsum*[3]
\end{document}

Pros: very customizable. Cons: less easy to learn how to manage the definitions.

Third. Package listings

\documentclass{article}
\usepackage{listings,color,lipsum}

\lstnewenvironment{blockcode}[1][]
  {\lstset{language=Bash,
           columns=fullflexible,
           basicstyle=\small\ttfamily,
           keywordstyle=\color{blue},
   }}
  {}

\begin{document}
\lipsum*[2]
\begin{blockcode}
alias vi="vim"
\end{blockcode}
\lipsum*[3]
\end{document}

Pros: keyword coloring for the supported languages. Cons: doesn't support UTF-8