自定义形状填充颜色问题

2019-11-08 23:45发布

定义了一个画立方体的形状,填充颜色的时候出了点问题,虽然解决了但是并不知道原理,MWE如下 ```tex \documentclass[tikz]{standalone} %% Language a...

定义了一个画立方体的形状,填充颜色的时候出了点问题,虽然解决了但是并不知道原理,MWE如下 ```tex \documentclass[tikz]{standalone} %% Language and font encodings \usepackage[english]{babel} \usepackage[utf8x]{inputenc} \usepackage[T1]{fontenc} \usepackage{xcolor} \usetikzlibrary{positioning} % Define parallelepiped shape: \makeatletter \pgfkeys{/pgf/.cd, xoffset/.initial=2mm, yoffset/.initial=2mm } \pgfdeclareshape{parallelepiped} { \inheritsavedanchors[from=rectangle] % this is nearly a rectangle \inheritanchorborder[from=rectangle] \inheritanchor[from=rectangle]{north} \inheritanchor[from=rectangle]{north west} \inheritanchor[from=rectangle]{north east} \inheritanchor[from=rectangle]{center} \inheritanchor[from=rectangle]{west} \inheritanchor[from=rectangle]{east} \inheritanchor[from=rectangle]{south} \inheritanchor[from=rectangle]{south west} \inheritanchor[from=rectangle]{south east} \backgroundpath{ % store lower right in xa/ya and upper right in xb/yb \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y \pgfmathsetlength\pgfutil@tempdima{\pgfkeysvalueof{/pgf/xoffset}} \pgfmathsetlength\pgfutil@tempdimb{\pgfkeysvalueof{/pgf/yoffset}} \def\ppd@offset{\pgfpoint{\pgfutil@tempdima}{\pgfutil@tempdimb}} % front rectangle \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}} \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}} \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}} \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}} \pgfpathclose % other sides \pgfpathmoveto{\pgfqpoint{\pgf@xb}{\pgf@yb}} \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xb}{\pgf@yb}}{\ppd@offset}} \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xa}{\pgf@yb}}{\ppd@offset}} \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xa}{\pgf@ya}}{\ppd@offset}} \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@ya}} \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@yb}} \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xa}{\pgf@yb}}{\ppd@offset}} } } \pgfdeclareshape{parallelepiped2} { \inheritsavedanchors[from=rectangle] % this is nearly a rectangle \inheritanchorborder[from=rectangle] \inheritanchor[from=rectangle]{north} \inheritanchor[from=rectangle]{north west} \inheritanchor[from=rectangle]{north east} \inheritanchor[from=rectangle]{center} \inheritanchor[from=rectangle]{west} \inheritanchor[from=rectangle]{east} \inheritanchor[from=rectangle]{south} \inheritanchor[from=rectangle]{south west} \inheritanchor[from=rectangle]{south east} \backgroundpath{ % store lower right in xa/ya and upper right in xb/yb \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y \pgfmathsetlength\pgfutil@tempdima{\pgfkeysvalueof{/pgf/xoffset}} \pgfmathsetlength\pgfutil@tempdimb{\pgfkeysvalueof{/pgf/yoffset}} \def\ppd@offset{\pgfpoint{\pgfutil@tempdima}{\pgfutil@tempdimb}} \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}} \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}} \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}} \pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}} \pgfpathclose \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@ya}} \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xa}{\pgf@ya}}{\ppd@offset}} \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xa}{\pgf@yb}}{\ppd@offset}} \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xb}{\pgf@yb}}{\ppd@offset}} \pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yb}} \pgfpathmoveto{\pgfqpoint{\pgf@xa}{\pgf@yb}} \pgfpathlineto{\pgfpointadd{\pgfpoint{\pgf@xa}{\pgf@yb}}{\ppd@offset}} } } \makeatother \tikzset{ conv/.style={ parallelepiped, draw, xoffset=-0.5cm, yoffset=0.5cm, path picture={ \draw[top color=red!50,bottom color=red!50] (path picture bounding box.south west) rectangle (path picture bounding box.north east); }, text=black, }, conv2/.style={ parallelepiped2, draw, xoffset=-0.5cm, yoffset=0.5cm, path picture={ \draw[top color=red!50,bottom color=red!50] (path picture bounding box.south west) rectangle (path picture bounding box.north east); }, text=black, }, } \begin{document} \begin{tikzpicture} \node[conv, minimum width=3cm, minimum height=1cm](conv1){Conv}; \node[conv2, right=1cm of conv1, minimum width=3cm, minimum height=1cm](conv1){Conv2}; \end{tikzpicture} \end{document} ``` 运行结果如下 ![](https://pics.latexstudio.net/data/images/201911/cbf79ccfcf86905.png)