key_define 选项

key_define 选项

考虑以下 MWE:

在此处输入图片描述

\documentclass[letterpaper]{article}
\usepackage{l3draw}
\ExplSyntaxOn
%----------------------------------------------------------------------------------------
%               Math Icon Command Defintions
%---------------------------------------------------------------------------------------- 
% user interface
\NewDocumentCommand{\mibullseye}{ O{} }
 {
  \group_begin:
  \mathicon_bullseye:n { #1 }
  \group_end:
 }

% variables and variants
\cs_generate_variant:Nn \color_fill:n { V }     %color fill variant
\cs_generate_variant:Nn \color_select:n { V }   %color selection variant

\dim_new:N \l__mathicon_size_dim
\fp_new:N \l__mathicon_linethickness_fp

\keys_define:nn { mathicon }
 {
  size  .dim_set:N = \l__mathicon_size_dim,
  color .tl_set:N  = \l__mathicon_color_tl,
  color .initial:n = black,
  angle .fp_set:N  = \l__mathicon_angle_fp,
  angle .initial:n = 110,
  thick .dim_set:N  = \l__mathicon_thick_dim,
 }

% internal implementation
\cs_new_protected:Npn \mathicon_circle_origin:n #1
  { \draw_path_circle:nn { 0 , 0 } {#1} }
%----------------------------------------------------------------------------------------
%               Bullseye
%----------------------------------------------------------------------------------------
\cs_new_protected:Nn \mathicon_bullseye:n
  {
    \keys_set:nn { mathicon } { size=1ex, thick=0.08ex, angle=45, #1 }
    \draw_begin:
    \draw_cap_round:
    \draw_join_round:
    \draw_linewidth:n { \l__mathicon_thick_dim }
    
    \dim_step_function:nnnN { 0.5\l__mathicon_size_dim } 
                            { 0.25\l__mathicon_size_dim } 
                            { 1.0\l__mathicon_size_dim }
                            \mathicon_circle_origin:n
    \color_select:V { \l__mathicon_color_tl }
    \draw_path_use_clear:n { stroke }
    
    \draw_path_circle:nn { 0 , 0 }{ 0.250\l__mathicon_size_dim }
    \draw_path_use_clear:n { fill , stroke }
            
    \draw_scope_begin:
    \draw_transform_rotate:n { \l__mathicon_angle_fp } % angle
        \group_begin:
            \draw_linewidth:n   { 3.000\l__mathicon_thick_dim }
            \draw_path_moveto:n { 0.000\l__mathicon_size_dim , 0.000\l__mathicon_size_dim }
            \draw_path_lineto:n { 1.250\l__mathicon_size_dim , 0.000\l__mathicon_size_dim }
            \color_select:n { white }
            \draw_path_use_clear:n { stroke }
        \group_end:
        \group_begin:
            \draw_linewidth:n { 1.000\l__mathicon_thick_dim }
            \draw_path_moveto:n { 0.000\l__mathicon_size_dim , 0.000\l__mathicon_size_dim }
            \draw_path_lineto:n { 1.250\l__mathicon_size_dim , 0.000\l__mathicon_size_dim }
            \draw_path_use_clear:n { stroke }
        \group_end:
        \group_begin:
            \draw_transform_matrix:nnnn { 1 } { 0 } { 0.75 } { 1 }
            \draw_path_moveto:n { 1.125\l__mathicon_size_dim , 0.125\l__mathicon_size_dim }
            \draw_path_lineto:n { 1.450\l__mathicon_size_dim , 0.125\l__mathicon_size_dim }
            \draw_path_lineto:n { 1.450\l__mathicon_size_dim , 0.225\l__mathicon_size_dim }
            \draw_path_lineto:n { 1.125\l__mathicon_size_dim , 0.225\l__mathicon_size_dim }
            \draw_path_close:
            \draw_path_use_clear:n { fill, stroke }
        \group_end:
        \group_begin:
            \draw_transform_matrix:nnnn { 1 } { 0 } { 0.75 } { -1 }
            \draw_path_moveto:n { 1.125\l__mathicon_size_dim , 0.125\l__mathicon_size_dim }
            \draw_path_lineto:n { 1.450\l__mathicon_size_dim , 0.125\l__mathicon_size_dim }
            \draw_path_lineto:n { 1.450\l__mathicon_size_dim , 0.225\l__mathicon_size_dim }
            \draw_path_lineto:n { 1.125\l__mathicon_size_dim , 0.225\l__mathicon_size_dim }
            \draw_path_close:
            \draw_path_use_clear:n { fill, stroke }
        \group_end:
    \draw_scope_end:
    \draw_end:
  }
%----------------------------------------------------------------------------------------  
\ExplSyntaxOff
\begin{document}
    Sample \mibullseye %\mibullseye[size=3ex, thick=1pt]
\end{document}

有几件事我需要帮助或审查代码:

  1. 你能找出代码失败的原因吗?并提出改进建议。我尝试使用命令\draw_scope_begin:\draw_scope_end:
  2. 我觉得实际的箭头可以以更简单的形式绘制,也许可以稍微压缩一下代码。请注意,我以前使用的是相同的线,但粗细不同。:)

  1. 我如何创建一个键值选项,例如noarrow只绘制圆圈​​?例如,只需写入\mibullseye[noarrow]

问题 3是我真正的问题。

答案1

下面noarrow通过设置布尔变量来添加选项。您只需评估它即可决定是否应该绘制箭头。我还通过将代码双元组移到函数内部来删除它们。这样您的箭头代码就会变得更加简洁。

\begin_group:/\end_group:此外,我还将所有这些对都更改为\draw_scope_begin:/\draw_scope_end:看起来适合在这里使用的正确对。

\documentclass[letterpaper]{article}
\usepackage{l3draw}
\ExplSyntaxOn
%-------------------------------------------------------------------------------
%               Math Icon Command Defintions
%-------------------------------------------------------------------------------
% user interface
\NewDocumentCommand{\mibullseye}{ O{} }
 {
  \group_begin:
  \mathicon_bullseye:n { #1 }
  \group_end:
 }

% variables and variants
\cs_generate_variant:Nn \color_fill:n { V }     %color fill variant
\cs_generate_variant:Nn \color_select:n { V }   %color selection variant

\dim_new:N \l__mathicon_size_dim
\fp_new:N \l__mathicon_linethickness_fp

\keys_define:nn { mathicon }
 {
  size  .dim_set:N = \l__mathicon_size_dim,
  color .tl_set:N  = \l__mathicon_color_tl,
  color .initial:n = black,
  angle .fp_set:N  = \l__mathicon_angle_fp,
  angle .initial:n = 110,
  thick .dim_set:N  = \l__mathicon_thick_dim,
  noarrow .bool_set:N = \l__mathicon_no_arrow_bool,
  noarrow .default:n = true,
 }

% internal implementation
\cs_new_protected:Npn \mathicon_circle_origin:n #1
  { \draw_path_circle:nn { 0 , 0 } {#1} }
%-------------------------------------------------------------------------------
%               Bullseye
%-------------------------------------------------------------------------------
\cs_new_protected:Npn \__mathicon_arrow_shaft_path:n #1
  {
    \draw_linewidth:n   { #1 \l__mathicon_thick_dim }
    \draw_path_moveto:n { 0 , 0 }
    \draw_path_lineto:n { 1.25\l__mathicon_size_dim , 0 }
  }
\cs_new_protected:Npn \__mathicon_arrow_feathers_path:
  {
    \draw_path_moveto:n
      { 1.125\l__mathicon_size_dim , 0.125\l__mathicon_size_dim }
    \draw_path_lineto:n
      { 1.45 \l__mathicon_size_dim , 0.125\l__mathicon_size_dim }
    \draw_path_lineto:n
      { 1.45 \l__mathicon_size_dim , 0.225\l__mathicon_size_dim }
    \draw_path_lineto:n
      { 1.125\l__mathicon_size_dim , 0.225\l__mathicon_size_dim }
    \draw_path_close:
  }
\cs_new_protected:Npn \mathicon_bullseye:n #1
  {
    \keys_set:nn { mathicon } { size=1ex, thick=0.08ex, angle=45, #1 }
    \draw_begin:
      \draw_cap_round:
      \draw_join_round:
      \draw_linewidth:n { \l__mathicon_thick_dim }
      %
      \dim_step_function:nnnN { 0.5\l__mathicon_size_dim } 
                              { 0.25\l__mathicon_size_dim } 
                              { \l__mathicon_size_dim }
                              \mathicon_circle_origin:n
      \color_select:V \l__mathicon_color_tl
      \draw_path_use_clear:n { stroke }
      %
      \draw_path_circle:nn { 0 , 0 }{ 0.25\l__mathicon_size_dim }
      \draw_path_use_clear:n { fill , stroke }
      %
      \bool_if:NF \l__mathicon_no_arrow_bool
        {
          \draw_scope_begin:
            \draw_transform_rotate:n { \l__mathicon_angle_fp } % angle
            \draw_scope_begin:
              \__mathicon_arrow_shaft_path:n { 3 }
              \color_select:n { white }
              \draw_path_use_clear:n { stroke }
            \draw_scope_end:
            \draw_scope_begin:
              \__mathicon_arrow_shaft_path:n { 1 }
              \draw_path_use_clear:n { stroke }
            \draw_scope_end:
            \draw_scope_begin:
              \draw_transform_matrix:nnnn { 1 } { 0 } { 0.75 } { 1 }
              \__mathicon_arrow_feathers_path:
              \draw_path_use_clear:n { fill, stroke }
            \draw_scope_end:
            \draw_scope_begin:
              \draw_transform_matrix:nnnn { 1 } { 0 } { 0.75 } { -1 }
              \__mathicon_arrow_feathers_path:
              \draw_path_use_clear:n { fill, stroke }
            \draw_scope_end:
          \draw_scope_end:
        }
    \draw_end:
  }
%-------------------------------------------------------------------------------
\ExplSyntaxOff

\begin{document}
Sample \mibullseye

Sample \mibullseye[size=3ex, thick=1pt]

Sample \mibullseye[noarrow]
\end{document}

在此处输入图片描述

相关内容