有效的方法:
我有包含如下代码的 TeX 文件:
I want \texttt{this text} to have a background color.
我想要\texttt
有一个背景颜色:
I want \colorbox{lightgray}{\texttt{this text}} to have a background color.
但是,我无法修改 TeX 文件的主体 - 只能修改前言。因此我添加了:
\let\oldtexttt\texttt
\renewcommand{\texttt}[1]{\colorbox{lightgray}{\oldtexttt{#1}}}
效果非常好。
无效的方法:
不幸的是,我还有:
I want \Verb|this text| to have a background color.
真正应该是:
I want \colorbox{lightgray}{\Verb|this text|} to have a background color.
但我无法\renewcommand
按照上面相同的方式在它周围添加\colorbox
。
麦格维:
\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{xcolor}
\usepackage{fancyvrb}
\let\oldtexttt\texttt
\renewcommand{\texttt}[1]{\colorbox{lightgray}{\oldtexttt{#1}}}
\begin{document}
I want \texttt{this text} to have a background color.
I want \Verb|this text| to have a background color.
\end{document}
答案1
使用newverbs
包:
\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{xcolor}
\usepackage{fancyvrb}
\usepackage{newverbs}
\let\oldtexttt\texttt
\renewcommand{\texttt}[1]{\colorbox{lightgray}{\oldtexttt{#1}}}
\renewcommand{\Verb}{\collectverb{\colorbox{lightgray}}}
\begin{document}
I want \texttt{this text} to have a background color.
I want \Verb|this text| to have a background color.
\end{document}