我想用矩形创建一个新形状,该形状只绘制了顶部和底部的线,但可以填充。感谢 Andrew Stacy 和 percusse,答案现在如下所示:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[twoside,reqno,12pt]{amsart}
\usepackage{a4wide} %% uncomment if a4wide is not installed
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% T i k Z
%% -- packages
%% -- macros & shapes
\usepackage{tikz}
\usetikzlibrary{arrows,backgrounds}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{decorations.markings}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% new shape : iso
%%
\makeatletter
\newif\ifpullback
\pgfkeys{/tikz/pullback/.is if=pullback}
\pgfkeys{/tikz/pullback=false}
\pgfdeclareshape{iso}{%
%
\inheritsavedanchors[from=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]{mid}
\inheritanchor[from=rectangle]{mid west}
\inheritanchor[from=rectangle]{mid east}
\inheritanchor[from=rectangle]{base}
\inheritanchor[from=rectangle]{base west}
\inheritanchor[from=rectangle]{base east}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{south west}
\inheritanchor[from=rectangle]{south east}
%
\savedanchor\centerpoint
{
\pgf@x=0pt
\pgf@y=0pt
}
\anchor{text}{%
\pgf@x=0pt
% adjust vertical positioning
\pgf@y=0pt
\advance\pgf@y by -0.2\ht\pgfnodeparttextbox
}
%
\backgroundpath{%
\northeast \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\southwest \pgf@xb=\pgf@x \pgf@yb=\pgf@y
\begingroup
\tikz@mode
\iftikz@mode@draw
% -- draw top and bottom line
\pgfpathmoveto\northeast
\pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
\pgfpathmoveto\southwest
\pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
\pgfusepath{stroke}
\fi
\iftikz@mode@fill
% -- fill rectangle if demanded
\pgfsetlinewidth{\pgflinewidth}
\advance\pgf@ya by -0.5\pgflinewidth%
\advance\pgf@yb by 0.5\pgflinewidth%
\pgfpathrectanglecorners{\pgfqpoint{\pgf@xb}{\pgf@yb}}{\pgfqpoint{\pgf@xa}{\pgf@ya}}
\pgfusepath{fill}
\fi
\ifpullback
% -- if option pullback is given draw hook
\northeast \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\southwest \pgf@xb=\pgf@x \pgf@yb=\pgf@y
\pgf@xc=\pgf@xb
\advance\pgf@xc by 3\pgflinewidth
\pgf@yc=\pgf@ya
\advance\pgf@yc by -2\pgflinewidth
\pgfpathmoveto{\pgfqpoint{\pgf@xc}{\pgf@ya}}
\pgfpathlineto{\pgfqpoint{\pgf@xc}{\pgf@yc}}
\pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@yc}}
\pgfusepath{stroke}
\fi
\endgroup
}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\draw (0,-2) grid (6,3);
\node[fill=blue!20,line width=2pt,iso,draw,minimum width=2.5cm,inner ysep=0pt] at (2,2){$=$};
\node[fill=green!20,pullback,line width=1.25pt,iso,draw,minimum width=2.5cm,inner ysep=0pt] at (2,1){$=$};
\end{tikzpicture}
\end{document}
此解决方案还考虑到了填充框所用的矩形会覆盖部分顶部和底部线条,从而将其宽度减半。因此,在绘制填充矩形之前,东北和西南 y 坐标会改变一半 \pgflinewidth。pullback
添加了 On 选项,可在左上角绘制一个小的钩子。[此形状旨在描绘 2 个类别的 2 个缠结中的 iso-2 单元格]。
答案1
下面的操作可以完成该任务(感谢 Andrew Stacey 的精彩修正)。
\documentclass{article}
\usepackage{tikz}
\makeatletter
\pgfdeclareshape{iso}{%
%
\inheritsavedanchors[from=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]{mid}
\inheritanchor[from=rectangle]{mid west}
\inheritanchor[from=rectangle]{mid east}
\inheritanchor[from=rectangle]{base}
\inheritanchor[from=rectangle]{base west}
\inheritanchor[from=rectangle]{base east}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{south west}
\inheritanchor[from=rectangle]{south east}
%
\savedanchor\centerpoint
{
\pgf@x=0pt
\pgf@y=0pt
}
\anchor{text}{%
\pgf@process{\centerpoint}
\advance\pgf@y by -0.2pt
}
%
\backgroundpath{%
\northeast \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\southwest \pgf@xb=\pgf@x \pgf@yb=\pgf@y
\begingroup
\tikz@mode
\iftikz@mode@fill
\pgfpathrectanglecorners{\northeast}{\southwest}
\pgfusepath{fill}
\fi
\iftikz@mode@draw
\pgfpathmoveto\northeast
\pgfpathlineto{\pgfqpoint{\pgf@xb}{\pgf@ya}}
\pgfpathmoveto\southwest
\pgfpathlineto{\pgfqpoint{\pgf@xa}{\pgf@yb}}
\pgfusepath{stroke}
\fi
\endgroup
}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\node[iso,draw,fill=yellow] (a) at (0,0) {slight yellow};
\end{tikzpicture}
\end{document}