声明TCBListing并写入百分比字符(expl3)

声明TCBListing并写入百分比字符(expl3)

这个问题与声明TCBListing并将百分比写入文件(感谢薛定谔的猫)但方法不同,我想我知道发生了什么:

当将字符写入文件时,该tcolorbox包会对其进行特殊处理,此示例运行正常:%

\documentclass[10pt]{article}
\usepackage{tcolorbox}
\tcbuselibrary{listings,breakable,skins,xparse}
\DeclareTCBListing{mytcblisting}{ O{} }
  {%
     bottom=-1mm, boxrule=0.4pt, boxsep=0mm, top=-1mm, arc=0mm,%
     title style={draw=none,fill=none},
      enhanced, autoparskip, 
    freelance,#1%
  }
\begin{document}
% OK, from doc pag 130
\def\percent{\%}
\begin{tcblisting}{title=Normal}
%\begin{center}\bfseries
This is my 5\percent\ text
and this is my 10\% text.
%\end{center}
\end{tcblisting}

% No rror

\begin{mytcblisting}
%\begin{center}\bfseries
This is my 5\percent\ text
and this is my 10\% text.
%\end{center}
\end{mytcblisting}

\end{document}

但是,通过传递内部代码\ExplSyntaxOn并且\ExplSyntaxOff这种特殊处理丢失,此示例失败:

平均能量损失

