I'm trying to write a \newcommand for verbatim:


and use it as

\codeline{int foo;}

but it gives an error "File ended while scanning use of \@xverbatim". How to fix it?

  • 6
    You would have to use other means to include verbatim content inside a macro. See Verbatim inside a command. You may be interested in what the listings package has to offer instead. It also provides the means to create your own environments. For example, see Creating a custom environment. – Werner Dec 8 '12 at 6:18
  • @Werner Thanks for your reply. Why can't I include verbatim inside a macro the way I did it? – Paul S. Dec 8 '12 at 6:26
  • 2
    An environment should be used for verbatim text. Take a loot at the fancyvrb package; it provides the \DefineVerbatimEnvironment for defining your own verbatim environment. There is also \DefineShortVerb for creating short verbatim texts like this |int foo;|. – Stephan Lehmke Dec 8 '12 at 6:26
  • If you don't want to use TeX stuff like \ or % in code lines, maybe \newcommand{\codeline}[1]{\par{\ttfamily#1\par}} is enough? – Stephan Lehmke Dec 8 '12 at 6:36
  • 7
    @PaulS.: See Why doesn’t verbatim work within …? – Werner Dec 8 '12 at 7:04

If you want a simple implementation, go with


If you want more detailed control, you should go with fancyvrb or minted packages.

|improve this answer|||||
  • Oh, btw, I like to put a \small in there because the default monospace font is a little too big for my taste. I like my code snippets relatively equal to or smaller than my main font. – recluze Dec 8 '12 at 11:45
  • 2
    This "dirty" command does not work for special caracters as _, &, etc. I.e. \codeline{a_b_c} will result in an error. – strpeter Jan 4 '14 at 16:20
  • 1
    @strpeter I use the underscore package by default in my docs and your example works perfectly with that. – recluze Jan 5 '14 at 13:04

Specific answer

For my example below the following code from the listings package was the solution:


Of course this is only reasonable if you are using short inline code examples without vertical line |.


MWE in which this implementation makes sense:


\title{All graphics of my thesis}

|improve this answer|||||
  • 3
    What's the advantage of \codeline{text} over \lstinline|text|? – egreg Jan 4 '14 at 16:47
  • 1
    @egreg: You can crop several lines together in order to reduce repeating code. Is this not advantage enough? If there is a more efficient answer, please feel free to correct me. – strpeter Jan 4 '14 at 17:06

Use the following:


This will print #1 vermbatim.

|improve this answer|||||

I met the same problem when I tried to include the output of the Unix cal command inside a LaTeX macro. Enclosing the cal output within the verbatim environment does not work for reasons cited above.

My solution is a hack.

  1. Create a LaTeX document (calendar-out.tex) containing only the output of cal and inside a verbatim environment.
  2. Convert calendar-out.tex to png via the commands $ latex calendar-out.tex and $ dvipng -T tight calendar-out.
  3. Incorporate calendar-out.png into original LaTeX document via the graphicx package.

I hope this helps.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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