Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
1  
Don't give \blockcode an argument. \newcommand{\blockcode}{\ttfamily\small\color{blue}} –  Ulrike Fischer Feb 5 '13 at 15:13

2 Answers 2

up vote 5 down vote accepted

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

share|improve this answer
    
What does lipsum do? –  ATOzTOA Feb 5 '13 at 16:31
    
Just try it: it provides mock text for filling examples. –  egreg Feb 5 '13 at 16:31
    
Thanks, this worked for me, although this wasn't my exact question... –  ATOzTOA Feb 5 '13 at 17:05
    
@ATOzTOA David will surely say that I always answer the question that should have been asked. –  egreg Feb 5 '13 at 17:10
    
Please see my Update 2. –  ATOzTOA Feb 6 '13 at 6:18
\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.

share|improve this answer
    
This is not working, the verbatim doesn't stop at \end{verbatim}. Please see the updated question. –  ATOzTOA Feb 5 '13 at 17:05
    
You included the verbatim package which has the documented restriction that you can not have anything on the line after \end{verbatim} so you would have to move that last } down a line, which just shows: always post complete documents in questions not fragments –  David Carlisle Feb 5 '13 at 17:08
    
Yeah, it works now... thanks... But, that last brace is already included in my question... oh yeah, now I understand what you mean :) –  ATOzTOA Feb 5 '13 at 18:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.