\documentclass[10pt]{article}
\usepackage{tcolorbox}
\tcbuselibrary{listings,breakable,skins,xparse}
\def\percent{\%}
% Colors
\definecolor{colKeys}{rgb}{0,0,1}
\definecolor{colIdentifier}{rgb}{0,0,0}
\definecolor{colComments}{rgb}{1,0,0}
\definecolor{colString}{rgb}{0,0.5,0}
% Listings
\lstdefinestyle{example}{
  basicstyle=\ttfamily\small, %
  identifierstyle=\color{colIdentifier}, %
  keywordstyle=\color{colKeys}, %
  stringstyle=\color{colString}, %
  commentstyle=\color{colComments}, %
  numbersep=3pt,%
  numberstyle=\tiny\color{red}%
}
\ExplSyntaxOn
\bool_new:N \l_example_margin_left_bool
\tl_new:N \l_example_position_tl
\tl_new:N \l_example_haling_tl
\keys_define:nn { examplekeys }
  {
    codefile .code:n = \tl_set:Nn \l_example_codefile_tl {#1},
    lstconf  .code:n = \tl_set:Nn \l_example_lstconf_tl {#1},
    fcolor   .code:n = \tl_set:Nn \l_example_fcolor_tl {#1},
    bcolor   .code:n = \tl_set:Nn \l_example_bcolor_tl {#1},
    hsep     .code:n = \tl_set:Nn \l_example_hsep_tl {#1},
    vsep     .code:n = \tl_set:Nn \l_example_vsep_tl {#1},
    width    .code:n = \tl_set:Nn \l_example_width_tl {#1},
    align    .choice:  ,
    align/j  .code:n = \tl_set:Nn \l_example_haling_tl { justify },
    align/l  .code:n = \tl_set:Nn \l_example_haling_tl { flush~left } ,
    align/r  .code:n = \tl_set:Nn \l_example_haling_tl { flush~right } ,
    align/c  .code:n = \tl_set:Nn \l_example_haling_tl { flush~center } ,
    pos      .choice:  ,
    pos/r    .code:n = {
                         \bool_set_true:N \l_example_margin_left_bool
                         \tl_set:Nn \l_example_position_tl { listing~outside~text }
                       },
    pos/l    .code:n = {
                         \bool_set_false:N \l_example_margin_left_bool
                         \tl_set:Nn \l_example_position_tl { text~outside~listing }
                       },
    pos/t    .code:n = \tl_set:Nn \l_example_position_tl { listing~above~text } ,
    pos/b    .code:n = \tl_set:Nn \l_example_position_tl { text~above~listing } ,
  }

\keys_set:nn { examplekeys }
  {
    codefile = \jobname-tmp.ltx,
    lstconf  = { style=example,numbers=left },
    fcolor   = red!75!black,
    bcolor   = red!5!white,
    hsep     = 5mm,
    vsep     = 5mm,
    width    = 0.5\linewidth,
    pos      = l,
    align    = j,
  }
\DeclareTCBListing{mytcblisting}{ O{} }
  {%
    bottom=-1mm, boxrule=0.4pt, boxsep=0mm, top=-1mm, arc=0mm,%
    title~style={draw=none,fill=none}, enhanced, autoparskip, freelance,%
    %listing~options={ \tl_use:N \l_example_lstconf_tl },%
    listing~options = {style=example,numbers=left},
    listing~file    = \tl_use:N \l_example_codefile_tl,
    sidebyside~gap  = \tl_use:N \l_example_hsep_tl,
    middle          = \tl_use:N \l_example_vsep_tl,
    halign~lower    = \tl_use:N \l_example_haling_tl,
    colframe        = \tl_use:N \l_example_fcolor_tl,
    colback         = \tl_use:N \l_example_bcolor_tl,
    \tl_use:N \l_example_position_tl,
  }
\NewDocumentEnvironment{ example }{ O{} }
  {
    \group_begin:
    \IfNoValueF{#1}{ \keys_set:nn { examplekeys } { #1 } }
    \bool_if:NTF { \l_example_margin_left_bool }
      { \tcbset{ righthand~width = \tl_use:N \l_example_width_tl } }
      { \tcbset{ lefthand~width  = \tl_use:N \l_example_width_tl }  }
    \mytcblisting
  }
  {
    \endmytcblisting
    \group_end:
  }

\ExplSyntaxOff
\begin{document}

\begin{tcblisting}{title=Normal}
%\begin{center}\bfseries
This is my 5\percent\ text
and this is my 10\% text.
%\end{center}
\end{tcblisting}

\begin{example}[fcolor=blue,width=3cm, pos=t, vsep=2mm, align=j]
%\begin{center}\bfseries
This is my 5\percent\ text
and this is my 10\% text.
%\end{center}
\end{example}

% no [opt]
\begin{example}
Some
text
in
some
lines
\end{example}

\begin{example}[pos=r,width = 3cm]
This is my
\LaTeX\ box.
\end{example}

\end{document}

我该如何定义%才能得到正确的对待?

答案1

您正在定义mytcblisting查找可选参数,但该参数显然不存在,因为该可选参数已被使用example。查找它会导致初始值%过早被标记化。

\NewTCBListing{mytcblisting}{ }
  {
    bottom=-1mm, boxrule=0.4pt, boxsep=0mm, top=-1mm, arc=0mm,
    title~style={draw=none,fill=none}, enhanced, autoparskip, freelance,
    %listing~options={ \tl_use:N \l_example_lstconf_tl },
    listing~options = {style=example,numbers=left},
    listing~file    = \tl_use:N \l_example_codefile_tl,
    sidebyside~gap  = \tl_use:N \l_example_hsep_tl,
    middle          = \tl_use:N \l_example_vsep_tl,
    halign~lower    = \tl_use:N \l_example_haling_tl,
    colframe        = \tl_use:N \l_example_fcolor_tl,
    colback         = \tl_use:N \l_example_bcolor_tl,
    \tl_use:N \l_example_position_tl,
  }

你也应该这样做

\NewDocumentEnvironment{ example }{ !O{} }
  {
    \IfNoValueF{#1}{ \keys_set:nn { examplekeys } { #1 } }
    \bool_if:NTF { \l_example_margin_left_bool }
      { \tcbset{ righthand~width = \tl_use:N \l_example_width_tl } }
      { \tcbset{ lefthand~width  = \tl_use:N \l_example_width_tl }  }
    \mytcblisting
  }
  {
    \endmytcblisting
  }

因此[必须在 旁边\begin{example},并且缺少可选参数不会导致初始的标记化%

我删除了无用的部分\group_begin:和匹配的部分\group_end:(环境会自动提供它们)。

相关内容