2

Trying to make a new command expanding tikz I got stuck changing the color for the used \fill macro. Any construction of .unknown-handler or .search also={/tikz} failed. The solution in Pass key option inside a macro to a TikZ drawing command adds the keys to \begin{tikzpicture} instead of \fill.

MWE:

\documentclass{article}
\usepackage{tikz}

\newlength\tikwidth

\pgfkeys{/tikzTest/.cd,
         TestWidth/.code = \setlength{\tikwidth}{#1},
         /tikzTest/.search also = {/tikz},
        }

%%%% userlevel call \Block[]()()
\def\Block[#1]#2#3{
  \begin{tikzpicture}
    \pgfkeys{/tikzTest/.cd,#1}
    \fill (#2) rectangle (#3);
  \end{tikzpicture}
  }

\begin{document}
  \Block[color=red] {0,0}{0.5,0.5}
  \Block[color=blue] {0.5,0.5}{1,1};
\end{document}

which gives me two black squares instead of one red and one blue.

I'm out of ideas of what I'm missing...

2 Answers 2

2

The problem is that \pgfkeys does not set the options for the current picture or path. Therefore you have to pass the options to the picture directly.

You should also use \newcommand for optional argument, so it doesn't fail if the optional argument is not there and you will want to remove the line endings using % to prevent spurious spaces.

\documentclass{article}
\usepackage{tikz}

\newlength\tikwidth

\pgfkeys{
  /tikzTest/.cd,
  TestWidth/.code = \setlength{\tikwidth}{#1},
  /tikzTest/.search also = {/tikz},
}

%%%% userlevel call \Block[]()()
\newcommand\Block[3][]{%
  \begin{tikzpicture}[/tikzTest/.cd,#1]
    \fill (#2) rectangle (#3);
  \end{tikzpicture}%
}

\begin{document}

\Block[fill=red]{0,0}{0.5,0.5}
\Block[fill=blue]{0.5,0.5}{1,1}
\Block{0,0}{.5,.5}

\end{document}

enter image description here

2
  • for the MWE it works - but not for the original code. It's a part of 50Mb of a book - guess I have the error somewhere else, so I have to look over again. Thx anyway - and for the newcommand - I just hardcoded it for the MWE. I'm 30yrs in tex now and the original is using oldstyle \@ifnextchar [... By the way - you also added \begin{tikzpicture}[/tikzTest/.cd,#1]. I'll give it a try with \fill[/tikzTest/.cd,#1]...
    – AndiW
    Nov 19, 2017 at 0:54
  • that does it - at least partly. Since the boxes are part of a bigger picture I replaced \begin{tikzpicture}[/tikzTest/.cd,#1] by a \begin{scope}[/tikzTest/.cd,#1] and voilà. What confused me is that in the original scope shift does not work in a situation where you place the boxes via anchors related to tcolorboxes...
    – AndiW
    Nov 19, 2017 at 10:54
0

You can force the options to take place by every path key.

\documentclass{article}
\usepackage{tikz}

\newlength\tikwidth
\pgfkeys{
  /tikzTest/.is family,
  /handler config=full or existing,
  /tikzTest/.search also={/tikz},
  /tikzTest/TestWidth/.code = \setlength{\tikwidth}{#1}
}

%%%% userlevel call \Block[]()()
\def\Block[#1]#2#3{%
  \begin{tikzpicture}
    \pgfkeys{/tikzTest/.cd,every path/.append style={#1}}
    \path (#2) rectangle (#3);
  \end{tikzpicture}%
  }

\begin{document}
  \Block[fill=red,line width=1.4mm,draw=yellow] {0,0}{0.5,0.5}
  \Block[fill=blue] {0.5,0.5}{1,1};
\end{document}

enter image description here

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.