5

Sorry the code below for my minimal example is a bit long. In this code I defined two macros : the first macro \SC draws semi-circle and the second macro \ARC draws arcs. The first macro uses the second one.

\SC uses some options like radius and diameter. I need to use /SCopt/.search also={/tikz} if I want some keys from tikz.

\ARC uses some options too like through and angle but here only through is used.

My english is not perfect so I don't understand the subtleties of the pgfkeys section in the pfgmanual. My problem is how to manage the keys and the paths of \pgfkeys{/ARCopt/.cd,....

If I do not use

/ARCopt/.unknown/.code     = {\let\searchname=\pgfkeyscurrentname
                                  \pgfkeysalso{\searchname/.try=#1,
                                   /tikz/\searchname/.retry=#1}},

I can't use the key diameter and I get

Error: I do not know the key '/tikz/diameter'.

It's normal this is a key of the first macro \SC but in this case I can use blue and not color=blue with /ARCopt/.search also={/tikz} (thanks to Paul Gaborit)

The question is : it is possible to get the two possibilities : unknown keys in the second macro and keys from pgf/tikz ?

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\makeatletter

\def\SCR(#1,#2){% semi circle with radius defined by 2 pts
    \coordinate (LastPT) at (barycentric cs:#2=1,#1=-2)  ;
    \coordinate (CenterPT) at (#1);
    \coordinate (StartPT) at (#2); 
}
%-----------------------------------------------------
\def\SCD(#1,#2){% semi circle with diameter defined by 2 pts
    \coordinate (CenterPT) at (barycentric cs:#1=1,#2=1)  ;
    \coordinate (StartPT) at (#1);
    \coordinate (LastPT) at (#2);}
%-----------------------------------------------------
\pgfkeys{/SCopt/.cd,
        opt/.is choice,
        opt/diameter/.code    =\def\num{1},
        opt/radius/.code      =\def\num{0},
        opt/.default =radius,
        opt,
        diameter/.style={opt=diameter},
        radius/.style={opt=radius},
        /SCopt/.search also={/tikz}
        } 
%-----------------------------------------------------
\def\SC{\@ifnextchar[{\SCb}{\SCb[]}}    
\def\SCb[#1](#2){% 
\pgfqkeys{/SCopt}{#1}
    \ifcase\num%
        \SCR(#2) % semi circle with radius defined by 2 pts
    \or% 1
        \SCD(#2)  % semi circle with diameter defined by 2 pts
    \fi
    \ARC[#1](CenterPT,StartPT,LastPT)}%
%-----------------------------------------------------
\pgfkeys{/ARCopt/.cd,
        type/.is choice,
        type/through/.code      =   \def\numa{0},
        type/angle/.code        =   \def\numa{1},
        type/.default           =   through,
        type,
        through/.style          =   {type=through},% to avoid "type ="
        angle/.style            =   {type=angle},
        /ARCopt/.unknown/.code     = {\let\searchname=\pgfkeyscurrentname
                                      \pgfkeysalso{\searchname/.try=#1,
                                       /tikz/\searchname/.retry=#1}},
        %/ARCopt/.search also={/tikz},
} 
%-----------------------------------------------------
\def\ARC{\@ifnextchar[{\ARCb}{\ARCb[]}} 
\def\ARCb[#1](#2){% 
\pgfqkeys{/ARCopt}{#1}
    \ifcase\numa%
        \ARCT[#1](#2) % arc with only pts
    \or% 1
        \ARCA[#1](#2)  % arc with 2 pts and one angle
    \fi}

\def\ARCT[#1](#2,#3,#4){%
    \pgfpointdiff{\pgfpointanchor{#2}{center}}{\pgfpointanchor{#3}{center}}
    \pgfmathparse{veclen(\pgf@x,\pgf@y)}
    \let\ra\pgfmathresult                       %it's the radius
    \pgfmathanglebetweenpoints{\pgfpointanchor{#2}{center}}{%
                               \pgfpointanchor{#3}{center}}  %first angle
    \let\fa\pgfmathresult
    \pgfmathanglebetweenpoints{\pgfpointanchor{#2}{center}}{%
                               \pgfpointanchor{#4}{center}}  %last angle
    \let\la\pgfmathresult
    \draw[shift = {(#2)},/ARCopt/.cd,#1] (\fa:\ra pt) arc (\fa:\la:\ra pt);}

\def\ARCA[#1](#2,#3,#4){%
% not defined
}
%-----------------------------------------------------
\makeatother

\begin{document}
\begin{tikzpicture}[point/.style={circle,minimum size=4pt,
    inner sep=0pt,outer sep=0pt,fill=black}]
    \path coordinate[point] (A) at (0,0)
          coordinate[point] (B) at (6,0)        
          coordinate[point] (C) at (4,0);
    \SC[color=red,thick](A,B)
    \SC[blue,thick](A,C)
    \SC[diameter](A,B)
    %\SC[diameter](B,C)
\end{tikzpicture}

\end{document}

enter image description here

  • I found a solution but I don't like it . In \pgfkeys{/ARCopt/.cd, ... I added a fake key with type/diameter/.code ={},diameter/.style={type=diameter}, and at the end I used only /ARCopt/.search also={/tikz}. The problem is that I need to do this for all the keys of \SC not used in \ARC. – Alain Matthes Apr 29 '16 at 12:19
  • Perhaps someone knows a good tutorial about pgfkeys with some examples ? – Alain Matthes Apr 29 '16 at 12:22
  • Bad solution because if several macros use the same (sub) macro, it's very difficult to manage the keys – Alain Matthes Apr 29 '16 at 21:05
  • After a look at tex.stackexchange.com/q/34376/86 I tried type/.unknown/.style = {} , The code compiles but I need an explanation or the good way to do this – Alain Matthes Apr 30 '16 at 12:38
3

Perhaps my original answer is not clear.

You can replace

    /ARCopt/.unknown/.code     = {\let\searchname=\pgfkeyscurrentname
                                  \pgfkeysalso{\searchname/.try=#1,
                                   /tikz/\searchname/.retry=#1}},

by

    /ARCopt/.search also    =   {/SCopt,/tikz},

Since in the definition of /SCopt it searches also /tikz, it should be equivalent to say

    /ARCopt/.search also    =   {/SCopt},

But then color=red fails. I do not know why.

On the other hand, if you remove

    /SCopt/.search also={/tikz},

then TeX complains that color unknown key. This is really undesired.



The argument of `.search also` is a **list** of paths. That is to say, you can `.search also` a hundred paths, including `/pgf`, `/tikz`, `/ARCopt`, `/SCopt`, and whatever you want. In your case, what you need is `/ARCopt/.search also={/SCopt}` because that is where you defined `diameter`.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.