Wie umschließt man eine abgerundete Tcolorbox um eine Lstinline?

Wie umschließt man eine abgerundete Tcolorbox um eine Lstinline?

Ich hätte gerne Kästchen um \lstinline. Aus einer anderen Frage habe ich den \xpretocmdTrick mit gefunden \Colorbox, aber ich schaffe es nicht, ihn mit anzuwenden \tcbox. Gibt es eine Möglichkeit?

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

Bildbeschreibung hier eingeben

Antwort1

Die tcolorboxDokumentation enthält auf Seite 21 ein Beispiel, das die Verwendung \NewTotalTCBoxmit zeigt \lstinline. Angepasst an Ihren Fall:

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

Bildbeschreibung hier eingeben

Antwort2

Das \lstinlinewill das Argument zwischen zwei Symbolen, wie \lstinline!for!, wobei !nicht im Argument ist. Was Sie also mit Ihrem Befehl machen wollen, ist, \Colorbox{MyGray}{\lstinline!for!}wobei fordas Argument ist (wenn ich Ihre Frage richtig verstehe). Um diesen Befehl zu machen, müssen Sie zuerst das Argument erweitern und dann \lstinline. Also einer von

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

Sie werden mit oder ohne Symbol aufgerufen. Der folgende Code scheint hier also zu funktionieren

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

verwandte Informationen