对象的 z 顺序

对象的 z 顺序

当我在 PSTricks 中绘制物体时,物体会按照我绘制它们的顺序放置,即,图片中较晚出现的物体会隐藏较早出现的物体,例如这里:

\begin{pspicture}(70,70)
\psframe[fillcolor=green!20,fillstyle=solid](0,0)(30,30)
\psframe[fillcolor=blue!20,fillstyle=solid](10,10)(40,40)
\end{pspicture}

蓝色方块部分遮住了绿色方块。

在 CSS 中,可以为每个对象指定一个“z 索引”,这样 z 索引较大的对象就会隐藏 z 索引较小的对象,例如,如果我为绿色正方形指定 z 索引 10,为蓝色正方形指定 z 索引 0,则无论它们在文档中的放置顺序如何,绿色正方形都会隐藏蓝色正方形。

PSTricks 中有类似的选项吗?

(当然在上面的例子中可以只切换图片中方块的顺序,但是在更复杂的图片中,有许多不同的对象,其中一些来自宏等,这可能非常困难)。

答案1

我们可以收集类似\PST<z>where <z>starts with这样的宏0。首先我们必须改变反斜杠的行为来保存完整的命令序列:

\documentclass{minimal}
\usepackage{pstricks}
\makeatletter
\def\addentry#1{\addentry@i#1\@nil}
\begingroup
\catcode`\|=0    %% make the | behave like a backslash
|catcode`\\=12   %% make the \ a normal character
|gdef|addentry@i#1;#2|@nil{|@namedef{PST#1}{#2}}% save #2 as \PST#1
|endgroup
\def\printentries{\printentries@i{0}}
\def\printentries@i#1{%
  \if\@nameuse{PST#1}\relax
  \else
    \@nameuse{PST#1}%
    \printentries@i{\the\numexpr#1+1}%
 \fi}
\makeatother
\begin{document}

\psset{unit=0.1}
\begin{pspicture}(40,40)
\pscircle*[linecolor=magenta,opacity=0.5](20,20){20}
\psframe[fillcolor=green!20,fillstyle=solid](0,0)(30,30)
\psframe[fillcolor=blue!20,fillstyle=solid](10,10)(40,40)
\end{pspicture}
%
\begin{pspicture}(40,40)
\addentry{2;\pscircle*[linecolor=magenta,opacity=0.5](20,20){20}}
\addentry{1;\psframe[fillcolor=green!20,fillstyle=solid](0,0)(30,30)}
\addentry{0;\psframe[fillcolor=blue!20,fillstyle=solid](10,10)(40,40)}
\printentries
\end{pspicture}

\end{document}

在此处输入图片描述

答案2

PSTricks 中没有这样的选项,但是pst-ovl包可用于更详细的覆盖类型:

\documentclass[pstricks]{standalone}
\usepackage{pst-ovl}
\begin{document}
\psset{unit=0.1}
\begin{pspicture}(40,40)
\begin{overlaybox}
  \psoverlay{two}\psframe[fillcolor=green!20,fillstyle=solid](0,0)(30,30)
  \psoverlay{one}\psframe[fillcolor=blue!20,fillstyle=solid](10,10)(40,40)
\end{overlaybox}%
\putoverlaybox{one}\putoverlaybox{two}
\end{pspicture}
\end{document}

在此处输入图片描述

相关内容