
我想要周圍有盒子\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
文件在第 14 頁上有一個範例。 21 顯示如何使用\NewTotalTCBox
with \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}