我尝试使用相对坐标和符号++(x,y)
,通常效果很好。但在下面的例子中却不行。我想定义相对于矩阵中心的节点,我在下面代码的末尾做了这个,\node [font=\color{blue}] at (m.center)++(1,-3) {$4$};
我以为这会起作用,但事实并非如此。如果删除此行,一切都会好起来。我该怎么做?
\documentclass[border=10pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix,fit}
\begin{document}
\begin{tikzpicture}
\matrix(m) [matrix of math nodes, nodes in empty cells, nodes={minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex}]
{
7 & & 4 & \\
& & & \\
8 & & 3 & \\
& & & \\
};
\draw (m-1-1.north west) rectangle (m-4-4.south east);
\foreach \i in {2,4} {
\draw (m-\i-1.south west) -- (m-\i-4.south east);
}
\foreach \j in {2,4} {
\draw (m-1-\j.north east) -- (m-4-\j.south east);
}
% dual ui
\node [font=\color{blue}] at (3,1) {$5$};
\node [font=\color{blue}] at (3,-1) {$6$};
% dual vj
\node [font=\color{blue}] at (-1,-3) {$14$};
% \node [font=\color{blue}] at (1,-3) {$4$};
% Testing new notation
\node [font=\color{blue}] at (m.center) {$4$};
% This last line is not valid for some reason
\node [font=\color{blue}] at (m.center)++(1,-3) {$4$};
\end{tikzpicture}
\end{document}
答案1
通常最好使用
\path (x,y) node{bla};
比使用
\node {bla} at (x,y);
您所经历的只是原因之一。使用第一个语法,一切都按预期工作。第二个语法实际上需要一种特定的语法,您仍然可以使用库calc
或使用移位来使其工作,但这些只是使功能较弱的语法工作的技巧。
\documentclass[border=10pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix,fit}
\begin{document}
\begin{tikzpicture}
\matrix(m) [matrix of math nodes, nodes in empty cells, nodes={minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex}]
{
7 & & 4 & \\
& & & \\
8 & & 3 & \\
& & & \\
};
\draw (m-1-1.north west) rectangle (m-4-4.south east);
\foreach \i in {2,4} {
\draw (m-\i-1.south west) -- (m-\i-4.south east);
}
\foreach \j in {2,4} {
\draw (m-1-\j.north east) -- (m-4-\j.south east);
}
% dual ui
\node [text=blue] at (3,1) {$5$};
\node [text=blue] at (3,-1) {$6$};
% dual vj
\node [text=blue] at (-1,-3) {$14$};
% \node [text=blue] at (1,-3) {$4$};
% Testing new notation
\node [text=blue] at (m.center) {$4$};
% This last line is not valid for some reason
\path (m.center)++(1,-3) node [text=blue] {$4$};
\end{tikzpicture}
\end{document}