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'm trying to create an alias for the verbatim environment like this:

\newcommand{\vb}[1]{\begin{verbatim} #1 \end{verbatim}}

So I can use it like this:

 \vb{
      word.
  }

But when using it, I get this error:

! Missing $ inserted.
<inserted text> 
            $
l.93 \end{verbatim}

Where line 93 is the end of a verbatim environment I used after later down the file after closing the \vb. If I replace all verbatim environments with the \vb syntax, then I get this error:

Runaway argument?
zip([],B) = case ([],B) of ([],_) => [] | (_,[]) => [] | (x::L,y::R\ETC.
! File ended while scanning use of \@xverbatim.
<inserted text> 
            \par 

So it seems like the \vb is not closing the environment correctly? How do I get this to work ? The end goal is to get a \vb command that I can use like I described above, and bonus points if I could indent the stuff in the \vb command by a few mm.

share|improve this question
1  
As far as I know, there’s no way in replacing the environment with a command. Nonetheless you should take a deep look into the fancyvrbdocumentation. –  Speravir Sep 18 '12 at 2:29
    
If you just want oneline verbatim text, you could use a \Verb variant in Fancyvrb. –  Speravir Sep 18 '12 at 2:32
1  
Your statement "So it seems like the \vb is not closing the environment correctly" is absolutely correct. Tokens are scanned in search of \end{verbatim} and is never found. –  Werner Sep 18 '12 at 5:26
1  
Werner forgot to provide the following link (I found it in another answer of him) from the UK TeX FAQ: Why doesn’t verbatim work within …?. –  Speravir Sep 18 '12 at 22:41
add comment

1 Answer 1

up vote 6 down vote accepted

A general rule is that you can't have \begin{verbatim} or the \verb command in the argument to another command, including an argument to \newcommand.

If you really want to use that syntax, you of course can't have braces in the argument and hope that they will be printed as themselves: either they delimit the argument or they must be printed.

If this limitation satisfies you, then

\makeatletter
\newcommand{\vb}{%
  \begingroup
  \advance\@totalleftmargin\parindent
  \@verbatim
  \catcode`{=1 \catcode`}=2 \catcode` =10
  \frenchspacing
  \@vb}
\def\@vb#1{#1\endtrivlist\endgroup} 
\makeatother

will allow you to write

\vb{
    word.
  }

I don't think this is a great improvement than saying

\begin{verbatim}
word.
\end{verbatim}

If you want a margin indent of the verbatim, look at the option xleftmargin in to the Verbatim environment provided by the package fancyvrb.

share|improve this answer
add comment

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.