我想在周围放一些盒子\lstinline
。我从另一个问题中找到了\xpretocmd
技巧\Colorbox
,但我无法将它应用于\tcbox
。有办法吗?
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{regexpatch}
\usepackage{realboxes}
\usepackage{textcomp}
\usepackage{xfrac}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{realboxes}
\usepackage[most]{tcolorbox}
\setmonofont{inconsolata}
\lstset{
language=c++,
breaklines=true,
basicstyle=\ttfamily\footnotesize\color{black},
keywordstyle=\bfseries,
commentstyle=\itshape\color{black!40!black},
keepspaces=true,
showspaces=false,
showtabs=true,
tabsize=3,
upquote=true,
aboveskip=2pt,
belowskip=2pt,
framexleftmargin=2pt,
extendedchars=true,
inputencoding=utf8
}
\definecolor{MyGray}{rgb}{0.8,0.8,0.8}
\makeatletter
\xpretocmd\lstinline{\Colorbox{MyGray}\bgroup\appto\lst@DeInit{\egroup}}{}{}
\makeatother
\newcommand{\CD}[1]{\lstinline{#1}}
\tcbset{on line,
boxsep=3pt, left=0pt,right=0pt,top=0pt,bottom=0pt,
colframe=white,colback=MyGray,
highlight math style={enhanced}
}
\begin{document}
\section{Highlight}
There is a \CD{for} loop. It uses a \CD{std::vector}. A variable is named \CD{i} and \CD{j} is used. \tcbox{variable}, \tcbox{i}, \tcbox{j}
\end{document}
答案1
文档tcolorbox
第 21 页有一个示例,展示了如何使用\NewTotalTCBox
。\lstinline
适合您的情况:
\documentclass{article}
\usepackage[most]{tcolorbox}
\lstset{
language=c++,
breaklines=true,
basicstyle=\ttfamily\footnotesize\color{black},
keywordstyle=\bfseries,
commentstyle=\itshape\color{black!40!black},
keepspaces=true,
showspaces=false,
showtabs=true,
tabsize=3,
upquote=true,
aboveskip=2pt,
belowskip=2pt,
framexleftmargin=2pt,
extendedchars=true,
inputencoding=utf8
}
\definecolor{MyGray}{rgb}{0.8,0.8,0.8}
\NewTotalTCBox{\CD}{v}{verbatim,colback=MyGray}{\lstinline[]^#1^}
\tcbset{on line,
boxsep=3pt, left=0pt,right=0pt,top=0pt,bottom=0pt,
colframe=white,colback=MyGray,
highlight math style={enhanced}
}
\begin{document}
\section{Highlight}
There is a \CD{for} loop. It uses a \CD{std::vector}. A variable is named \CD{i} and \CD{j} is used. \tcbox{variable}, \tcbox{i}, \tcbox{j}
\end{document}
答案2
需要\lstinline
两个符号之间的参数,例如\lstinline!for!
,其中!
不在参数中。因此,您希望命令执行的操作是参数\Colorbox{MyGray}{\lstinline!for!}
在哪里for
(如果我理解您的问题正确的话)。要执行该命令,您首先需要扩展参数,然后\lstinline
。因此,其中一个
\newcommand\CD[1]{\Colorbox{MyGray}{\expandafter\lstinline\expandafter!#1!}}
\newcommand\CDa[1]{\Colorbox{MyGray}{\expandafter\lstinline#1}}
它们可以用符号或不用符号来调用。因此,以下代码似乎可以在这里工作
\definecolor{MyGray}{rgb}{0.8,0.8,0.8}
%%\makeatletter
%%\xpretocmd\lstinline{\Colorbox{MyGray}\bgroup\appto\lst@DeInit{\egroup}}{}{}
%%\makeatother
%%\newcommand{\CD}[1]{\lstinline#1}
\newcommand\CD[1]{\Colorbox{MyGray}{\expandafter\lstinline\expandafter!#1!}}
\newcommand\CDa[1]{\Colorbox{MyGray}{\expandafter\lstinline#1}}
\tcbset{on line,
boxsep=3pt, left=0pt,right=0pt,top=0pt,bottom=0pt,
colframe=white,colback=MyGray,
highlight math style={enhanced}
}
\begin{document}
\section{Highlight}
Test \Colorbox{MyGray}{\lstinline!for!} or \CD{for} or \CDa{!for!} or \CDa{$for$}
\end{document}