我正在尝试使用 Tikz 为我的学生说明两个相似盒子周围的剪切流分布。结果应类似于以下两个示例:
通过之前的尝试,我已经有了相应框的图示。以下是右侧框的图示:
\begin{tikzpicture}[
x=1.00cm,
y=1.00cm,
scale=2.0,
t1 line/.style={line width=1.0pt,line cap=butt},
pile/.style={thin, dashed, ->, >=stealth', shorten <=4pt, shorten >=4pt},
every node/.style={color=black}
]
\def\H{0.75}
\def\B{2}
\coordinate (K1) at (2*\B , \H);
\coordinate (K2a) at (8*\B/5, \H);
\coordinate (K2b) at (2*\B/5, \H);
\coordinate (K3) at ( 0.0, \H);
\coordinate (K4) at ( 0.0,0.0);
\coordinate (K5a) at (2*\B/5,0.0);
\coordinate (K5b) at (8*\B/5,0.0);
\coordinate (K6) at (2*\B ,0.0);
% Linien Umfang
\draw[t1 line] (K1.center) -- (K2a.center) node[above, midway] {};
\draw[t1 line] (K2a.center) -- (K2b.center) node[above, midway] {};
\draw[t1 line] (K2b.center) -- (K3.center) node[above, midway] {};
\draw[t1 line] (K3.center) -- (K4.center) node[left, midway] {};
\draw[t1 line] (K4.center) -- (K5a.center) node[below, midway] {};
\draw[t1 line] (K5a.center) -- (K5b.center) node[below, midway] {};
\draw[t1 line] (K5b.center) -- (K6.center) node[below, midway] {};
\draw[t1 line] (K6.center) -- (K1.center) node[left, midway] {};
% Linien Stege
\draw[t1 line] (K2a.center) -- (K5b.center) node[left, midway] {};
\draw[t1 line] (K2b.center) -- (K5a.center) node[left, midway] {};
\end{tikzpicture}
现在我问自己,合并蓝色矩形的最佳方法是什么?“手动”编码的努力似乎有点太高了。是否可以使用已定义的节点来定义蓝色矩形?有没有更优雅的方法?
答案1
一个想法:
\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc}
\definecolor{bluei}{RGB}{153,204,255}
\definecolor{blueii}{RGB}{51,102,255}
\newcommand\Rect[2]{%
\draw[draw=blueii,fill=bluei] #1 rectangle node {+} #2 {};
}
\def\Mult{\dfrac{M_{T}}{a^{2}}}
\begin{document}
\begin{tikzpicture}[scale=2]
\def\UL{0,0} % Upper left vertex of the rectangle
\def\LR{4,-1.5} % Lower right vertex of the rectangle
% The width (or height) of the blue rectangles
\def\hwi{15pt}
\def\hwii{7pt}
\def\hwiii{7pt}
\def\hwiv{12pt}
\coordinate (ul) at (\UL);
\coordinate (lr) at (\LR);
\coordinate (iul) at ([xshift=15pt]\UL); % Upper coordinate of left inner line
\coordinate (ilr) at ([xshift=-15pt]\LR); % Lower coordinate of right inner line
% The blue rectangles
\Rect{(iul)}{([xshift=\hwi]iul|-lr)}
\Rect{(ilr)}{([xshift=-\hwi]ilr|-ul)}
\Rect{([xshift=-\hwii]ul)}{(ul|-lr)}
\Rect{([xshift=\hwii]lr)}{(lr|-ul)}
\Rect{(ul)}{([yshift=\hwiii]iul)}
\Rect{(ul|-lr)}{([yshift=-\hwiii]iul|-lr)}
\Rect{(ilr)}{([yshift=-\hwiii]lr)}
\Rect{(ilr|-ul)}{([yshift=\hwiii]lr|-ul)}
\Rect{(iul)}{([yshift=\hwiv]ilr|-ul)}
\Rect{(iul|-lr)}{([yshift=-\hwiv]ilr)}
% The outer black rectangle with inner lines
\begin{scope}[line width=1.3pt,line cap=rec]
\draw (ul) -| (lr) -| (ul);
\draw (iul) -- (iul|-lr);
\draw (ilr) -- (ilr|-ul);
\end{scope}
% The labels and dashed lines
\path (ul) to node[gray] {$0,0461\Mult$} (lr);
\node[anchor=north east]
at (ul|-lr) {$0,0329\Mult$};
\node[anchor=north west]
at (lr) {$0,0329\Mult$};
\node[anchor=north]
at ([yshift=-1.7*\hwiv]$(ul|-lr)!0.5!(lr)$) (low)
{$0,0461\Mult-0,0329\Mult=0,0132\Mult$};
\draw[dashed] ([xshift=.5*\hwi,yshift=-32pt]iul) -- (low.160);
\draw[dashed] ([xshift=-.5*\hwi,yshift=-32pt]ilr|-iul) -- (low.20);
\end{tikzpicture}
\end{document}
一些评论:
让我们以问题右侧的图像为例。要绘制此图像,您只需设置四个坐标:两个坐标表示矩形的一对相对顶点,另外两个坐标用于内线。
以下代码显示这(ul)
是左上顶点,(lr)
这是右下顶点,(iul)
设置了内左线的坐标,(ilr)
设置了内右线的坐标;为了清晰起见,添加了一些颜色和一些节点和标签。
注意垂直坐标系是如何绘制内线的(该坐标系也将用于绘制蓝色矩形):
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=2]
\def\UL{0,0} % Upper left vertex of the rectangle
\def\LR{4,-1.5} % Lower right vertex of the rectangle
\coordinate (ul) at (\UL);
\coordinate (lr) at (\LR);
\coordinate (iul) at ([xshift=15pt]\UL); % Upper coordinate of left inner line
\coordinate (ilr) at ([xshift=-15pt]\LR); % Lower coordinate of right inner line
% Show the coordinates
\foreach \coord/\pos in {ul/above left,lr/below right,iul/above,ilr/below}
\node[label=\pos:\coord,inner sep=2pt,circle,fill] at (\coord) {};
% The rectangle
\draw[cyan] (ul) -| (lr) -| (ul);
% The inner lines
\draw[orange] (iul) -- (iul|-lr);
\draw[magenta] (ilr) -- (ilr|-ul);
\end{tikzpicture}
\end{document}
同样,要绘制每个蓝色矩形,只需要指定一对相对的顶点,因此我们为此定义一个命令:
\newcommand\Rect[2]{%
\draw[draw=blueii,fill=bluei] #1 rectangle node {+} #2 {};
}
我们使用此命令和四个初始坐标绘制所有蓝色矩形;例如
\Rect{(ilr)}{([xshift=-15pt]ilr|-ul)}
\Rect{(ul|-lr)}{([yshift=-7pt]iul|-lr)}
会给
在最终的绘图中,首先绘制蓝色矩形,然后绘制黑色矩形(这样黑色矩形就不会被蓝色矩形覆盖)。最后一步是使用一些节点来放置标签。
当然,初始坐标和矩形的高度/宽度是通过一些辅助命令处理的,以保持一致性。