11

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?

lockstep
  • 250,273

3 Answers3

12

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.

Martin Scharrer
  • 262,582
3

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

\documentclass[a4paper]{article}
\usepackage{color}
\definecolor{cverbbackground}{gray}{0.7}
\definecolor{cverbborder}{gray}{0.2}
\makeatletter
\newbox\cverbbox
\def\cverb{\setbox\cverbbox=\hbox\bgroup
    \verb@eol@error \let\do\@makeother \dospecials
    \verbatim@font\@noligs
    \@ifstar\@scverb\@cverb}
\def\@scverb#1{%
  \catcode`#1\active
  \lccode`\~`#1%
  \gdef\verb@balance@group{\cverb@egroup
     \@latex@error{\noexpand\verb illegal in command argument}\@ehc}%
  \aftergroup\verb@balance@group
  \lowercase{\let~\cverb@egroup}}%
\def\@cverb{\@vobeyspaces \frenchspacing \@scverb}
\def\cverb@egroup{\global\let\verb@balance@group\@empty\egroup
  \fcolorbox{cverbborder}{cverbbackground}{\box\cverbbox}}
\makeatother

\begin{document}

\cverb|ab{\c| \cverb*|a }|
\end{document}
egreg
  • 1,121,712
  • 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. – Martin Scharrer Jul 05 '11 at 20:44
  • 1
    @Martin: I supposed so. It was just to try it out. – egreg Jul 05 '11 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 Mar 13 '12 at 08:11
3

Here a solution which works with xparse:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{xparse}
\NewDocumentCommand{\Fverb}{v}
      {\fcolorbox{black!50}{black!25}{#1}}
\begin{document} 
HALLO \Fverb|\foo| HALLO
\end{document}
Marco Daniel
  • 95,681