顺序相关的 pgfkeys

顺序相关的 pgfkeys

简短问题

如何通过仅修改来根据最后使用的键来设置键的值\pgfkeys

长问题

我想定义一组 pgfkeys,然后我将能够在许多函数中使用它。我希望它们只有几个参数,例如“width”、“height”和“pos”,这样我的函数就非常容易使用。所以基本的想法是使用:

\myRectangle[width=1cm, height=2cm, pos={(current page.center)}}

但是,我想允许用户使用快捷方式。例如,如果他使用键fullWidth,则与使用完全相同width=\pagewidth

\myRectangle[fullWidth, height=2cm, pos={(current page.center)}}

但是因为矩形填满了整个宽度,所以给位置提供两个值是没有意义的,只有 1 有用(这里是高度)。所以我想让用户使用:

\myRectangle[fullWidth, height=2cm, pourcent=.5}

代替

\myRectangle[fullWidth, height=2cm, pos={(current page.north east) + (current page.north west) + (0,-.5\paperheight)}}

如果我们只是fullWidth用替换fullHeight,那么代码

\myRectangle[fullWidth, height=2cm, pourcent=.5}

应该相当于

\myRectangle[fullWidth, height=2cm, pos={(current page.north east) + (current page.north west) + (.5\paperheight,0)}}

(请注意,现在最后一对已经改变了)

所以,正如您所看到的,pourcent有两个不同的目标,取决于最后使用的键,我不知道如何在代码中表达这一点。

这是我的 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}

-- 编辑 -- 我试过:

  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%
  },

它可以编译,但不起作用,我猜测表达式\edef\w{\pgfkeysvalueof{pourcent}}并没有减少......

答案1

最后我得到了它 !!!

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

相关内容