我想使用 l3regex 实现代码高亮环境

我想使用 l3regex 实现代码高亮环境

我想使用l3regex实现一个代码高亮环境,代码如下:

\documentclass{article}
\usepackage{xcolor}
\begin{document}
\ExplSyntaxOn
\cs_set:Npn \highlight_cmd:n #1 {
  \str_set:Nn \l_tmpa_str
  {#1}
\regex_replace_all:nnN{\\clist}{\c{textcolor}{cyan}{\0}}\l_tmpa_str
\str_use:N \l_tmpa_str  
}
\cs_set_eq:NN \highlight \highlight_cmd:n 
\ExplSyntaxOff
\highlight{
  \cs_argument_spec:N
  \clist_clear:N
  \clist_clear_new:N
}
\end{document}

演出 在此处输入图片描述

但我想要 在此处输入图片描述

答案1

请参阅下面的示例。您可以使用它将fancyvrb逐字内容保存到外部文件。然后,您可以逐行读取文件并应用格式。

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}
\usepackage{xcolor}

% change the VerbatimOut environment for our purposes
% save the verbatim content to \jobname-HiLite.vrb so that they can be read back and processed line by line
\makeatletter
\def\HiLite{\FV@Environment{}{HiLite}}
\def\FVB@HiLite{%
  \@bsphack
  \begingroup
    \FV@UseKeyValues
    \FV@DefineWhiteSpace
    \def\FV@Space{\space}%
    \FV@DefineTabOut
    \def\FV@ProcessLine{\immediate\write\FV@OutFile}%
    \immediate\openout\FV@OutFile \jobname-HiLite.vrb\relax % this is the output filename
    \let\FV@FontScanPrep\relax
%% DG/SR modification begin - May. 18, 1998 (to avoid problems with ligatures)
    \let\@noligs\relax
%% DG/SR modification end
    \FV@Scan}
    
% call the procesing callback when the environment ends
\def\FVE@HiLite{\immediate\closeout\FV@OutFile\endgroup\@esphack\HiLiteCallback} 
\DefineVerbatimEnvironment{HiLite}{HiLite}{}
\makeatother

\ExplSyntaxOn

\ior_new:N \g_hilite_ior

\cs_new:Npn \HiLiteCallback {
  % read back the file
  \ior_open:Nn \g_hilite_ior {\jobname-HiLite.vrb}
  % process each line
  \ior_str_map_inline:Nn \g_hilite_ior {
    
      \str_set:Nn \l_tmpa_str {##1}
      \regex_replace_all:nnN {\\clist} {\c{textcolor}\cB{cyan\cE}\cB{\0\cE}} \l_tmpa_str
      % optional: use a different font
      \group_begin:
      \ttfamily
      \par \tl_use:N \l_tmpa_str  
      \group_end:
  }
  \ior_close:N \g_hilite_ior
}

\ExplSyntaxOff

\begin{document}

\begin{HiLite}
  \cs_argument_spec:N
  \clist_clear:N
  \clist_clear_new:N
\end{HiLite}


\end{document}

使用xparse

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\highlight}{+v}{\hilite:n {#1}}

\ior_new:N \g_hilite_ior

\cs_new:Npn \hilite_format_line:n #1 {
    \str_set:Nn \l_tmpb_str {#1}
    \regex_replace_all:nnN {\\clist} {\c{textcolor}\cB{cyan\cE}\cB{\0\cE}} \l_tmpb_str
    % optional: use a different font
    \group_begin:
    \ttfamily
    \par \tl_use:N \l_tmpb_str  
    \group_end:
}

\cs_new:Npn \hilite:n #1 {
  \regex_split:nnN {\^^M} {#1} \l_tmpa_seq
  \seq_show:N \l_tmpa_seq
  \int_set:Nn \l_tmpa_int {1}
  \seq_map_inline:Nn \l_tmpa_seq {
    \str_set:Nn \l_tmpa_str {##1}
    
    \str_if_empty:NTF \l_tmpa_str {
      % skip the first and last empty line
      \bool_if:nF { \int_compare_p:n { \l_tmpa_int = 1 } || \int_compare_p:n { \l_tmpa_int = \seq_count:N \l_tmpa_seq } } {
        \par\null
      }
    } {
      \exp_args:NV \hilite_format_line:n \l_tmpa_str
    }
    
    % increment line counter
    \int_incr:N \l_tmpa_int
  }
}

\ExplSyntaxOff

\begin{document}

Content
\highlight{
  \cs_argument_spec:N
  \clist_clear:N
  \clist_clear_new:N
  
  
  \clist_something
}
Content

\end{document}

相关内容