1

I want to change style of verb environment of my latex.

  • Want a light gray background with red text.
  • I tried using newverbs package I'm able to change textcolor or backgroundcolor but it's failing to use both once.
\documentclass{article}

\usepackage{newverbs}
\usepackage{xcolor}
\begin{document}

\renewenvironment{verbatim}
{\semiverbatim\colorbox{gray!20}}
{\endsemiverbatim}
\renewcommand{\verb}{\collectverb{\colorbox{gray!20}}}

\colorbox{gray!20}{{\color{red}What I want}}


\verb|What I have|


\end{document}
  • Above is the working example with only foreground changed.
  • Below is the code I'm using for both.
\documentclass{article}

\usepackage{newverbs}
\usepackage{xcolor}
\begin{document}

\renewenvironment{verbatim}
{\semiverbatim\colorbox{gray!20}\color{red}}
{\endsemiverbatim}
\renewcommand{\verb}{\collectverb{\colorbox{gray!20}\color{red}}}

\colorbox{gray!20}{{\color{red}What I want}}


\verb|What I have|


\end{document}

1 Answer 1

3

You code does not work, because it would use \color as contents argument of \colorbox. You could define a new command:

\newcommand{\colorboxwithcontentcolor}[3]{\colorbox{#1}{\color{#2}#3}}

and use

\renewcommand{\verb}{\collectverb{\colorboxwithcontentcolor{gray!20}{red}}}

but easier would be to move \color{red} in front of \colorbox:

\documentclass{article}

\usepackage{newverbs}
\usepackage{xcolor}
\begin{document}

\renewcommand{\verb}{\collectverb{\color{red}\colorbox{gray!20}}}

\colorbox{gray!20}{{\color{red}What I want}}


This is, \verb|what I have| and want.


\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .