在 tcolorbox 下绘制的代码高亮不显示

在 tcolorbox 下绘制的代码高亮不显示

我正在寻求为列表添加突出显示。我尝试过以下技术:https://tex.stackexchange.com/users/23765/siracusa在问题中突出显示代码列表中的文本,同时保持语法突出显示

但是我遇到了一个问题,如下面的示例所示。我希望我的列表位于 tcolorbox 中。当它不在 tcolorbox 中时,突出显示会按预期工作。当它在框内时,突出显示会正确呈现在正确的位置,但它不会显示,因为 tcolorbox 似乎绘制在它上面。

有没有办法在框顶部(但仍在文本下方)绘制突出显示?我真的很想保留突出显示解决方案的核心,因为它与 listings 包配合得很好。

还要注意的是,任何涉及手动调整单个代码块的解决方案都不是一个选择。它们的内容是由另一个程序生成的,而且有数千个。

\LoadClass[10pt]{book}
\RequirePackage{fontspec}
\RequirePackage{listings}
\RequirePackage{xcolor}
\RequirePackage{atbegshi}
\RequirePackage{ifthen}
\RequirePackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{fit}
\RequirePackage[breakable]{tcolorbox}

% HIGHLIGHTING
\makeatletter

\newcommand\if@empty[1]{%
    \if\relax\detokenize{#1}\relax
    \expandafter\@firstoftwo
    \else
    \expandafter\@secondoftwo
    \fi
}

\newcommand\ifthen[2]{\ifthenelse{#1}{#2}{}}
\newcommand\ifelse[2]{\ifthenelse{#1}{}{#2}}

% Is highlighting currently active in the listings environment?
\newif\ifhl@active

% Material executed at each page shipout
\newtoks\hl@shipout

% Extra styling parameters for the currently active highlight
\newtoks\hl@parameters

% Inline highlight style. This does not break across line ends
\newcommand\hl[2][]{\begingroup\hlstyle@start{#1}#2\endgroup}

% The highlight style for listings environments
\newcommand\hlstyle[1][]{\hlstyle@start{#1}}
\newcommand\hlstyle@start[1]{%
    \global\hl@activetrue
    \aftergroup\hlstyle@end
    \write\@auxout{\noexpand\hl@setparameters{#1}}%
    \hl@mark{S}%
}
\newcommand\hlstyle@end{%
    \hl@mark{E}%
    \global\hl@activefalse
}

% Sets the current highlight parameters
\newcommand\hl@setparameters[1]{%
    \global\hl@parameters={#1}%
}

% Writes a new node marker with the current position and page number to the .aux file
\newcommand\hl@mark[1]{%
    \ensuremath{\vcenter{\hbox{\pdfsavepos}}}%
    \write\@auxout{\noexpand\hl@processmark{#1}{\the\pdflastxpos}{\the\pdflastypos}{\arabic{page}}}%
}

% Writes the current page origin to the .aux file, and issues the current \hl@shipout material
% to be processed
\AtBeginShipout{\AtBeginShipoutUpperLeft{%
        \pdfsavepos
        \write\@auxout{\noexpand\hl@processmark{Z}{\the\pdflastxpos}{\the\pdflastypos}{\arabic{page}}}%
        \hl@doshipout
}}

% Maintains the current first and last node positions, checks whether a node marker should be used
% for the current highlighting segment, and issues a \hl@draw call if necessary.
% #1 is either 'S' (start node), 'M' (intermediate node), 'E' (end node), or 'Z' (page origin)
% #2/#3 are the x/y-coordinates, #4 is the page number
\newcommand\hl@processmark[4]{%
    \if#1S%
    \xdef\hl@firstx{#2}%
    \xdef\hl@firsty{#3}%
    \xdef\hl@firstp{#4}%
    \else\if#1M%
    \ifthenelse{\hl@firsty=#3}{%
        % Still on same line
        \xdef\hl@lastx{#2}%
        \xdef\hl@lasty{#3}%
    }{%
        \ifelse{\hl@firstx=\hl@lastx \and \hl@firsty=\hl@lasty}{%
            % Last highlight part is not empty
            \expandafter\hl@draw\expandafter{\the\hl@parameters}%
        }%
        \xdef\hl@firstx{#2}%
        \xdef\hl@firsty{#3}%
        \xdef\hl@firstp{#4}%
    }%
    \else\if#1E%
    \ifthen{\hl@firsty=#3}{%
        % Still on same line
        \xdef\hl@lastx{#2}%
        \xdef\hl@lasty{#3}%
        \ifelse{\hl@firstx=#2}{%
            % Last highlight part is not empty
            \expandafter\hl@draw\expandafter{\the\hl@parameters}%
        }%
    }%
    \else\if#1Z%
    \xdef\hl@zerox{#2}%
    \xdef\hl@zeroy{#3}%
    \fi\fi\fi\fi
}

% Add a new highlight segment to \hl@shipout to be processed on every shipout.
% #1 is the styling command used to draw the segment within a properly positioned \tikz call.
%    If #1 is empty, \hldefaultstyle is used as a default. Every styling command can take
%    arbitrary parameters, only the last four are mandatory (x_first, y_first, x_last, y_last)
\newcommand\hl@draw[1]{%
    \edef\@temp{{\noexpand\tikz [ overlay, shift = {(-\hl@zerox sp, -\hl@zeroy sp)} ]
            {\if@empty{#1}{\noexpand\hldefaultstyle}{\unexpanded{#1}}%
                {\hl@firstx sp}{\hl@firsty sp}{\hl@lastx sp}{\hl@lasty sp}}}%
        {\hl@firstp}%
    }%
    \expandafter\global\expandafter\addto@hook\expandafter\hl@shipout\expandafter{\@temp}%
}

% Processes the current \hl@shipout material. The material is a sequence of
% {TikZ commands}{page number} pairs
\def\hl@doshipout{%
    \expandafter\hl@doshipout@\the\hl@shipout{}{}\@end
}
\def\hl@doshipout@#1#2#3\@end{%
    \if@empty{#2}{}{%
        \ifthen{\arabic{page}=#2}{#1}%
        \hl@doshipout@#3\@end
    }%
}

% Hook into listings' discretionary breaks
\let\orig@lst@discretionary=\lst@discretionary
\gdef\lst@discretionary{%
    \ifhl@active
    \hl@mark{M}%
    \fi
    \orig@lst@discretionary
    \ifhl@active
    \hl@mark{M}%
    \fi
}



% Styling command for a simple marker highlights
\newcommand\hlmarkerstyle[5][yellow]{%
    \draw [ line width = \baselineskip, #1 ] (#2, #3) -- (#4, #5);
}


% Default styling command
\let\hldefaultstyle=\hlmarkerstyle

\makeatother

\newcommand\delimstyleB{\hlstyle}

%ENDHIGHLIGHTING



%% Define custom verbatim as listing allowing background color and mark notation
\let\verbatim\someundefinedcommand
\lstnewenvironment{CustomVerbatim}[1][name=main]
{
    \lstset{
        basicstyle=\fontsize{8}{8}\ttfamily,
        moredelim=**[is][\delimstyleB]{`}{`},
        aboveskip=3mm,
        breaklines=true,
        breakatwhitespace=false,
        keepspaces=true,
        extendedchars=true,
        xleftmargin=8pt,
        xrightmargin=8pt,
        framextopmargin=3pt,
        framexbottommargin=3pt,
        frame=tb,
        framerule=0pt,
        escapeinside={¬}{¬},
        postbreak=\mbox{\textcolor{blue}{$\hookrightarrow$}\space},
        #1
    }
}
{}

\newtcolorbox{codebox}[1][]
{
    #1,
    breakable,
    size=fbox,
    arc=0mm,
    colback=red,
    colframe=red,
}

\begin{document}


\begin{codebox}\begin{CustomVerbatim}%

The text of this `codeblock should be partially highlighted. However, the red box is blocking the highlighting.`

\end{CustomVerbatim}\end{codebox}



\begin{CustomVerbatim}%

The text of `this codeblock is properly partially highlighted, even across multiple lines because it` isn't in a tcolorbox

\end{CustomVerbatim}

\end{document}

结果页面

相关内容