我有以下基于的 tikz 代码https://tex.stackexchange.com/a/13952/15360。现在我想给弹簧、阻尼器和 3 个箭头贴上标签。例如,F 表示力,k 表示弹簧常数,d 表示阻尼器常数。
我必须使用固定坐标吗\node[draw] at (0,0) {$F$};
??
或者我可以做得更聪明吗?因为目前弹簧不是节点,所以我没有它的坐标......
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,patterns,decorations.pathmorphing,decorations.markings}
\begin{document}
\begin{tikzpicture}[every node/.style={draw,outer sep=0pt,thick}]
\tikzstyle{spring}=[thick,decorate,decoration={zigzag,pre length=0.3cm,post
length=0.3cm,segment length=6}]
\tikzstyle{damper}=[thick,decoration={markings,
mark connection node=dmp,
mark=at position 0.5 with
{
\node (dmp) [thick,inner sep=0pt,transform shape,rotate=-90,minimum
width=15pt,minimum height=3pt,draw=none] {};
\draw [thick] ($(dmp.north east)+(2pt,0)$) -- (dmp.south east) -- (dmp.south
west) -- ($(dmp.north west)+(2pt,0)$);
\draw [thick] ($(dmp.north)+(0,-5pt)$) -- ($(dmp.north)+(0,5pt)$);
}
}, decorate]
\tikzstyle{ground}=[fill,pattern=north east lines,draw=none,minimum
width=0.75cm,minimum height=0.3cm]
\node (M1) [minimum width=1.5cm, minimum height=1.5cm] {$m_1$};
\node (M2) at (3,0) [minimum width=1.5cm, minimum height=1.5cm] {$m_2$};
\draw[spring] ($(M1.east) - (0,0.5)$) -- ($(M2.west) - (0,0.5)$);
\draw[damper] ($(M1.east) + (0,0.5)$) -- ($(M2.west) + (0,0.5)$);
\draw[thick, dashed] ($(M1.north west)$) -- ($(M1.north west) + (0,1)$);
\draw[thick, dashed] ($(M2.north west)$) -- ($(M2.north west) + (0,1)$);
\draw[ultra thick, -latex] ($(M2.north west) + (0,0.75)$) --
($(M2.north west) + (1,0.75)$);
\draw[ultra thick, -latex] ($(M1.north west) + (0,0.75)$) --
($(M1.north west) + (1,0.75)$);
\draw[ultra thick, -latex] ($(M1.west) - (1,0)$) -- ($(M1.west)$);
\end{tikzpicture}
\end{document}
答案1
如果我理解你的问题正确的话,这很容易做到。我在下面提供了两个示例,通过修改你的代码,标记你的弹簧和一个箭头。例如
\draw[spring] ($(M1.east) - (0,0.5)$) -- ($(M2.west) - (0,0.5)$)
你可以加
node [midway,above] {$k$};
将节点置于其上方和之间
($(M1.east) - (0,0.5)$)
和($(M2.west) - (0,0.5)$)
下面的代码
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,patterns,
decorations.pathmorphing,
decorations.markings}
\begin{document}
\begin{tikzpicture}
\tikzstyle{spring}=[thick,decorate,decoration={zigzag,pre length=0.3cm,post
length=0.3cm,segment length=6}]
\tikzstyle{damper}=[thick,decoration={markings,
mark connection node=dmp,
mark=at position 0.5 with
{
\node (dmp) [thick,inner sep=0pt,transform shape,rotate=-90,minimum
width=15pt,minimum height=3pt,draw=none] {};
\draw [thick] ($(dmp.north east)+(2pt,0)$) -- (dmp.south east) -- (dmp.south
west) -- ($(dmp.north west)+(2pt,0)$);
\draw [thick] ($(dmp.north)+(0,-5pt)$) -- ($(dmp.north)+(0,5pt)$);
}
}, decorate]
\tikzstyle{ground}=[fill,pattern=north east lines,draw=none,minimum
width=0.75cm,minimum height=0.3cm]
\node[draw,outer sep=0pt,thick] (M1) [minimum width=1.5cm, minimum height=1.5cm] {$m_1$};
\node[draw,outer sep=0pt,thick] (M2) at (3,0) [minimum width=1.5cm, minimum height=1.5cm] {$m_2$};
\draw[spring] ($(M1.east) - (0,0.5)$) -- ($(M2.west) - (0,0.5)$)
node [midway,above] {$k$};
\draw[damper] ($(M1.east) + (0,0.5)$) -- ($(M2.west) + (0,0.5)$);
\draw[thick, dashed] ($(M1.north west)$) -- ($(M1.north west) + (0,1)$);
\draw[thick, dashed] ($(M2.north west)$) -- ($(M2.north west) + (0,1)$);
\draw[ultra thick, -latex] ($(M2.north west) + (0,0.75)$) --
($(M2.north west) + (1,0.75)$)
node [midway, below] {$F_x$};
\draw[ultra thick, -latex] ($(M1.north west) + (0,0.75)$) --
($(M1.north west) + (1,0.75)$);
\draw[ultra thick, -latex] ($(M1.west) - (1,0)$) -- ($(M1.west)$);
\end{tikzpicture}
\end{document}
给出 。
请注意,我将盒装节点的样式从环境移到tikzpicture
了节点本身,以避免标签周围出现方框。
另外,如果添加定位库:
\usetikzlibrary{positioning}
您可以根据现有的命名节点来定位新节点:
\node (label1) [below=of M1] {A label};
有关节点放置的更多信息,您可以查看 PGF 手册中的第 16.5 章。