您好,我试图绘制一个图形,其中我必须显示描述我的问题几何形状的向量。因此,我想延长用于描述机制的线段(此处为 AD),我将shorten >=-2cm
其延长固定量。这会产生第一个问题,因为不仅要绘制向量,还要在线段的顶部绘制,另一个问题是我找到的标记向量的解决方案 - 我想要一个围绕箭头尖端的节点 - 我必须手动调整值pos
以放置它,因为它会随着长度(AD)而变化
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc}
\usetikzlibrary{through,intersections}
\usepackage{tkz-euclide,esvect}
\tikzset{
Points/.style={circle,draw,line width=1pt,minimum width=.4cm,fill=white},
Vect/.style={line width=.5pt,shorten >=-2cm,-stealth},
}
\begin{document}
\begin{tikzpicture}
\node[Points] (PtA) at (5,2) {} ;
\node[Points] (PtB) at (8,4) {} ;
\node[Points] (PtD) at ($(PtB)+(-90:3)$) {} ;
\node[above=0.3cm] at (PtA) {A};
\node[below=0.3cm] at (PtB) {B};
\node[below=0.3cm] at (PtD) {D};
\draw[red, line width=2pt] (PtA) -- (PtD);
\draw[Vect] (PtA) -- (PtD) node[pos=1.47] {$\vv{z_2}$};
\end{tikzpicture}
\end{document}
在该图像上,您还可以看到延长的结果不在独立图形的框架内,因为您几乎看不到箭头的尖端。
答案1
代码
\documentclass[tikz]{standalone}
\usepackage{esvect}
\usetikzlibrary{arrows.meta, calc, quotes}
\tikzset{
Points/.style={circle, draw, line width=1pt, minimum width=.4cm},
Vect/.style={
line width=.5pt, -Stealth, sloped, at end,
to path={(\tikztotarget)--($(\tikztotarget)!-2cm!(\tikztostart)$)\tikztonodes}}}
\begin{document}
\begin{tikzpicture}
\node[Points, "A" ] (PtA) at (5,2) {};
\node[Points, "B" below] (PtB) at (8,4) {};
\node[Points, "D" below] (PtD) at ($(PtB)+(-90:3)$) {} ;
\draw[red, line width=2pt] (PtA) -- (PtD);
\draw[Vect] (PtA) to["$\vv{z_2}$"] (PtD);
\end{tikzpicture}
\end{document}
输出
答案2
- 在连接器前绘制矢量(红线)。我必须为此定义坐标
- 用于
[sloped, above]
矢量标签 - 定义一个
ExtensionFactor
,指定延伸部分相对于 A 和 D 之间的距离的长度。使用该值计算标签位置,使其位于延伸部分的中间
第 2 步也以某种方式消除了边框边距问题 :)
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc}
\usetikzlibrary{through,intersections}
\usepackage{tkz-euclide,esvect}
\tikzset{
Points/.style={circle,draw,line width=1pt,minimum width=.4cm,fill=white},
Vect/.style={line width=.5pt,-stealth},
}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro\ExtensionFactor{0.5}
\coordinate (PtA) at (5,2);
\coordinate (PtB) at (8,4);
\coordinate (PtD) at ($(PtB)+(-90:3)$);
\coordinate (PtDex) at ($(PtD)+\ExtensionFactor*(PtD)-\ExtensionFactor*(PtA)$);
\draw[Vect] (PtA) -- (PtDex) node [pos=(1+0.5*\ExtensionFactor)/(1+\ExtensionFactor), sloped, above] {$\vv{z_2}$};
\node[Points] (PtA) at (PtA) {};
\node[Points] (PtB) at (PtB) {};
\node[Points] (PtD) at (PtD) {};
\node[above=0.3cm] at (PtA) {A};
\node[below=0.3cm] at (PtB) {B};
\node[below=0.3cm] at (PtD) {D};
\draw[red, line width=2pt] (PtA) -- (PtD);
\end{tikzpicture}
\end{document}