
の周囲にボックスを配置したいと思います。別の質問から のトリック\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
ドキュメントの 21 ページに、のtcolorbox
使用方法を示す例があります。あなたのケースに合わせて調整します。\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
、引数が 2 つの記号の間にあることを必要とします。たとえば、 は\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}