lstinline 주위에 둥근 tcolorbox를 감싸는 방법은 무엇입니까?

lstinline 주위에 둥근 tcolorbox를 감싸는 방법은 무엇입니까?

주변에 상자를 두고 싶습니다 \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에는 p.2에 예제가 있습니다. 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}

관련 정보