2

I have the following code section:

\pgfkeys{
  /tikz/.cd,
  color izquierda/.initial=\tikz@fillcolor,
  color derecha/.initial=\tikz@fillcolor,
}

It works, but if I don't have activated both options, I get an error if option 'fill=any color' is not selected. Please, can someone help me?. I also need to detect if fill option is selected. Thanks in advance!

Here a MWE:

\documentclass[]{article}

\usepackage{tikz,makeshape}

\def\gap{0mm}

\makeatletter

% Dimensiones medidas en Autocad para las válvulas con anchura = 3mm en un
% círculo de 2mm de radio.

\def\radio@llave{2mm}
\def\x@llave{1.5mm}
\def\y@llave{1.32mm}

\pgfkeys{
    % Requiere que fill=color esté activado en caso de no estar izq. y dcha.
    /tikz/.cd,
    color izquierda/.initial=\tikz@fillcolor,
    color derecha/.initial=\tikz@fillcolor
}

%% Anchor path:

\def\llaveanchor{ % Obtiene la caja de texto correcta:

    \pgf@xa=\ctbnex
    \pgf@ya=\ctbney

    % Crea el margen para la forma:
    \advance\pgf@xa by \gap
    \advance\pgf@ya by \gap
    % Corrige minheight y minwidth, y outerxsep o outerysep:
    \mincorrect{\pgf@xa}{\pgfshapeminwidth}
    \advance\pgf@xa\pgfshapeouterxsep
    \mincorrect{\pgf@ya}{\pgfshapeminheight}
    \advance\pgf@ya\pgfshapeouterysep
    % Dibuja el anchor con \pgfusepath{stroke}:
    \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfpoint{\pgf@xa}{-\pgf@ya}}
    \pgfpathlineto{\pgfpoint{-\pgf@xa}{-\pgf@ya}}
    \pgfpathlineto{\pgfpoint{-\pgf@xa}{\pgf@ya}}
    \pgfpathclose 
    % lo siguiente solo para comprobar
    %\pgfusepath{stroke}

}

%% Background path:

\def\llaveborder{ % Obtiene la caja de texto correcta:

    \pgf@xa=\ctbnex
    \pgf@ya=\ctbney

    % Crea el margen para la forma:
    \advance\pgf@xa by \gap
    \advance\pgf@ya by \gap
    % Corrige minheight y minwidth, y outerxsep o outerysep:
    \mincorrect{\pgf@xa}{\pgfshapeminwidth}
    \advance\pgf@xa\pgfshapeouterxsep
    \mincorrect{\pgf@ya}{\pgfshapeminheight}
    \advance\pgf@ya\pgfshapeouterysep
    %
    % Dibuja el círculo:
    %
    \pgfcircle{\pgfpointorigin}{\radio@llave}
    % con la opción clip evito los picos de la válvula saliendo el círculo
    \pgfusepath{fill,stroke,clip}
    %
    % Triángulo izquierdo:
    %
    \pgfsetfillcolor{\pgfkeysvalueof{/tikz/color izquierda}}
    %
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathlineto{\pgfpoint{-\x@llave}{ \y@llave}}
    \pgfpathlineto{\pgfpoint{-\x@llave}{-\y@llave}}
    \pgfpathclose
    \pgfusepath{fill,stroke}
    %
    % Triángulo derecho:
    %
    \pgfsetfillcolor{\pgfkeysvalueof{/tikz/color derecha}}
    %
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathlineto{\pgfpoint{ \x@llave}{ \y@llave}}
    \pgfpathlineto{\pgfpoint{ \x@llave}{-\y@llave}}
    \pgfpathclose
    \pgfusepath{fill,stroke}
    %
    % Línea vertical:
    %
    \pgfpathmoveto{\pgfpoint{0}{ \radio@llave}}
    \pgfpathlineto{\pgfpoint{0}{-\radio@llave}}
    \pgfusepath{stroke}

}

%% Declaración de la forma:

\pgfdeclareshape{llave toma en carga}{

    \setpaths{\llaveanchor}{\llaveborder}

    \savedanchor{\centerpoint}{
        \pgf@x = \ctbnex
        \pgf@y = \ctbney
        \advance\pgf@x by \gap
        \advance\pgf@y by \gap
        \mincorrect{\pgf@x}{\pgfshapeminwidth}
        \mincorrect{\pgf@y}{\pgfshapeminheight}
        \advance\pgf@x\pgfshapeouterxsep
        \advance\pgf@y\pgfshapeouterysep
    }

    % Anchors:
    \anchor{c}{\centerpoint \pgf@x=0pt \pgf@y=0pt}
    %
    \anchor{u}{\centerpoint \pgf@x=0pt \pgf@y= \radio@llave}
    \anchor{d}{\centerpoint \pgf@x=0pt \pgf@y=-\radio@llave}
    \anchor{r}{\centerpoint \pgf@x= \radio@llave \pgf@y=0pt}
    \anchor{l}{\centerpoint \pgf@x=-\radio@llave \pgf@y=0pt}
    %
    \anchor{ur}{\centerpoint \pgf@x= \x@llave \pgf@y= \y@llave}
    \anchor{dr}{\centerpoint \pgf@x= \x@llave \pgf@y=-\y@llave}
    \anchor{ul}{\centerpoint \pgf@x=-\x@llave \pgf@y= \y@llave}
    \anchor{dl}{\centerpoint \pgf@x=-\x@llave \pgf@y=-\y@llave}

}

\makeatother

\tikzset{
  st valvula/.style={
    draw,minimum width=3mm,minimum height=2.85mm,line width=0.1mm,inner sep=0em,scale=2
  }
}

\pagestyle{empty}
\begin{document}

When 'fill' and 'color izquierda'/'color derecha' are commented, an error is obtained. A way to detect if 'fill' is uncommented is necesary.
Moreover, there would be feasible not to fill anything.

\begin{center}
\begin{tikzpicture}[x=1mm,y=1mm]
    \node[%
    llave toma en carga,st valvula,scale=2,
    fill=gray!15,
    color izquierda=cyan,
    color derecha=red!50
  ] (v2) at (0,0) {};
\end{tikzpicture}
\end{center}

\end{document}

That yield:

enter image description here

6
  • 1
    There is the .is if key which you could use. Or am I misinterpreting the question? Do you want something like .try? – user121799 Mar 1 '19 at 14:39
  • My main problem is that undertanding pgf manual cost me a big effort due to the English language. Then, could you post me an example of .is if. What I need is to have three different options in a new shape: fill, color izquierda (left color) y color derecha (right color), but in some cases I would need that fill is not activated. – aguivald Mar 1 '19 at 15:03
  • Could you perhaps check if the .is if as used in tex.stackexchange.com/a/468144/121799 is what you want? I have a feeling that you may be looking for /.try or /.search also. – user121799 Mar 1 '19 at 15:13
  • I think this is not the solution because I need that \pgfsets detect if 'fill=COLOR' is done or not (no filling path). If yes, 'color izquierda' and 'color derecha' would take 'COLOR' as a default, and this works, but when neither 'color derecha' or 'color izquierda' and "fill' are not done I get an error 'Undefined color'. What I want is to have the posibility of not filling anything of the shape. I'm afraid I'm being a bit hopeless, and I'm grateful for your patience. – aguivald Mar 1 '19 at 15:35
  • Could you please try to add a very minimal compilable example? I just do not fully understand your question. – user121799 Mar 1 '19 at 16:50
2

Sorry for the silence but I am very busy. This seems to work and I left commented out \typeouts in to indicate how one can debug this.

\documentclass[]{article}

\usepackage{tikz,makeshape}

\def\gap{0mm}

\makeatletter

% Dimensiones medidas en Autocad para las válvulas con anchura = 3mm en un
% círculo de 2mm de radio.

\def\radio@llave{2mm}
\def\x@llave{1.5mm}
\def\y@llave{1.32mm}

\pgfkeys{
    % Requiere que fill=color esté activado en caso de no estar izq. y dcha.
    /tikz/.cd,
    color izquierda/.initial=\pgfutil@empty,
    %color izquierda/.default=\pgfutil@empty,
    color derecha/.initial=\pgfutil@empty%
}

%% Anchor path:

\def\llaveanchor{ % Obtiene la caja de texto correcta:

    \pgf@xa=\ctbnex
    \pgf@ya=\ctbney

    % Crea el margen para la forma:
    \advance\pgf@xa by \gap
    \advance\pgf@ya by \gap
    % Corrige minheight y minwidth, y outerxsep o outerysep:
    \mincorrect{\pgf@xa}{\pgfshapeminwidth}
    \advance\pgf@xa\pgfshapeouterxsep
    \mincorrect{\pgf@ya}{\pgfshapeminheight}
    \advance\pgf@ya\pgfshapeouterysep
    % Dibuja el anchor con \pgfusepath{stroke}:
    \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
    \pgfpathlineto{\pgfpoint{\pgf@xa}{-\pgf@ya}}
    \pgfpathlineto{\pgfpoint{-\pgf@xa}{-\pgf@ya}}
    \pgfpathlineto{\pgfpoint{-\pgf@xa}{\pgf@ya}}
    \pgfpathclose 
    % lo siguiente solo para comprobar
    %\pgfusepath{stroke}

}

%% Background path:

