首先,使用语法\node at (coord) {}'
还是更好\node[right=of coord] {}
?我更喜欢将节点的位置放在外面[]
,所以我尝试使用定位库语法,例如:\node[] at ([right]coord) {};
,这可能吗?
如果我想使用on grid
,我相信只需使用 即可获得此行为x/y-shift
。但我想要做的是根据节点朝向彼此设置 x-shift,就像on grid
关闭时定位所做的那样。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node(start) at (0,0) {Hello World};
%\node[anchor=west](end) at ([xshift=1]start.east) {Over here!}; % I like this style of syntax, explicitly using 'at'
\node[right=of start] (end) {Over here!}; % using positioning library
\node[draw=red] (end) at ([right]start) {Over here!}; % can I use positioning library style in this way? the positioning is not the same as above, this node is overlayed with start
\end{tikzpicture}
\end{document}
答案1
这需要对坐标解析器进行新的实现。定位键right
也需要进行调整,以便与此配合使用。
实现起来要容易得多AT ([right]start)
,即使用不同的关键字,将其转换([<pos>=<dist>]<coord>)
为\tikzset{<pos>=<dist> of <coord>}
。
代码
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{positioning}
\makeatletter
\let\orig@tikz@@scan@fig\tikz@@scan@fig
\def\tikz@@scan@fig{\pgfutil@ifnextchar A\tikz@fig@scan@AT\orig@tikz@@scan@fig}
\def\tikz@fig@scan@AT AT#1(#2[#3]#4){%
\pgfutil@in@{=}{#3}%
\ifpgfutil@in@\tikzset{#3 of #4}\else\tikzset{#3=of #4}\fi
\tikz@@scan@fig}
\makeatother
\begin{document}
\begin{tikzpicture}
\node(start) at (0,0) {Hello World};
\node[right=of start] (end) {Over here!};
\node[draw=red] (end) AT ([right] start) {Over here!};
\end{tikzpicture}
\end{document}
答案2
正如我在评论中指出的那样,我的第一印象是,您的问题是功能请求,应该向tikz
软件包维护者提出。我的第二印象是,您的请求没有多大意义(如果有的话)。
为了澄清上述内容,以下是一些现有的节点放置方式,它们也可以组合在一起:
- 节点放置在路径上:
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}[
N/.style = {draw, ultra thin, minimum height=4ex}
]
\path (0,0) node [N] {Hello World}
(4,0) node [N] {Nodes on given position};
\end{tikzpicture}
\end{document}
- 另一种可能性是按以下顺序排列节点:
节点之间的恒定距离(由
node distance
边界决定),或节点放置在网格上(网格的距离由决定
node distance
)两种情况下都可以使用
chains
库。
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{chains,
positioning}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}[
node distance = 2mm,
start chain = going right,
N/.style = {draw, ultra thin, minimum height=4ex, on chain}
]
\node [N] (start) {Hello World};
\node [N] (end) {Nodes on \qty{2}{\mm} distances};
\end{tikzpicture}
\qquad
\begin{tikzpicture}[
node distance = 30mm, on grid,
start chain = going right,
N/.style = {draw=red, ultra thin, minimum height=4ex, going right}
]
\node [N] (start) {Hello World};
\node [N] (end) {Nodes on \qty{30}{\mm} grid};
\end{tikzpicture}
\end{document}
在形成节点序列时不需要使用chains
库(当序列中有大量节点时它实际上有意义),你可以使用仅定位语法:
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}[
node distance = 2mm,
N/.style = {draw, ultra thin, minimum height=4ex}
]
\node [N] (start) {Hello World};
\node [N, right=of start] (end) {Nodes on \qty{2}{\mm} distances}; % I like this style of syntax, explicitly using 'at'
\end{tikzpicture}
\qquad
\begin{tikzpicture}[
node distance = 30mm, on grid,
N/.style = {draw=red, ultra thin, minimum height=4ex}
]
\node [N] (start) {Hello World};
\node [N, right=of start] (end) {Nodes on \qty{30}{\mm} grid}; % I like this style of syntax, explicitly using 'at'
\end{tikzpicture}
\end{document}
两个 MWE 产生相同的结果,
如果你想将节点移出给定的坐标,那么你可以这样写:
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}[
N/.style = {draw, ultra thin, minimum height=4ex}
]
\node [N, right] at (0,0) {Hello World};
\node [N, right] at (3,0) {Nodes on given position};
\end{tikzpicture}
\end{document}
上述内容中究竟选择什么,取决于个人喜好和图像内容。
答案3
我不确定你为什么要这样做,但你可以拆分节点标签和定位,如下所示:
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node(start) at (0,0) {Hello World};
\node[right=of start] (end) {Over here!}; % using positioning library
\node[draw=red] (end2) [right=of start] {Over here!}; % this will be overlaid with "end"
\end{tikzpicture}
\end{document}