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:

\graphicspath{{photos/}} % on change la racine

% Define the used keys in the following document
  /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
  \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$)}] {};%


\myRectangle[fullWidth,pos={(current page.center)}]


% Does not work as expected... see pourcent/.style


-- EDIT -- I tried:

  pourcent/.code args={#1}{
    \pgfkeysalso{pos={(current page.north west) + (.5\paperwidth,-#1\paperheight)}}%
    \pgfkeysalso{pos={(current page.north west) + (.5\paperwidth,-\paperheight)}}%

it compiles, but does not work, I guess that the expression \edef\w{\pgfkeysvalueof{pourcent}} does not reduce...


Finally, I got it !!!

  pourcent/.code args={#1}{
    \pgfkeysalso{pos={(current page.north west) + (.5\paperwidth,-#1\paperheight)}}%
    \pgfkeysalso{pos={(current page.north west) + (#1\paperwidth,-.5\paperheight)}}%

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.