我多次想画这个东西:
\node[circle,draw,thick,inner sep=0pt,minimum size=2ex] (sum) [left = of myobject] {};
\draw[semithick] (sum.north west) -- (sum.south east);
\draw[semithick] (sum.south west) -- (sum.north east);
\fill (sum.center) -- (sum.south west) arc (-135:-45:1ex) -- cycle;
是否可以创建一个命令来执行所有这些代码?并且[left = of myobject]
应该是一个变量。
答案1
NewTiKZ 3.0 pics
可以帮助您:
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning}
\tikzset{mysymbol/.pic={
\node[circle,draw,thick,inner sep=0pt, minimum size=2ex] (-sum) {};
\draw[semithick] (-sum.north west)--(-sum.south east);
\draw[semithick] (-sum.south west)--(-sum.north east);
\fill (-sum.center)--(-sum.south west) arc (-135:-35):1ex)--cycle;
}}
\begin{document}
\begin{tikzpicture}
\node (a) {A};
\path pic (symbol_a) at ([xshift=1cm]a) {mysymbol};
\pic[above right=5mm and 8mm of a, red] (symbol_b) {mysymbol};
\pic[left=of symbol_b-sum, blue] {mysymbol};
\end{tikzpicture}
\end{document}
这里有一些pic
例子:
更新:包括填充扇区作为参数
下一个代码展示了将哪些扇区作为参数填充到定义中的一种可能性pic
。
要使用,需要一种新的语法,原来的语法parameters
更改为。这只需要一个参数就足够了,但也可以使用 2 个()或更多()。看看pic
mysymbol/.pic
pics/mysymbol/.style={code={...
pics/mysymbol/.style 2 args={code={...
pics/mysymbol/.style n args={n}{code={...
TikZ 3.0——`pic` 的多个参数查看其他一些示例。
在这种情况下,您可以传递一个扇区列表(从 0 到 3,0=东扇区,1=北扇区,...)。
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning}
\tikzset{
pics/mysymbol/.style={
code={
\node[circle,draw,thick,inner sep=0pt, minimum size=2ex] (-sum) {};
\draw[semithick] (-sum.north west)--(-sum.south east);
\draw[semithick] (-sum.south west)--(-sum.north east);
\foreach \i in {#1}
\fill[rotate=90*\i] (-sum.center)--++(-45:1ex) arc[start angle=-45,delta angle=90,radius=1ex]--cycle;
}}}
\begin{document}
\begin{tikzpicture}
\node (a) {A};
\path pic (symbol_a) at ([xshift=1cm]a) {mysymbol={2,0}};
\pic[above right=5mm and 8mm of a, red] (symbol_b) {mysymbol={0}};
\pic[left=of symbol_b-sum, blue] {mysymbol={0,2,3}};
\end{tikzpicture}
\end{document}
答案2
根据您的问题,[left = of myobject] 被翻译成 3 个参数 [#1 = #2 of #3]
用户定义的命令mydraw
是绘制重复的线条,其mylocation
采用的样式#1 = above, below, left or right, #2 = x unit and #3 = myobject
。在此示例中,A=myobject。
代码
\documentclass[border=10pt]{standalone}
%\usepackage[margin=1cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc,positioning}
\tikzset{mylocation/.style args={#1 and #2 of #3}{
#1 = #2 of #3}
}
\newcommand\mydraw[3]{
\node[circle,draw,thick,inner sep=0pt,minimum size=2ex] (sum) [mylocation= #1 and #2 of #3] {};
\draw[semithick] (sum.north west) -- (sum.south east);
\draw[semithick] (sum.south west) -- (sum.north east);
\fill (sum.center) -- (sum.south west) arc (-135:-45:1ex) -- cycle;
}
\begin{document}
\begin{tikzpicture}
\node(A) at (0,0) {A};
\mydraw{above}{2}{A}
\mydraw{left}{2}{A}
\mydraw{right}{2}{A}
\mydraw{below}{2}{A}
\end{tikzpicture}
\end{document}