我对这个答案有疑问问题。
根据其答案的评论所建议,我修改了解决方案如下:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\usetikzlibrary{calc,positioning}
\newcounter{image}
\setcounter{image}{0}
\tikzstyle{square} = [draw, rectangle, draw=black, minimum width=1cm, minimum height=1cm, text centered]
\tikzset{drawinside/.code args={#1}{
\draw($(#1.west)!0.3!(#1.center)$)--($(#1.east)!0.3!(#1.center)$);
\draw($(#1.south)!0.3!(#1.center)$)--($(#1.north)!0.3!(#1.center)$);
\draw($(#1.south west)!0.4!(#1.west)!0.3!(#1.center)$)--($(#1.south west)!0.165!(#1.west)!0.5!(#1.center)$)--(#1.center);
\draw(#1.center)--($(#1.north east)!0.165!(#1.east)!0.65!(#1.center)$)--($(#1.north east)!0.45!(#1.east)!0.45!(#1.center)$);
}
}
\tikzset{record/.style args={#1 and #2}{
rectangle,draw,minimum width=#1, minimum height=#2
}
}
\newcommand{\drawrecord}[1]
{
\stepcounter{image}
{
\node [record=1 cm and 1 cm, name=a\theimage] at {#1};
}
\node[drawinside={a\theimage}]{};
}
\begin{document}
\begin{tikzpicture}
\drawrecord{0,0};
\node (sq) [square] {};
\drawrecord{sq};
\end{tikzpicture}
\end{document}
! Package tikz Error: Cannot parse this coordinate.
但是当我调用时我不断收到错误\drawrecord
.我该如何解决这个问题?
。有没有办法将其定义\drawrecord
为\tikzstyle
而不是新命令?
编辑:
对于第二个问题,有人建议我研究一下“append after command”,但我找不到关于它的文档,有人有链接吗?
编辑2: 实际上,第二个问题有点多余,我可以简单地调用
\node [record=1 cm and 1 cm, name=newnode, right of=oldnode];
\node[drawinside={newnode}]{};
在我的文档中。并且
\tikzset{drawinside/.code args={#1}{
\draw($(#1.west)!0.3!(#1.center)$)--($(#1.east)!0.3!(#1.center)$);
\draw($(#1.south)!0.3!(#1.center)$)--($(#1.north)!0.3!(#1.center)$);
\draw($(#1.south west)!0.4!(#1.west)!0.3!(#1.center)$)--($(#1.south west)!0.165!(#1.west)!0.5!(#1.center)$)--(#1.center);
\draw(#1.center)--($(#1.north east)!0.165!(#1.east)!0.5!(#1.center)$)--($(#1.north east)!0.4!(#1.east)!0.3!(#1.center)$);
}
}
是对称的(前一个略有偏差)
答案1
如果你的目的是定义一个newshape
,而不是使用Claudio 的回答, 使用Percusse 的一个。
下一个代码稍微修改了satnode
Percusse 定义的形状。我已将其名称更改为record
和一些尺寸,使其看起来更像您的示例。
您可以使用
\node[record,minimum size=1cm,fill=blue!30,draw] (a) {};
将其放置record
在您想要的位置,固定其大小、颜色......,然后使用它的锚点以供日后使用。
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\makeatletter
\pgfdeclareshape{record}{
\inheritsavedanchors[from={rectangle}]
\inheritbackgroundpath[from={rectangle}]
\inheritanchorborder[from={rectangle}]
\foreach \x in {center,north east,north west,north,south,south east,south west}{
\inheritanchor[from={rectangle}]{\x}
}
\foregroundpath{
\pgfpointdiff{\northeast}{\southwest}
\pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast
\pgfpathmoveto{\pgfpoint{0}{0.33\pgf@ya}}
\pgfpathlineto{\pgfpoint{0}{-0.33\pgf@ya}}
\pgfpathmoveto{\pgfpoint{0.33\pgf@xa}{0}}
\pgfpathlineto{\pgfpoint{-0.33\pgf@xa}{0}}
\pgfpathmoveto{\pgfpointadd{\southwest}{\pgfpoint{-0.33\pgf@xa}{-0.6\pgf@ya}}}
\pgfpathlineto{\pgfpointadd{\southwest}{\pgfpoint{-0.5\pgf@xa}{-0.6\pgf@ya}}}
\pgfpathlineto{\pgfpointadd{\northeast}{\pgfpoint{-0.5\pgf@xa}{-0.6\pgf@ya}}}
\pgfpathlineto{\pgfpointadd{\northeast}{\pgfpoint{-0.33\pgf@xa}{-0.6\pgf@ya}}}
}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\node[record,minimum size=1cm,fill=blue!30,draw] (a) {};
\node[record,draw,fill=red!30,minimum height=12mm, minimum width=6mm] (a2) at (-1,1.5) {};
\node[record,thick,draw=purple!30,minimum height=1cm,minimum width=1.5 cm,fill=yellow!10, above right=.5cm and .4cm of a] (a1){};
\draw[-latex] (a2) -| (a);
\draw[-latex] (a) -| (a1);
\end{tikzpicture}
\end{document}
答案2
另一种做事的方式,这次使用path picture
:
\documentclass[tikz,border=5]{standalone}
\tikzset{%
saturation block/.style={%
draw,
path picture={
% Get the width and height of the path picture node
\pgfpointdiff{\pgfpointanchor{path picture bounding box}{north east}}%
{\pgfpointanchor{path picture bounding box}{south west}}
\pgfgetlastxy\x\y
% Scale the x and y vectors so that the range
% -1 to 1 is slightly shorter than the size of the node
\tikzset{x=\x*.4, y=\y*.4}
%
% Draw annotation
\draw (-1,0) -- (1,0) (0,-1) -- (0,1);
\draw (-1,-.7) -- (-.7,-.7) -- (.7,.7) -- (1,.7);
}
}
}
\begin{document}
\begin{tikzpicture}
\node [saturation block, minimum size=0.5cm] at (0,0) {};
\node [saturation block, minimum size=1cm] at (0,1) {};
\node [saturation block, minimum width=2cm, minimum height=1cm] at (0,2.25) {};
\end{tikzpicture}
\end{document}
答案3
这里有一个语法问题:
\node [record=1 cm and 1 cm, name=a\theimage] at {#1};
at
需要一个坐标,所以你需要
\node [record=1 cm and 1 cm, name=a\theimage] at (#1) {};
代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows}
\usetikzlibrary{calc,positioning}
\newcounter{image}
\setcounter{image}{0}
\tikzset{
square/.style={
rectangle,
draw=black,
minimum width=1cm,
minimum height=1cm,
text centered
},
drawinside/.code args={#1}{
\draw($(#1.west)!0.3!(#1.center)$)--($(#1.east)!0.3!(#1.center)$);
\draw($(#1.south)!0.3!(#1.center)$)--($(#1.north)!0.3!(#1.center)$);
\draw($(#1.south west)!0.4!(#1.west)!0.3!(#1.center)$)--($(#1.south west)!0.165!(#1.west)!0.5!(#1.center)$)--(#1.center);
\draw(#1.center)--($(#1.north east)!0.165!(#1.east)!0.65!(#1.center)$)--($(#1.north east)!0.45!(#1.east)!0.45!(#1.center)$);
},
record/.style args={#1 and #2}{
rectangle,draw,minimum width=#1, minimum height=#2
}
}
\newcommand{\drawrecord}[1]{%
\stepcounter{image}%
\node [record=1 cm and 1 cm, name=a\theimage] at (#1) {};
\node[drawinside={a\theimage}]{};%
}
\begin{document}
\begin{tikzpicture}
\drawrecord{0,0};
\node (sq) [square] {};
\drawrecord{sq}
\end{tikzpicture}
\end{document}