我已经构建了一个将 minted 与 相结合的命令tcolorbox
,但我遇到了 SASS 语法问题。这是一个最小可重现的示例:
\documentclass[a4paper,headinclude=on,footinclude=on,12pt,oneside]{scrbook}
\usepackage[dvipsnames]{xcolor}
\usepackage{color}
\usepackage{fontawesome}
\usepackage{minted}
\usepackage[most,minted]{tcolorbox}
\tcbuselibrary{listings,minted,breakable}
% custom colors
\definecolor{codebackground}{HTML}{F2F2F2}
% for fancy spacing of code snippet title bars
\usepackage{tabularx}
\newcolumntype{\CeX}{>{\centering\let\newline\\\arraybackslash}X}%
\newcommand{\TwoSymbolsAndText}[3]{%
\begin{tabularx}{\textwidth}{c\CeX c}%
#1 & #2 & #3
\end{tabularx}%
}
\AtBeginDocument{
\newtcblisting[blend into=listings]{codeInput}[3]{
listing engine=minted,
minted language=#1,
minted options={breaklines,breaksymbolleft=,breaksymbolright=,fontsize=\footnotesize},
listing only,
listing remove caption=true,
size=title,
arc=1.5mm,
breakable,
enhanced jigsaw,
colframe=Black,
coltitle=White,
boxrule=0.5mm,
colback=codebackground,
coltext=Black,
title=\TwoSymbolsAndText{\faCode}{%
\footnotesize{\texttt{#2}}
}{\faCode},
list text=#3
}
}
\begin{document}
\begin{codeInput}{sass}{\_toasts.scss}{A small SASS file.}
.Toastify__progress-bar--default {
background: \$primary !important;
}
\end{codeInput}
\end{document}
产生以下 PDF 输出:
请注意,括号、美元符号和分号点周围都标有红色边框 - 但这是正确的 SASS 语法。(除了美元符号前面的斜线(我需要它以使 LaTeX 不抱怨)之外 - 我想一个子问题是我如何$
在这里的源代码中转义!)
我该如何修复这个语法高亮问题?
答案1
您可以重新定义\PYG@tok@err
:
\AddToHook{cmd/minted@addcachefile/after}{%
\@namedef{PYG@tok@err}{\def\PYG@bc##1{##1}}}
在您的代码中:
\documentclass[a4paper,headinclude=on,footinclude=on,12pt,oneside]{scrbook}
\usepackage[dvipsnames]{xcolor}
\usepackage{color}
\usepackage{fontawesome}
\usepackage{minted}
\usepackage[most,minted]{tcolorbox}
\tcbuselibrary{listings,minted,breakable}
% custom colors
\definecolor{codebackground}{HTML}{F2F2F2}
% for fancy spacing of code snippet title bars
\usepackage{tabularx}
\newcolumntype{\CeX}{>{\centering\let\newline\\\arraybackslash}X}%
\newcommand{\TwoSymbolsAndText}[3]{%
\begin{tabularx}{\textwidth}{c\CeX c}%
#1 & #2 & #3
\end{tabularx}%
}
\AtBeginDocument{
\newtcblisting[blend into=listings]{codeInput}[3]{
listing engine=minted,
minted language=#1,
minted options={breaklines,breaksymbolleft=,breaksymbolright=,fontsize=\footnotesize},
listing only,
listing remove caption=true,
size=title,
arc=1.5mm,
breakable,
enhanced jigsaw,
colframe=Black,
coltitle=White,
boxrule=0.5mm,
colback=codebackground,
coltext=Black,
title=\TwoSymbolsAndText{\faCode}{%
\footnotesize{\texttt{#2}}
}{\faCode},
list text=#3
}
}
\makeatletter
\AddToHook{cmd/minted@addcachefile/after}{%
\@namedef{PYG@tok@err}{\def\PYG@bc##1{##1}}}
\makeatother
\begin{document}
\begin{codeInput}{sass}{\_toasts.scss}{A small SASS file.}
.Toastify__progress-bar--default {
background: $primary !important;
}
\end{codeInput}
\end{document}
注意:您需要$
而不是\$
。