Como envolver o tcolorbox arredondado em torno do lstinline?

Como envolver o tcolorbox arredondado em torno do lstinline?

Eu gostaria de ter caixas por perto \lstinline. Em outra pergunta, encontrei o \xpretocmdtruque \Colorbox, mas não consigo aplicá-lo \tcbox. Há algum caminho?

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

insira a descrição da imagem aqui

Responder1

A tcolorboxdocumentação tem um exemplo na pág. 21 que mostra como usar \NewTotalTCBoxcom \lstinline. Adaptado ao seu 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}

insira a descrição da imagem aqui

Responder2

O \lstinlinequer o argumento entre dois símbolos, como \lstinline!for!, onde !não está no argumento. Então, o que você deseja que seu comando faça é \Colorbox{MyGray}{\lstinline!for!}onde forestá o argumento (se bem entendi sua pergunta). Para fazer esse comando você primeiro precisa expandir o argumento e depois \lstinline. Então um dos

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

Eles são chamados com ou sem símbolo. Portanto, o código a seguir parece funcionar aqui

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

informação relacionada