Как обернуть скругленный tcolorbox вокруг lstinline?

Как обернуть скругленный tcolorbox вокруг lstinline?

Я бы хотел иметь коробки вокруг \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}

Связанный контент