如何修正Tikz零尺寸节点连线未达实际点问题(不使用绝对坐标)
TikZ零尺寸节点连线终止问题的相对坐标解决方案
当你用TikZ创建无内容的空节点(仅标记坐标系点位)时,连线会在实际点位前终止,这是因为空节点默认带有内外边距,连线会停在节点边界而非坐标点上。以下是两种不依赖绝对坐标、仅用相对坐标的修正方案:
方案一:用coordinate替代空node
TikZ的coordinate是专门用于标记点位的元素,本身没有尺寸和边距,连线会直接连接到坐标点,是最适配需求的方案。只需将代码中的空节点定义替换为coordinate:
原空节点写法:
\node at(0,0) (origin) {}; \node[right = 2cm of y1] (yz1) {};
修改为:
\coordinate (origin) at (0,0); \coordinate (yz1) at ([right=2cm of y1]);
方案二:消除空节点的边距
如果必须使用node,可以通过设置参数强制消除节点的内外边距,让节点边界与坐标点重合:
\node[inner sep=0pt, outer sep=0pt] at(0,0) (origin) {}; \node[inner sep=0pt, outer sep=0pt, right=2cm of y1] (yz1) {};
修改后的完整代码示例(使用coordinate版本)
\coordinate (origin) at (0,0); \node[below = 0.0cm of origin]{$(0,0)$}; \node[above = 3cm of origin] (y) {$y$}; \node[right = 3cm of origin] (z) {$z$}; \node[above = 2cm of origin, left] (y1) {$1$}; \node[right = 2cm of origin, below] (z1) {$1$}; \coordinate (yz1) at ([right=2cm of y1]); \coordinate (endgz) at ([above=2.5cm of z]); \coordinate (yz) at ([above=3cm of z]); \coordinate (p0) at ([above=1cm of origin]); \draw[fill](p0) circle(0.06cm); \node[left=0cm of p0]{$g(0)=p0$}; \draw[fill](yz1) circle(0.06cm); \draw[->,thick] (origin) -- (y); \draw[->,thick] (origin) -- (z); \draw (origin) -- node [above = 1cm] {$y(z)=z$} (yz); \draw (p0) .. controls +(0:1cm) and +(205:1cm) .. (endgz) node [below] {$g(z)$} ; \draw (y1) -- (yz1); \draw (z1) -- (yz1);
此版本完全基于相对坐标体系,后续调整图形布局时,只需修改相对距离参数即可,无需改动绝对坐标。
内容的提问来源于stack exchange,提问作者mknaranja
相关产品推荐
相关产品推荐

