使用 pgfkeys 参数化的机械货车

使用 pgfkeys 参数化的机械货车

在此处输入图片描述我想用参数化方法为物理书绘制一辆机械马车。这些应该是参数:

#0 name,对象的名称,例如 mywagon

货车的 #1 位置作为坐标,中心位于地面高度下方,例如 (4.5,0)

#2 (可选)质量的宽度,例如 5,默认值为 3

#3 (可选)质量高度,例如 4,默认 2/3*宽度

#4 (可选)wheel:车轮直径,默认 1/5*宽度

#5 (可选) 间隙:质量边缘和车轮之间的间隙,默认为 1/10*车轮

#6(可选)重叠:质量和车轮的重叠,默认 1/3*车轮

tikz 中的调用看起来应该像这样:

\begin{tikzpicture}
   \draw[ground] (0,0) -- (9,0);%ground
   \wagoon[name=mywagon, width=5, height=4.2, wheel=1.1, gap=0.6, overlap=0.4] at (4.5,0);
   \draw[rope] mywagon-mass.east -- ++(1,0);
   \draw[rope] mywagon-mass.west -- ++(-1,0);
   \draw[arrow] mywagon-wheel-2.center -- ++(2,0);
   \draw[ground] (0,-6) -- (9,-6);%ground
   \wagon[name=mywagon2, width=5, very thick, red] at (4.5,-6);%with defaults of other optional parameters and then additional very thick and red
\end{tikzpicture}

货车应由正常节点组成:Ohne 矩形和两个圆圈

在进一步使用过程中,应该可以通过 object.anchor 符号访问图纸中的锚点。这很重要,例如,之后将绳索连接到马车上。

里面的子节点的名字应该是:

<name>-mass 表示质量,例如 mywagon-mass

<name>-wheel-1 和 <name>-wheel-2 表示车轮,例如 mywagon-wheel-1 和 mywagon-wheel-2

其中 <name> 是参数 #0 的值,名为 name

当然,我可以对这辆货车进行非参数化的绘图,但我认为这在这里没有帮助,反而很烦人,因为你可能会在绘图结构上采用我的错误方法。

重要的是,货车的 tikz 代码存储在序言中,以便该代码也可在其他图纸中使用。

\documentclass[border=10pt]{standalone}%
\usepackage{tikz}
\usetikzlibrary{arrows}
\tikzset{
  mass/.style={rectangle, draw, thick, fill=gray!25},%
  wheel/.style={circle, draw, thick, fill=white}},%
  wheelaxle/.style={circle, draw, fill}},%
  rope/.style={blue, thick},%
  ground/.style={thick},%
  arrow/.style={-latex, thick},%
}
% global tikz-code here

\begin{document}
\begin{tikzpicture}
   \draw[ground] (0,0) -- (9,0);%ground
   %\wagoon[name=mywagon, width=5, height=4.2, wheel=1.1, gap=0.6, overlap=0.4] at (4.5,0);
   \draw[rope] mywagon-mass.east -- ++(1,0);
   \draw[rope] mywagon-mass.west -- ++(-1,0);
   \draw[arrow] mywagon-wheel-2.center -- ++(2,0);
   \draw[ground] (0,-6) -- (9,-6);%ground
   %\wagoon[name=mywagon2, width=5, very thick, red] at (4.5,-6);%with defaults of other optional parameters and then additional very thick and red
\end{tikzpicture}
\end{document}

答案1

由于您使用的是节点,因此使用长度而不是无单位值要容易得多,而无单位值就是我在这里使用的。

如果您想要一个只需输入与货车宽度或车轮直径相关的比率的界面……这也是可能的。

的实现\tikzwaggon需要一个[]参数,并且不允许在之后使用at (coordinate)或。(name)]

在 pic 中定义了以下节点waggon

  • (<name>-mass)(长方形),
  • (<name>-left wheel)(圆圈),
  • (<name>-right wheel)(圆圈),
  • (<name>-left axle)(圆圈),
  • (<name>-right axle)(圆圈),
  • (<name>-left rope)(坐标)和
  • (<name>-right rope)(协调)

<name>图片的名字是哪里?

代码

