I'd like to redefine the \verb|...| command so that it had a light-gray background and a little bit darker border around it, and a little bit of padding to make this background "box" noticable. Anyone knows how to do that? If that is not possible to do easily, can I define a new command that acts like \verb|...| and does that?

  • 1
    Note that it is \verb<char>...<char>, e.g. \verb|...| or \verb{...{, not \verb{..}. Commented Jul 5, 2011 at 20:00
  • It's a pitty that xparse doesn't support a verbatim argument type. Commented Jul 5, 2011 at 20:06
  • @Martin: There are good reasons why xparse does not deal with verbatim material (essentially, a truly general solution is not available, as TeX's parsing approach makes certain cases impossible to handle with generalised code).
    – Joseph Wright
    Commented Jul 5, 2011 at 21:12
  • @Joseph: Indeed. I just had a look on \verb again to improve newverbs and will have to at least provide two ways: one which really only provides the verbatim characters and one which provides them in a typeset-able form (e.g. using \@noligs). Commented Jul 5, 2011 at 21:18
  • Preview on new version of newverbs (already implemented in the develop version): \newcommand\myverb{\collectverbtext{\@myverb}} \def\@myverb#1{The collected verb text: "#1"} Commented Jul 6, 2011 at 0:24

3 Answers 3


Update 2011-07-27

As promised the new version of my newverbs package provides two macros to collect verbatim arguments with the \verb|...| syntax but also with { .. }. For typesettable verbatim use \collectverb{\macrowhichgetsitasargument}.

For the requested colored background and frame use the \fcolorbox{<frame color>}{<background color>}{<text>} macro.




The newverbs package allows you to define variations of the \verb command which code placed before and after it. You need to use a savebox for advanced formatting. (I'm planning to also support macros which receive the verbatim text as normal argument in the next version.)






Use the xcolor package to add colors.

The package already provides \fverb (draws a \fbox around it) and \qverb (adds quoting signs) by default.

You could also use the \verb-like \lstinline macro of the listings package. It accepts the many listings options.


I've modified the standard \verb command and got the result, but anyway I recommend Martin's approach.

    \verb@eol@error \let\do\@makeother \dospecials
     \@latex@error{\noexpand\verb illegal in command argument}\@ehc}%
\def\@cverb{\@vobeyspaces \frenchspacing \@scverb}


\cverb|ab{\c| \cverb*|a }|
  • That's basically what newverbs does by itself, except it adds small begin and end hooks in the normal \verb so that it can be reused for multiple \verb like macros. It also support the \verb replacement of tabularx as a bonus. Commented Jul 5, 2011 at 20:44
  • 1
    @Martin: I supposed so. It was just to try it out.
    – egreg
    Commented Jul 5, 2011 at 20:46
  • 1
    This solution is the best i ve found, but is there a way to change \verb{} font color, and font size?
    – 71GA
    Commented Mar 13, 2012 at 8:11

Here a solution which works with xparse:

HALLO \Fverb|\foo| HALLO
  • Not quite: \Fverb|!`| will give ¡.
    – egreg
    Commented Jan 30, 2012 at 18:47
  • @egreg: Why this? I tested only commands ;-). A space between |! `| works. Commented Jan 30, 2012 at 18:50
  • 1
    The font has ligatures defined. Indeed in traditional verbatim mode the backquote, <, > and - are active and defined so as not to produce ligatures.
    – egreg
    Commented Jan 30, 2012 at 18:56

You must log in to answer this question.