TransWikia.com

key_define option

TeX - LaTeX Asked by azetina on November 30, 2020

Consider the following MWE:

enter image description here

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.5l__mathicon_size_dim } 
                            { 0.25l__mathicon_size_dim } 
                            { 1.0l__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.250l__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.000l__mathicon_thick_dim }
            draw_path_moveto:n { 0.000l__mathicon_size_dim , 0.000l__mathicon_size_dim }
            draw_path_lineto:n { 1.250l__mathicon_size_dim , 0.000l__mathicon_size_dim }
            color_select:n { white }
            draw_path_use_clear:n { stroke }
        group_end:
        group_begin:
            draw_linewidth:n { 1.000l__mathicon_thick_dim }
            draw_path_moveto:n { 0.000l__mathicon_size_dim , 0.000l__mathicon_size_dim }
            draw_path_lineto:n { 1.250l__mathicon_size_dim , 0.000l__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.125l__mathicon_size_dim , 0.125l__mathicon_size_dim }
            draw_path_lineto:n { 1.450l__mathicon_size_dim , 0.125l__mathicon_size_dim }
            draw_path_lineto:n { 1.450l__mathicon_size_dim , 0.225l__mathicon_size_dim }
            draw_path_lineto:n { 1.125l__mathicon_size_dim , 0.225l__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.125l__mathicon_size_dim , 0.125l__mathicon_size_dim }
            draw_path_lineto:n { 1.450l__mathicon_size_dim , 0.125l__mathicon_size_dim }
            draw_path_lineto:n { 1.450l__mathicon_size_dim , 0.225l__mathicon_size_dim }
            draw_path_lineto:n { 1.125l__mathicon_size_dim , 0.225l__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}

There are a few things I need assistance or review of the code:

  1. Can you identify ways the code would fail? And suggest improvements. I made an attempt to use the commands draw_scope_begin: and draw_scope_end:
  2. I feel that the actual arrow can be drawn in a much easier form, condense the code a bit, maybe. Note that I used to the same line but different thicknesses. 🙂

  1. How can I create a key value option such as noarrow that only draws the circles? For example, one would simply write mibullseye[noarrow].

Question 3 is my actual question.

One Answer

The following adds the noarrow option by setting a boolean variable. You can just evaluate it to decide whether you should draw the arrow. Also I removed code doublets by moving them inside a function. This way your arrow code becomes much more concise.

Also I've changed all those begin_group:/end_group: pairs to draw_scope_begin:/draw_scope_end: which seem like the correct thing to use here.

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.25l__mathicon_size_dim , 0 }
  }
cs_new_protected:Npn __mathicon_arrow_feathers_path:
  {
    draw_path_moveto:n
      { 1.125l__mathicon_size_dim , 0.125l__mathicon_size_dim }
    draw_path_lineto:n
      { 1.45 l__mathicon_size_dim , 0.125l__mathicon_size_dim }
    draw_path_lineto:n
      { 1.45 l__mathicon_size_dim , 0.225l__mathicon_size_dim }
    draw_path_lineto:n
      { 1.125l__mathicon_size_dim , 0.225l__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.5l__mathicon_size_dim } 
                              { 0.25l__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.25l__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}

enter image description here

Answered by Skillmon on November 30, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP