2

I have a problem with new options in new shapes using \pgfkeys: when I compile the following code:

\documentclass[border=2mm]{standalone}

\usepackage{tikz,makeshape,filecontents}

% Require makeshape package

\makeatletter

%% Constante para la forma valvula:
\def\gap{0mm}

%% Anchor path:

\def\valvulaanchor{ % 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\valvulaborder{ % 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 triángulo derecho:
    %
    \pgfkeys{/tikz/.cd,color derecha}
    %
    \pgfpathmoveto{\pgfpoint{0em}{0em}}
    \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@ya}}
  \pgfpathlineto{\pgfpoint{\pgf@xa}{-\pgf@ya}}
    \pgfpathclose
  \pgfusepath{fill,stroke}
  %
    % Triángulo izquierdo:
    %
    \pgfkeys{/tikz/.cd,color izquierda}
    %
    \pgfpathmoveto{\pgfpoint{0em}{0em}}
    \pgfpathlineto{\pgfpoint{-\pgf@xa}{\pgf@ya}}
  \pgfpathlineto{\pgfpoint{-\pgf@xa}{-\pgf@ya}}
    \pgfpathclose
    \pgfusepath{fill,stroke}

}

%% Declaración de la forma:

\pgfdeclareshape{valvula}{

  \setpaths{\valvulaanchor}{\valvulaborder}

  \savedanchor{\northeast}{
      \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}{\northeast \pgf@x=0pt \pgf@y=0pt}
  \anchor{ur}{\northeast}
  \anchor{r}{\northeast \pgf@y=0pt}
  \anchor{dr}{\northeast \pgf@y=-\pgf@y}
  \anchor{dl}{\northeast \pgf@x=-\pgf@x \pgf@y=-\pgf@y}
  \anchor{l}{\northeast \pgf@x=-\pgf@x \pgf@y=0pt}
  \anchor{ul}{\northeast \pgf@x=-\pgf@x}

}

\makeatother

\pgfkeys{%
    /tikz/color izquierda/.style={fill=#1},
    /tikz/color derecha/.style={fill=#1},
}

\begin{document}

\begin{tikzpicture}[scale=2]

\node[
  valvula,
  minimum width=6mm,minimum height=5.5mm,scale=2,
  color derecha=green,color izquierda=cyan
] (v1) at (0,0) {};

\end{tikzpicture}

\end{document}


I get an erroneous result:

enter image description here

However, when I substitute \pgfkeys{/tikz/.cd,color derecha} and \pgfkeys{/tikz/.cd,color izquierda} by \pgfsetfillcolor{green} and \pgfsetfillcolor{cyan}, respectively, the code works:

enter image description here

Please, someone can help me? Thanks in advance!

2 Answers 2

1

Conceptually similar to Ulrike's answer but with pgf keys only, i.e. no macro (on the surface).

\documentclass[border=2mm]{standalone}

\usepackage{tikz,makeshape,filecontents}

% Require makeshape package

\makeatletter

%% Constante para la forma valvula:
\def\gap{0mm}

%% Anchor path:

\def\valvulaanchor{ % 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\valvulaborder{ % 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 triángulo derecho:
    %
    \pgfsetfillcolor{\pgfkeysvalueof{/tikz/color derecha}}
    %
    \pgfpathmoveto{\pgfpoint{0em}{0em}}
    \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@ya}}
  \pgfpathlineto{\pgfpoint{\pgf@xa}{-\pgf@ya}}
    \pgfpathclose
  \pgfusepath{fill,stroke}
  %
    % Triángulo izquierdo:
    %
    \pgfsetfillcolor{\pgfkeysvalueof{/tikz/color izquierda}}
    %
    \pgfpathmoveto{\pgfpoint{0em}{0em}}
    \pgfpathlineto{\pgfpoint{-\pgf@xa}{\pgf@ya}}
  \pgfpathlineto{\pgfpoint{-\pgf@xa}{-\pgf@ya}}
    \pgfpathclose
    \pgfusepath{fill,stroke}

}

%% Declaración de la forma:

\pgfdeclareshape{valvula}{

  \setpaths{\valvulaanchor}{\valvulaborder}

  \savedanchor{\northeast}{
      \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}{\northeast \pgf@x=0pt \pgf@y=0pt}
  \anchor{ur}{\northeast}
  \anchor{r}{\northeast \pgf@y=0pt}
  \anchor{dr}{\northeast \pgf@y=-\pgf@y}
  \anchor{dl}{\northeast \pgf@x=-\pgf@x \pgf@y=-\pgf@y}
  \anchor{l}{\northeast \pgf@x=-\pgf@x \pgf@y=0pt}
  \anchor{ul}{\northeast \pgf@x=-\pgf@x}

}

\makeatother

\pgfkeys{%
    /tikz/color izquierda/.initial=blue,
    /tikz/color derecha/.initial=red,
}

\begin{document}

\begin{tikzpicture}[scale=2]

\node[
  valvula,
  minimum width=6mm,minimum height=5.5mm,scale=2,
  color derecha=green,color izquierda=cyan
] (v1) at (0,0) {};

\end{tikzpicture}

\end{document}

enter image description here

1
  • Also, thank you very much. In adition to Ulrike's, it's posible to have default options in a easy way.
    – aguivald
    Feb 27, 2019 at 23:57
3

Store the value of your keys in commands and then use them:

\documentclass[border=2mm]{standalone}

\usepackage{tikz,makeshape,filecontents}

% Require makeshape package

\makeatletter

%% Constante para la forma valvula:
\def\gap{0mm}

%% Anchor path:

\def\valvulaanchor{ % 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\valvulaborder{ % 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 triángulo derecho:
    %
    %\pgfkeys{/tikz/.cd,color derecha}
    \pgfsetfillcolor{\derechafillcolor}
    %
    \pgfpathmoveto{\pgfpoint{0em}{0em}}
    \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@ya}}
  \pgfpathlineto{\pgfpoint{\pgf@xa}{-\pgf@ya}}
    \pgfpathclose
  \pgfusepath{fill,stroke}
  %
    % Triángulo izquierdo:
     \pgfsetfillcolor{\izquierdafillcolor}
    %
    %\pgfkeys{/tikz/.cd,color izquierda}
    %
    \pgfpathmoveto{\pgfpoint{0em}{0em}}
    \pgfpathlineto{\pgfpoint{-\pgf@xa}{\pgf@ya}}
  \pgfpathlineto{\pgfpoint{-\pgf@xa}{-\pgf@ya}}
    \pgfpathclose
    \pgfusepath{fill,stroke}

}

%% Declaración de la forma:

\pgfdeclareshape{valvula}{

  \setpaths{\valvulaanchor}{\valvulaborder}

  \savedanchor{\northeast}{
      \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}{\northeast \pgf@x=0pt \pgf@y=0pt}
  \anchor{ur}{\northeast}
  \anchor{r}{\northeast \pgf@y=0pt}
  \anchor{dr}{\northeast \pgf@y=-\pgf@y}
  \anchor{dl}{\northeast \pgf@x=-\pgf@x \pgf@y=-\pgf@y}
  \anchor{l}{\northeast \pgf@x=-\pgf@x \pgf@y=0pt}
  \anchor{ul}{\northeast \pgf@x=-\pgf@x}

}

\makeatother

\pgfkeys{%
    /tikz/color izquierda/.store in=\izquierdafillcolor,
    /tikz/color derecha/.store in =\derechafillcolor,
}

\begin{document}

\begin{tikzpicture}[scale=2]

\node[
  valvula,
  minimum width=6mm,minimum height=5.5mm,scale=2,
  color derecha=green,color izquierda=cyan
] (v1) at (0,0) {};

\end{tikzpicture}

\end{document}

enter image description here

2
  • Thank you so much. Now, it's perfect por me.
    – aguivald
    Feb 27, 2019 at 23:55
  • There is a minor problem consisting in deactivating fill option. Is there any solution for that?
    – aguivald
    Feb 28, 2019 at 8:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .