如何相对于另一个节点定位一个节点?

如何相对于另一个节点定位一个节点?

在此处输入图片描述 [取自https://tex.stackexchange.com/a/396436/128718]

我想使用类似

\node[basic,anchor=north.west, xshift=5cm] {instructor.south.east}(department) {department,

代替

\node[basic,right=5cm of instructor] (department) {department  

north.west这意味着角departmentsouth.east角之间的 x 坐标距离instructor为 5 厘米。

MWE(取自@marmot)

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{positioning,shapes.multipart,shapes}
\begin{document}
\tikzset{basic/.style={
        draw,
        rectangle split,
        rectangle split parts=2,
        rectangle split part fill={blue!20,white},
        minimum width=2.5cm,
        text width=2cm,
        align=left,
        font=\itshape
    },
    Diamond/.style={ diamond, 
                        draw, 
                        shape aspect=2, 
                        inner sep = 2pt,
                        text centered,
                        fill=blue!10!white,
                        font=\itshape
                      }}
\begin{tikzpicture}
\node[basic] (instructor) {instructor
\nodepart{second}
\underline{ID}\\
name\\
salary};
\node[basic,right=5cm of instructor] (department) {department
%I want to 
%\node[basic,anchor=north,yshift=-3cm] (department)(instructor.south) {department
\nodepart{second}
\underline{dept\_name}\\
building\\
budget};
\draw (instructor) -- (department) node[midway,Diamond]{member};
\end{tikzpicture}
\end{document}

答案1

更新:我完全误解了你想要什么。语法是

\node[basic,right=5cm of instructor.south,anchor=north] (department) ...

以下是 MWE:

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{positioning,shapes.multipart,shapes}
\begin{document}
\tikzset{basic/.style={
        draw,
        rectangle split,
        rectangle split parts=2,
        rectangle split part fill={blue!20,white},
        minimum width=2.5cm,
        text width=2cm,
        align=left,
        font=\itshape
    },
    Diamond/.style={ diamond, 
                        draw, 
                        shape aspect=2, 
                        inner sep = 2pt,
                        text centered,
                        fill=blue!10!white,
                        font=\itshape
                      }}
\begin{tikzpicture}
\node[basic] (instructor) {instructor
\nodepart{second}
\underline{ID}\\
name\\
salary};
\node[basic,right=5cm of instructor.south,anchor=north] (department) {department
\nodepart{second}
\underline{dept\_name}\\
building\\
budget};
\draw (instructor) -- (department) node[midway,Diamond]{member};
\end{tikzpicture}
\end{document}

在此处输入图片描述

原始答案:我以为你想合并Torbjørn 的精彩回答使用“基本”样式。这是可能的,例如

\documentclass[tikz,border=3mm]{standalone}
\usepackage[simplified]{pgf-umlcd} %<- added simplified here in order to avoid
% empty splits
% redefine enviroment
\renewenvironment{class}[3][]%
{
\begin{classAndInterfaceCommon}{#1}{#2}{#3}
}%
{\calcuateNumberOfParts{}
% Thorbjorns only change was in the following line, where "anchor=north" was moved before "this umlcd style"
% but I replaced "this umlcd style" by "basic=\theumlcdClassSplitPartNum"
% and changed the way you define the position. Essentially you need to put 
% brackets around
\node[anchor=north,basic=\theumlcdClassSplitPartNum] (\umlcdClassName) at \umlcdClassPos
    {\textbf{\umlcdClassName}
\insertAttributesAndOperations{}
};

\end{classAndInterfaceCommon}
}
\usetikzlibrary{positioning,shapes.multipart,shapes}
\tikzset{basic/.style={
        draw,
        rectangle split,
        rectangle split parts=#1,
        rectangle split part fill={blue!20,white},
        minimum width=2.5cm,
        text width=2cm,
        align=left,
        font=\itshape
    },
    Diamond/.style={ diamond, 
                        draw, 
                        shape aspect=2, 
                        inner sep = 2pt,
                        text centered,
                        fill=blue!10!white,
                        font=\itshape
                      }}
\begin{document}
\begin{tikzpicture}
\begin{class}{foo}{(0,0)}
\attribute{+ Public}
\end{class}

\begin{class}{bar}{([yshift=-10mm]foo.south)}
\attribute{+ balance : Dollars}
\operation{+ deposit( amount : Dollars )}
\end{class}

\begin{class}[anchor=west]{baz}{([xshift=20mm]bar.east)}
\end{class}
\end{tikzpicture}
\end{document} 

在此处输入图片描述

此代码的工作原理与 Torbjørn 的代码基本相同,只是您现在必须将所有放置信息放在第三个参数中,并用括号括起来,上面提供了示例。作为奖励,如果没有必要,节点将不再被拆分。显然,根据您想要执行的操作,您可能希望增加text width节点的数量等等。

相关内容