Set color to output from verbatim

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


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

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

Only really works by accident. the argument to \blockcode is (just) \begin so \begin is removed from its argument {verbatim} but as # is replaced at the end of the definition, \begin is put back and it works out OK,

Once you use

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

Then the expansion is

\ttfamily\small{\color{blue}\begin}

So the first thing \begin sees is a closing brace and things go wrong.

Just use

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

Note that you should always end a font size change command in vertical mode

{\small aaaa\par}

not

{\small aaa}\par

otherwise the line spacing will be wrong. In this case you are OK as verbatim is a list environment so ends the paragraph.

Also as noted in grfguide using a \color command before the list will adversely affect the spacing (it's not my fault). In this case as you are changing teh size anyway thi sisn't perhaps too much of an issue.

Tags:

Verbatim

Color