\def\llaveborder{ % Obtiene la caja de texto correcta:

    \pgf@xa=\ctbnex
    \pgf@ya=\ctbney

    % Crea el margen para la forma:
    \advance\pgf@xa by \gap
    \advance\pgf@ya by \gap
    % Corrige minheight y minwidth, y outerxsep o outerysep:
    \mincorrect{\pgf@xa}{\pgfshapeminwidth}
    \advance\pgf@xa\pgfshapeouterxsep
    \mincorrect{\pgf@ya}{\pgfshapeminheight}
    \advance\pgf@ya\pgfshapeouterysep
    %
    % Dibuja el círculo:
    %
    \pgfcircle{\pgfpointorigin}{\radio@llave}
    % con la opción clip evito los picos de la válvula saliendo el círculo
    \pgfusepath{fill,stroke,clip}
    %
    % Triángulo izquierdo:
    %
    %\typeout{izquierda\pgfkeysvalueof{/tikz/color izquierda}}
    \edef\pgfutil@tmpa{\pgfkeysvalueof{/tikz/color izquierda}}
    \ifx\pgfutil@tmpa\pgfutil@empty
         %\typeout{izquierda\space empty}
    \else
        \pgfsetfillcolor{\pgfkeysvalueof{/tikz/color izquierda}}
    \fi 
    %
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathlineto{\pgfpoint{-\x@llave}{ \y@llave}}
    \pgfpathlineto{\pgfpoint{-\x@llave}{-\y@llave}}
    \pgfpathclose
    \pgfusepath{fill,stroke}
    %
    % Triángulo derecho:
    %
    %\typeout{derecha\pgfkeysvalueof{/tikz/color derecha}}
    \edef\pgfutil@tmpa{\pgfkeysvalueof{/tikz/color derecha}}
    \ifx\pgfutil@tmpa\pgfutil@empty
        %\typeout{derecha\space empty}
    \else
    \pgfsetfillcolor{\pgfkeysvalueof{/tikz/color derecha}}
    \fi
    %
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathlineto{\pgfpoint{ \x@llave}{ \y@llave}}
    \pgfpathlineto{\pgfpoint{ \x@llave}{-\y@llave}}
    \pgfpathclose
    \pgfusepath{fill,stroke}
    %
    % Línea vertical:
    %
    \pgfpathmoveto{\pgfpoint{0}{ \radio@llave}}
    \pgfpathlineto{\pgfpoint{0}{-\radio@llave}}
    \pgfusepath{stroke}

}

%% Declaración de la forma:

\pgfdeclareshape{llave toma en carga}{

    \setpaths{\llaveanchor}{\llaveborder}

    \savedanchor{\centerpoint}{
        \pgf@x = \ctbnex
        \pgf@y = \ctbney
        \advance\pgf@x by \gap
        \advance\pgf@y by \gap
        \mincorrect{\pgf@x}{\pgfshapeminwidth}
        \mincorrect{\pgf@y}{\pgfshapeminheight}
        \advance\pgf@x\pgfshapeouterxsep
        \advance\pgf@y\pgfshapeouterysep
    }

    % Anchors:
    \anchor{c}{\centerpoint \pgf@x=0pt \pgf@y=0pt}
    %
    \anchor{u}{\centerpoint \pgf@x=0pt \pgf@y= \radio@llave}
    \anchor{d}{\centerpoint \pgf@x=0pt \pgf@y=-\radio@llave}
    \anchor{r}{\centerpoint \pgf@x= \radio@llave \pgf@y=0pt}
    \anchor{l}{\centerpoint \pgf@x=-\radio@llave \pgf@y=0pt}
    %
    \anchor{ur}{\centerpoint \pgf@x= \x@llave \pgf@y= \y@llave}
    \anchor{dr}{\centerpoint \pgf@x= \x@llave \pgf@y=-\y@llave}
    \anchor{ul}{\centerpoint \pgf@x=-\x@llave \pgf@y= \y@llave}
    \anchor{dl}{\centerpoint \pgf@x=-\x@llave \pgf@y=-\y@llave}

}

\makeatother

\tikzset{
  st valvula/.style={
    draw,minimum width=3mm,minimum height=2.85mm,line width=0.1mm,inner sep=0em,scale=2
  }
}

\pagestyle{empty}
\begin{document}

When 'fill' and 'color izquierda'/'color derecha' are commented, an error is obtained. A way to detect if 'fill' is uncommented is necesary.
Moreover, there would be feasible not to fill anything.

\begin{center}
\begin{tikzpicture}[x=1mm,y=1mm]
    \node[%
    llave toma en carga,st valvula,scale=2,
    fill=gray!15,
    %color izquierda=cyan,
    color derecha=red!50
  ] (v2) at (0,0) {};
\end{tikzpicture}
\end{center}

\end{document}
1
  • Thank you again for your time. Now it works. Pure tex and pfg commands are pretty tricky for me and your are being very kind. – aguivald Mar 1 '19 at 20:26

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.