根据 pagenodes 定义 tikz 节点

根据 pagenodes 定义 tikz 节点

我想current grid area根据包中定义的节点定义一个名为 的 tikz 节点。此节点是一个矩形,tikzpagenodes包含包中定义的 4 个节点;current page footercurrent page header和:current page text areacurrent page marginpar

网格区域的北边应该是页眉的北边;南边应该是页脚的南边。东边和西边取决于正反面页面。在奇数页中,西边应该是文本区域的西边和页边距的东边。在偶数页中,西边是页边距的西边,东边是文本区域的东边。

我提取每个区域角落的坐标值,并将它们保存在全局宏中,例如\gl@globalgridareaw\gl@globalgridareae...然后我使用代码构建节点current pagepgfmoduleshapes.code.tex这是我不太理解的部分:-),

% 特殊当前网格区域边界框矩形:
\expandafter\def\csname pgf@sh@ns@当前网格区域\endcsname{rectangle}
\expandafter\def\csname pgf@sh@np@当前网格区域\endcsname{%
  \def\southwest{\pgfqpoint{\gl@globalgridareaw}{\gl@globalgridareas}}%
  \def\northeast{\pgfqpoint{\gl@globalgridareae}{\gl@globalgridarean}}%
}
% % TODO:我应该重新定义它吗?
 \expandafter\def\csname pgf@sh@nt@当前网格区域\endcsname{{1}{0}{0}{1}{0pt}{0pt}}
 \expandafter\def\csname pgf@sh@pi@当前网格区域\endcsname{pgfpageorigin}
}

如下例所示,节点定义不正确。似乎应该添加一些偏移量。

\documentclass{article}
\usepackage{geometry}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}

\makeatletter
\newcommand{\calculateparameters}{
    \noindent\tikz[remember picture,overlay]{\path (current page header area.north);%
    \pgfgetlastxy{\gl@discard}{\gl@gridarean}
    \path (current page footer area.south);
    \pgfgetlastxy{\gl@discard}{\gl@gridareas}
    \global\let\gl@globalgridareas\gl@gridareas%
    \global\let\gl@globalgridarean\gl@gridarean%
  }
    \checkoddpage

    \ifoddpage
    \noindent\tikz[remember picture,overlay]{
      \path (current page marginpar area.east);
      \pgfgetlastxy{\gl@gridareae}{\gl@discard}
      \path (current page text area.west);
      \pgfgetlastxy{\gl@gridareaw}{\gl@discard}
      \global\let\gl@globalgridareaw\gl@gridareaw%
      \global\let\gl@globalgridareae\gl@gridareae%
    }
    \else
    \tikz[remember picture,overlay]{
      \path (current page marginpar area.west);
      \pgfgetlastxy{\gl@gridareaw}{\gl@discard}
      \path (current page text area.east);
      \pgfgetlastxy{\gl@gridareae}{\gl@discard}
      \global\let\gl@globalgridareaw\gl@gridareaw%
      \global\let\gl@globalgridareae\gl@gridareae%
    }
    \fi

% Special  current grid area bounding box rectangle:
\expandafter\def\csname pgf@sh@ns@current grid area\endcsname{rectangle}
\expandafter\def\csname pgf@sh@np@current grid area\endcsname{%
  \def\southwest{\pgfqpoint{\gl@globalgridareaw}{\gl@globalgridareas}}%
  \def\northeast{\pgfqpoint{\gl@globalgridareae}{\gl@globalgridarean}}%
}
% % TODO: Should I redefine this?
 \expandafter\def\csname pgf@sh@nt@current grid area\endcsname{{1}{0}{0}{1}{0pt}{0pt}}
 \expandafter\def\csname pgf@sh@pi@current grid area\endcsname{pgfpageorigin}
}

\makeatother

\begin{document}
\calculateparameters%
\noindent\tikz[<-,remember picture,overlay] {%
  \draw[red] (current grid area.south west) rectangle (current grid area.north);
}%


\tikz[remember picture,overlay] {%
\draw [blue,line width=2mm]
(current page.south west)
rectangle
(current page.north east);
\draw [green]
(current page text area.south west)
rectangle
(current page text area.north east);
\draw [yellow]
(current page marginpar area.south west)
rectangle
(current page marginpar area.north east);
\draw [red]
(current page header area.south west)
rectangle
(current page header area.north east);
\draw [orange]
(current page footer area.south west)
rectangle
(current page footer area.north east);
}%
\end{document}

答案1

您在那里提取的坐标值(您也可以使用\pgfpointanchor{<node name>}{<anchor>},然后您将获得尺寸中的坐标\pgf@x,而\pgf@y无需使用 TikZ)是当前 TikZ 图片中的坐标(相对于原点)。

但是在\northeast\southwest\savedanchor形状的rectangle)中,需要在节点本身的本地坐标系中使用坐标(在这些情况下,该坐标系是页面的原始坐标系{1}{0}{0}{1}{0pt}{0pt})。

如果当前 TikZ 图片(您访问节点的位置)与宏(此处)\pgfpictureid中的不一样,则当前 TikZ 图片和节点之间的差异将添加到坐标中(这是文件发挥作用的地方)。\pgf@sh@pi@<node name>pgfpageoriginremember picture.aux

如果你可以在一个 TikZ 图片中提取所有需要的坐标,然后定义节点current grid area并使用

\expandafter\edef\csname pgf@sh@pi@current grid area\endcsname{\pgfpictureid}

(无论如何,变换矩阵都是相同的)。

但你也可以使用这个fit库来做

\node[fit=(current page marginpar area)(current page footer area)(current page header area),
      inner sep=+0pt] (current grid area) {};

(仍然需要在每一页上都这样做,以区分偶数和奇数)。

长话短说:

我只需使用来自原始页面节点的坐标值tikzpagenodes。这些包括一些计算,考虑了某些区域的宽度和高度以及这些区域之间的分隔符。


我删除了geometry从您的示例中进行包,因为它对 d 文档中的边距宽度产生了不良影响(至少在没有任何设置的情况下)two side

代码

\documentclass[two side]{article}
\usepackage{lipsum,tikzpagenodes}
\makeatletter
\@newtikzpagenode{current grid area}{%
 \def\southwest{\pgfpoint
  {\current@textarea@left\ifoddpageoroneside\else-\marginparsep-\marginparwidth\fi}
  {\paperheight-\current@textarea@top-\textheight-\footskip}}%
 \def\northeast{\pgfpoint
  {\current@textarea@left+\textwidth\ifoddpageoroneside+\marginparsep+\marginparwidth\fi}
  {\paperheight-\current@textarea@top+\headsep+\headheight}}%
}
\makeatother
\newcommand*\drawAreas[1][]{%
\tikz[
  remember picture, overlay,
  nodes={fill=white, fill opacity=.8, text opacity=1, inner ysep=+1pt},
  RECT/.style={to path={(\tikztostart.south west) rectangle
    node[##1] {\tikztostart} (\tikztostart.north east)}},
  #1] {%
  \draw [blue,line width=2mm](current page)                to[RECT] ();
  \draw [green]              (current page text area)      to[RECT] ();
  \draw [yellow!70!black]    (current page marginpar area) to[RECT={rotate=90}] ();
  \draw [red]                (current page header area)    to[RECT] ();
  \draw [orange]             (current page footer area)    to[RECT] ();
  \draw [black, ultra thick] (current grid area)           to[RECT] ();
}}
\begin{document}
\marginpar{\color{gray}\raggedleft\lipsum[2]}
\drawAreas

\vfill\pagebreak
\marginpar{\color{gray}\raggedright\lipsum[2]}
\drawAreas
\end{document}

输出

在此处输入图片描述

在此处输入图片描述

相关内容