\documentclass[tikz]{standalone}
\tikzset{
  waggon width/.initial   = 3cm,
  waggon height/.initial  = .6667*(\pgfkeysvalueof{/tikz/waggon width}),
  waggon wheel diameter/.initial = .2*(\pgfkeysvalueof{/tikz/waggon width}),
  waggon gap/.initial     = .1   *(\pgfkeysvalueof{/tikz/waggon wheel diameter}),
  waggon overlap/.initial = .3333*(\pgfkeysvalueof{/tikz/waggon wheel diameter}),
  waggon rope length/.initial    = 1cm,
  waggon axle diameter/.initial  = +1pt,
  waggon/rope/.style=line to,
  waggon/axle/.style={
    fill, shape=circle, inner sep=+0pt, outer sep=auto,
    name=-#1 axle, minimum size=\pgfkeysvalueof{/tikz/waggon axle diameter}},
  waggon/mass/.style={
    draw, shape=rectangle, inner sep=+0pt, outer sep=auto,
    minimum width=\pgfkeysvalueof{/tikz/waggon width},
    minimum height=\pgfkeysvalueof{/tikz/waggon height},
    at=(up:{\pgfkeysvalueof{/tikz/waggon wheel diameter}
          -(\pgfkeysvalueof{/tikz/waggon overlap})}),
    anchor=south, name=-mass, node contents=},
  waggon/wheel/.style={
    draw, shape=circle, inner sep=+0pt, outer sep=auto,
    minimum size=\pgfkeysvalueof{/tikz/waggon wheel diameter},
    at=(#1:{.5*(\pgfkeysvalueof{/tikz/waggon width})
               -\pgfkeysvalueof{/tikz/waggon gap}
           -.5*(\pgfkeysvalueof{/tikz/waggon wheel diameter})}),
    anchor=south, name=-#1 wheel, label={[waggon/axle=#1]center:},
    node contents=},
  waggon/.pic={
    \tikzset{transform shape}
    \node[waggon/mass];
    \foreach\t in{left, right} \node[waggon/wheel=\t];
    \foreach\t/\tt in{west/left, east/right}
    \draw[waggon/rope=\tt] (-mass.\t)
      to +(\t:{\pgfkeysvalueof{/tikz/waggon rope length}})
      coordinate (-\tt\space rope);},
    @waggon/.style={
      pic type=waggon,
      height/.style={waggon height={##1}},
      width/.style={waggon width={##1}},
      wheel/.style={waggon wheel diameter={##1}},
      gap/.style={waggon gap={##1}},
      overlap/.style={waggon overlap={##1}},
      axle/.style={waggon axle diameter={##1}}},
}
\def\tikzwaggon#1[{\pic#1[@waggon,}
\begin{document}
\tikz[sloped, column sep=5mm]
  \matrix{
    \draw (0,0) -- pic(s){waggon} (right:5);
    \draw[green, ->] (s-left axle) -- +(right:1cm);
    &
    \draw (0,0) -- pic[waggon width       = 2cm,
                       waggon height      = 1cm,
                       waggon gap         = 3mm,
                       waggon rope length = 5mm,
                       waggon/rope/.style={loop ####1},
                       waggon/mass/.append style={
                         rounded corners=5mm,
                         fill=red!50!orange,
                         label={center:\tiny Wurst on Wheels}},
                       waggon/wheel/.append style={fill=blue!50}]{waggon} (60:5);
  \\};

\begin{tikzpicture}
\tikzwaggon (s) at (4.5, 0) [gray!50, line width=2mm, axle=5pt];
\foreach \anchor/\placement in {
     -mass.west/above, -left rope/below left,
     -mass.east/above, -right rope/below right,
     -left axle/above, -right axle/above,
     -left wheel.south west/left, -right wheel.south east/right}
  \draw[shift=(s\anchor)]plot[mark=x]coordinates{(0,0)}
   node[inner sep=0pt,outer sep=5pt,\placement]
       {\scriptsize\ttfamily(s\anchor)};
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述 在此处输入图片描述

答案2

我修复了大部分错误。我选择将参数设为宏。您可以改用 tikz 键或长度寄存器,但宏更简单。定义将局限于 tikzpicture。

默认情况下,节点按中心定位。您可以使用其他锚点,但仍需要计算位置。

\documentclass[border=10pt]{standalone}%
\usepackage{tikz}
\usetikzlibrary{arrows, calc}
\tikzset{
  mass/.style={rectangle, draw, thick, fill=gray!25},%
  wheel/.style={circle, draw, thick, fill=white},%
  wheelaxle/.style={circle, draw, fill},%
  rope/.style={blue, thick},%
  ground/.style={thick},%
  arrow/.style={-latex, thick},%
}
% global tikz-code here

\begin{document}
\begin{tikzpicture}[every node/.style={inner sep=0pt}]
  \def\height{4.2cm}% or use \pgfmathsetlengthmacro{\height}{4.2cm}
  \def\width{5cm}
  \def\wheel{1.1cm}% diameter
  \def\gap{0.6cm}
  \def\overlap{0.4cm}
  
   \draw[ground] (0,0) -- (9,0);%ground
   \node[draw, minimum width={\width}, minimum height={\height}] (mywagoon-mass) at (4.5, 0.5*\height+\overlap) {};
   \draw[rope] (mywagoon-mass.east) -- ++(1,0);
   \draw[rope] (mywagoon-mass.west) -- ++(-1,0);
   \path (mywagoon-mass.south west) ++(\gap+0.5*\wheel, 0.5*\wheel-\overlap)  node[wheel, minimum height={\wheel}] (mywagoon-wheel-2) {};
   \draw[arrow] (mywagoon-wheel-2.center) -- ++(2,0);
   %\draw[ground] (0,-6) -- (9,-6);%ground
   %\wagoon[name=mywagoon2, width=5, very thick, red] at (4.5,-6);%with defaults of other optional parameters and then additional very thick and red
\end{tikzpicture}
\end{document}

演示

相关内容