使用 tcbox 和 mintedinline 進行內嵌程式碼突出顯示的問題 - 將 # 渲染為 ##

使用 tcbox 和 mintedinline 進行內嵌程式碼突出顯示的問題 - 將 # 渲染為 ##

我已經使用 tcbox 和 mintedinline 創建了一個用於內聯代碼突出顯示的命令,但遇到了問題。當我嘗試只寫一個 # 時,它會呈現為 ##。我已經嘗試了 tcbox 的各種選項,但似乎沒有任何方法可以解決問題。有趣的是,當我只使用 mintedinline 時,它按預期工作。因此,我不認為問題出在我的 \setminted 配置中。

任何有關如何解決此問題的見解或建議將不勝感激。

最小乳膠代碼:

\documentclass{article}
\usepackage{xcolor}
\usepackage{minted}
\usepackage{tcolorbox}

\definecolor{codeinlineBlue}{HTML}{CFE2FC}
\newcommand\mystrut{\rule[-2.5pt]{0pt}{12pt}}
\newcommand{\codeinline}[2][text]{%
    \tcbox[
      on line,
      boxsep=0pt,
      left=2pt,
      right=2pt,
      top=0pt,
      bottom=0pt,
      enlarge top initially by=-4pt,
      enlarge bottom by=-4pt,
      enlarge right by=-4pt,
      enlarge left by=-2pt,
      opacityframe=0,
      colback=codeinlineBlue,
      fontupper={\ttfamily\mystrut},
      fontlower={\ttfamily\mystrut}]
      {\mintinline{#1}{#2}}
}

\begin{document}

Text before code \codeinline{int main() \{ return 0; \}}, text after. Problem: \codeinline{#}.
This works as expected: \mintinline{text}{#}

\end{document}

在此輸入影像描述

答案1

您應該逐字收集參數(但您將無法用作\codeinline另一個命令的參數)。為了做到這一點,你使用\NewTotalTCBox.

\documentclass{article}
\usepackage{xcolor}
\usepackage{minted}
\usepackage{tcolorbox}
\tcbuselibrary{xparse}

\definecolor{codeinlineBlue}{HTML}{CFE2FC}
\newcommand\mystrut{\rule[-2.5pt]{0pt}{12pt}}

\NewTotalTCBox{\codeinline}{O{text}v}{
  on line,
  boxsep=0pt,
  left=2pt,
  right=2pt,
  top=0pt,
  bottom=0pt,
  enlarge top initially by=-4pt,
  enlarge bottom by=-4pt,
  enlarge right by=-4pt,
  enlarge left by=-2pt,
  opacityframe=0,
  colback=codeinlineBlue,
  fontupper={\ttfamily\mystrut},
  %fontlower={\ttfamily\mystrut},
}{\mintinline{#1}{#2}}


\begin{document}

Text before code \codeinline{int main() \{ return 0; \}}, 
text after. Problem: \codeinline{#}.
This works as expected: \mintinline{text}{#}

\end{document}

在此輸入影像描述

相關內容