[Tex/LaTex] How to redefine the \verb|…| command

colormacrosverbatim

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?

Best Answer

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.

\documentclass{article}
\usepackage{xcolor}
\usepackage{newverbs}[2011/07/23]
\newcommand{\myverb}{\collectverb{\fcolorbox{black!50}{black!25}}}

\begin{document}
    \myverb|%^&\|
    \myverb{%^&\}
\end{document}

Result


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.)

Example:

\usepackage{newverbs}

\newverbcommand{\myverb}{\begin{lrbox}{\verbbox}\mytextformatmacro}{\end{lrbox}{\mycommand{\usebox{\verbbox}}}

...

\myverb|%^&\|

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.