Short question
How can I set the value of a key depending on the last used keys, by modifying only \pgfkeys
?
Long question
I'd like to define a set of pgfkeys that then I will be able to use in lots of functions. I'd like the have only a few number of parameters, like, for example "width","height" and "pos", so that my functions are then very easy to use. So the basic idea would be to use:
\myRectangle[width=1cm, height=2cm, pos={(current page.center)}}
However, I'd like to allow the user the capability of using shortcuts. For example, if he uses the key fullWidth
, the it's exactly the same thing as using width=\pagewidth
:
\myRectangle[fullWidth, height=2cm, pos={(current page.center)}}
but because the rectangle fills the whole width, it's useless to give two values for the position, only 1 is useful (the height here). So I'd like to be able to let the user use:
\myRectangle[fullWidth, height=2cm, pourcent=.5}
in place of
\myRectangle[fullWidth, height=2cm, pos={(current page.north east) + (current page.north west) + (0,-.5\paperheight)}}
and if we just replace fullWidth
with fullHeight
, then the code
\myRectangle[fullWidth, height=2cm, pourcent=.5}
should be equivalent to
\myRectangle[fullWidth, height=2cm, pos={(current page.north east) + (current page.north west) + (.5\paperheight,0)}}
(Notice that now, the last pair has changed)
So, as you saw, pourcent
has two different goals, depending on the last used keys, and I don't know how to express that in the code.
Here is my MWE:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{lipsum}
\graphicspath{{photos/}} % on change la racine
\usepackage{pgfkeys}
% Define the used keys in the following document
\pgfkeys{
/myKeys/.is family, /myKeys,
default/.style =
{
width = 1cm,
height = 1cm,
pos = {(current page.center)},
},
width/.estore in = \kWidth,
height/.estore in = \kHeight,
pos/.estore in = \kPos,
% Non fundamental keys
fullWidth/.style = {width=\paperwidth},
fullHeight/.style = {height=\paperheight},
pourcent/.style args={#1}{
% Works for fullWidth...
pos={(current page.north west) + (.5\paperwidth,-#1\paperheight)}
% But for fullHeight it should be
% pos={(current page.north west) + (#1\paperwidth,-.5\paperheight)}
}
}
% Create a rectangle. You can set the properties color, width
% height, pos, anchor, fullWidth, fullHeight, pospc, background
\newcommand{\myRectangle}[1][]{%
\pgfkeys{/myKeys, default, #1}%
\begin{tikzpicture}[remember picture,overlay]%
\node [inner sep=0pt, shape=rectangle, fill=red, minimum width=\kWidth, minimum height=\kHeight, anchor=center, at/.expanded={($\kPos$)}] {};%
\end{tikzpicture}%
}
\begin{document}
\myRectangle[fullWidth,pos={(current page.center)}]
\myRectangle[fullWidth,pourcent=.7]
% Does not work as expected... see pourcent/.style
\myRectangle[fullHeight,pourcent=.9]
\end{document}
-- EDIT -- I tried:
pourcent/.code args={#1}{
\edef\w{\pgfkeysvalueof{pourcent}}%
\ifx\w\paperwidth\relax%
\pgfkeysalso{pos={(current page.north west) + (.5\paperwidth,-#1\paperheight)}}%
\else%
\pgfkeysalso{pos={(current page.north west) + (.5\paperwidth,-\paperheight)}}%
\fi%
},
it compiles, but does not work, I guess that the expression \edef\w{\pgfkeysvalueof{pourcent}}
does not reduce...