这是我的一个或多或少最简单的示例,我在其中绘制了一个带有小折角的“文档”形状。值是硬编码的,以减小示例的大小。
我的问题是,在 中beforebackgroundpath
,我明确给出命令绘制一条粗红色线条,然后向下移动一点并绘制另一条更细的黑色线条。但是,只绘制了粗红线。
\documentclass[tikz,border=1mm]{standalone}
\makeatletter
\pgfdeclareshape{document}{
\inheritsavedanchors[from=rectangle]
\inheritanchorborder[from=rectangle]
\inheritanchor[from=rectangle]{center}
\inheritanchor[from=rectangle]{north}
\inheritanchor[from=rectangle]{north east}
\inheritanchor[from=rectangle]{east}
\inheritanchor[from=rectangle]{south east}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{south west}
\inheritanchor[from=rectangle]{west}
\inheritanchor[from=rectangle]{north west}
\backgroundpath{
\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
% main shape
\pgf@xc=\pgf@xb
\advance\pgf@xc by-10pt
\pgf@yc=\pgf@yb
\advance\pgf@yc by-5pt
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
\pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
\pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yb}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yc}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
\pgfpathclose
% fold
\pgfpathmoveto{\pgfpoint{\pgf@xb}{\pgf@yc}}
\pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
\pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yb}}
}
\beforebackgroundpath{
\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
% lines
\advance\pgf@xa by+5pt
\advance\pgf@xb by-5pt
\pgfmathsetlength\pgf@yc{(\pgf@yb-\pgf@ya)/8}
\pgf@ya=\pgf@yb
\advance\pgf@ya by-10pt
\pgf@xc=.3\pgf@xb
\pgfsetstrokecolor{red}
\pgfsetlinewidth{4pt}
\advance\pgf@ya by-\pgf@yc
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
\pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@ya}}
\pgfusepath{stroke}
\pgfsetstrokecolor{black}
\pgfsetlinewidth{2pt}
\advance\pgf@ya by-\pgf@yc
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
\pgfusepath{stroke}
}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\node[document,draw,text width=3cm,minimum height=5cm] {};
\end{tikzpicture}
\end{document}
答案1
问题是\pgf@xb
被 覆盖\pgfusepath{stroke}
,所以你画了一条长度为 0pt 的线。你需要\pgf@xb
随后设置,例如
\documentclass[tikz,border=1mm]{standalone}
\makeatletter
\pgfdeclareshape{document}{
\inheritsavedanchors[from=rectangle]
\inheritanchorborder[from=rectangle]
\inheritanchor[from=rectangle]{center}
\inheritanchor[from=rectangle]{north}
\inheritanchor[from=rectangle]{north east}
\inheritanchor[from=rectangle]{east}
\inheritanchor[from=rectangle]{south east}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{south west}
\inheritanchor[from=rectangle]{west}
\inheritanchor[from=rectangle]{north west}
\backgroundpath{
\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
% main shape
\pgf@xc=\pgf@xb
\advance\pgf@xc by-10pt
\pgf@yc=\pgf@yb
\advance\pgf@yc by-5pt
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
\pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
\pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yb}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yc}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
\pgfpathclose
% fold
\pgfpathmoveto{\pgfpoint{\pgf@xb}{\pgf@yc}}
\pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
\pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yb}}
}
\beforebackgroundpath{
\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
% lines
\advance\pgf@xa by+5pt
\advance\pgf@xb by-5pt
\pgfmathsetlength\pgf@yc{(\pgf@yb-\pgf@ya)/8}%
\pgf@ya=\pgf@yb
\advance\pgf@ya by-10pt
\pgf@xc=.3\pgf@xb
\pgfsetstrokecolor{red}%
\pgfsetlinewidth{4pt}
\advance\pgf@ya by-\pgf@yc
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}%
\pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@ya}}%
\typeout{before pgfusepath: xb=\the\pgf@xb}%
\pgfusepath{stroke}%
\typeout{after pgfusepath: xb=\the\pgf@xb}%
\pgfsetstrokecolor{black}%
\pgfsetlinewidth{2pt}
\advance\pgf@ya by-\pgf@yc
\pgf@xb=2\pgf@xc
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}%
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}%
\pgfusepath{stroke}%
}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\node[document,draw,text width=3cm,minimum height=5cm] {};
\end{tikzpicture}
\end{document}