使用 tikz 创建的图片中的文本定位

使用 tikz 创建的图片中的文本定位

很抱歉出现上述问题,我是这个实用论坛的新手,我仍然需要帮助。

我编写了以下代码,以便绘制 2 个带阻尼器的耦合水平振荡器:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing,patterns}
\usetikzlibrary{calc,patterns,decorations.markings}
\usetikzlibrary{positioning}

\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=10pt,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,-3pt)$) -- ($(dmp.north)+(0,3pt)$);
  }
}, decorate]
\tikzstyle{ground}=[fill,pattern=north east lines,draw=none,minimum width=0.5cm,minimum height=0.3cm]

\begin{scope}[xshift=7cm]
\node (M) [minimum width=1cm, minimum height=1cm] {$m$} node[draw=none,fill=none,pos=.5,below] {1};

\draw [<-,thick](-2.6,.6) -- (-1.5,-.2);

\node (wall) [ground, rotate=-90, minimum width=2cm,yshift=-3cm] {};

\draw (wall.north east) -- (wall.north west);

\draw [spring] (wall.120) -- ($(M.north west)!(wall.120)!(M.south west)$) node[draw=none,fill=none,pos=.5,above] {$k_1$};

\draw [damper] (wall.20) -- ($(M.north west)!(wall.20)!(M.south west)$) node[draw=none,fill=none,pos=.5,below] {$d_1$};
\end{scope}

\begin{scope}[xshift=10cm]
\node (M1) [minimum width=1cm, minimum height=1cm] {$m$} node[draw=none,fill=none,pos=.5,below] {2};

\draw [spring] (M.10) -- ($(M1.north west)!(M.10)!(M1.south west)$) node[draw=none,fill=none,pos=.5,above] {$K$};

\draw [damper] (M.-40) -- ($(M1.north west)!(M.-40)!(M1.south west)$);

\draw [->,thick](1.4,-.2) -- (2.5,.6);
\end{scope}

\begin{scope}[xshift=10cm]
\node (wall1) [ground, rotate=90, minimum width=2cm,yshift=-3cm] {};

\draw (wall1.north west) -- (wall1.north east);

\draw [spring] (wall1.60) -- ($(M1.north east)!(wall1.60)!(M1.south east)$) node[draw=none,fill=none,pos=.5,above] {$k_2$};

\draw [damper] (wall1.-160) -- ($(M1.north east)!(wall1.-160)!(M1.south east)$) node[draw=none,fill=none,pos=.5,below] {$d_2$};
\end{scope}

\end{tikzpicture}

\end{document}

在此处输入图片描述

我需要帮助来解决以下问题:

  1. 两个减震器的标注“d_1”和“d_2”应位于减震器下方约 3 毫米处。
  2. 两个块的标记“1”和“2”应该放在矩形下方,而不是内部。
  3. 第二个阻尼器的右侧点应从右侧墙壁的垂直规则开始,而不是从那些东北线开始。

答案1

第一点和第二点很容易固定,您可以写下来below=<distance>,而对于第三点,您只需从(wall1.160)而不是画一条线即可(wall1.-160)

您还可以通过将所有样式命令放在\tikzset{...}顶部来稍微合理化您的代码(请参阅应该使用 \tikzset 还是 \tikzstyle 来定义 TikZ 样式?)并给出名字,我已经习惯了blank经常使用的风格draw=none,fill=none,pos=.5

在此处输入图片描述

完整代码如下:

    \documentclass{article}
    \usepackage{tikz}
    \usetikzlibrary{decorations.pathmorphing,patterns}
    \usetikzlibrary{calc,patterns,decorations.markings}
    \usetikzlibrary{positioning}

    \begin{document}

    \tikzset{
      spring/.style={thick,decorate,decoration={zigzag,pre length=0.3cm,post length=0.3cm,segment length=6}},
      blank/.style={draw=none,fill=none,pos=0.5},
      ground/.style={fill,pattern=north east lines,draw=none,minimum width=0.5cm,minimum height=0.3cm},
      damper/.style={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=10pt,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,-3pt)$) -- ($(dmp.north)+(0,3pt)$);
          }
          }, decorate
      }
    }

    \begin{tikzpicture}[every node/.style={draw,outer sep=0pt,thick}]
      \begin{scope}[xshift=7cm]
        \node (M) [minimum width=1cm, minimum height=1cm] {$m$} node[blank,below=0.7] {1};
        \draw [<-,thick](-2.6,.6) -- (-1.5,-.2);
        \node (wall) [ground, rotate=-90, minimum width=2cm,yshift=-3cm] {};
        \draw (wall.north east) -- (wall.north west);
        \draw [spring] (wall.120) -- ($(M.north west)!(wall.120)!(M.south west)$) node[blank,above] {$k_1$};
    \draw [damper] (wall.20) -- ($(M.north west)!(wall.20)!(M.south west)$) node[blank,below=0.2] {$d_1$};
  \end{scope}
  \begin{scope}[xshift=10cm]
    \node (M1) [minimum width=1cm, minimum height=1cm] {$m$} node[blank,below=0.7] {2};
    \draw [spring] (M.10) -- ($(M1.north west)!(M.10)!(M1.south west)$) node[blank,above] {$K$};
    \draw [damper] (M.-40) -- ($(M1.north west)!(M.-40)!(M1.south west)$);
    \draw [->,thick](1.4,-.2) -- (2.5,.6);
  \end{scope}
  \begin{scope}[xshift=10cm]
    \node[] (wall1) [ground, rotate=90, minimum width=2cm,yshift=-3cm] {};
    \draw (wall1.north west) -- (wall1.north east);
    \draw [spring] (wall1.60) -- ($(M1.north east)!(wall1.60)!(M1.south east)$) node[blank,above] {$k_2$};
    \draw [damper] (wall1.160) -- ($(M1.north east)!(wall1.-160)!(M1.south east)$) node[blank,below=0.2] {$d_2$};
  \end{scope}
\end{tikzpicture}

\end{document}

答案2

替换此

node[draw=none,fill=none,pos=.5,below] {$d_2$};

node[draw=none,fill=none,pos=.5,below,yshift=-2mm] {$d_2$};

和这个

node[draw=none,fill=none,pos=.5,below] {$d_1$};

node[draw=none,fill=none,pos=.5,below,yshift=-2mm] {$d_1$};

和这个

\draw [damper] (wall1.-160) -- ($(M1.north east)!(wall1.-160)!(M1.south east)$)

有了这个

\draw [damper] (wall1.160) -- ($(M1.north east)!(wall1.-160)!(M1.south east)$)

相关内容