¿Cómo envolver tcolorbox redondeado alrededor de lstinline?

¿Cómo envolver tcolorbox redondeado alrededor de lstinline?

Me gustaría tener cajas alrededor \lstinline. En otra pregunta encontré el \xpretocmdtruco con \Colorbox, pero no logro aplicarlo con \tcbox. ¿Hay alguna manera?

\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}

ingrese la descripción de la imagen aquí

Respuesta1

La tcolorboxdocumentación tiene un ejemplo en la p. 21 que muestra cómo usarlo \NewTotalTCBoxcon \lstinline. Adaptado a tu caso:

\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}

ingrese la descripción de la imagen aquí

Respuesta2

Quiere \lstinlineel argumento entre dos símbolos, como \lstinline!for!, donde !no está en el argumento. Entonces, lo que desea que haga su comando es \Colorbox{MyGray}{\lstinline!for!}dónde forestá el argumento (si entiendo bien su pregunta). Para hacer ese comando primero necesitas expandir el argumento y luego \lstinline. Así que uno de

\newcommand\CD[1]{\Colorbox{MyGray}{\expandafter\lstinline\expandafter!#1!}}
\newcommand\CDa[1]{\Colorbox{MyGray}{\expandafter\lstinline#1}}

Se llaman con o sin símbolo. Entonces el siguiente código parece funcionar aquí

\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}

información relacionada