我有以下代码来绘制梯形。
我希望有箭头,表示一对对边是平行的。如何将箭头放到平行边上?
\begin{tikzpicture}[]
\node [trapezium, trapezium right angle=45,trapezium left angle=75,
minimum size=30mm,draw,thick, label=above:7cm, label=below:16cm,
label=right:9cm, label=left:8cm] at (0,0){};
\draw [black,thick,dashed] (1.7,1.5) --(5,1.5);
\draw [black,thick,dashed,->] (4.7,-1.5) --(4.7,1.5);
\node [right] at (4.7,0) {4 m};
\end{tikzpicture}
答案1
我会“手工”画出梯形。假设距离4m
应该是4cm
,梯形的右手角不能像 OP 中给出的那样,相反,图表应该更像这样:
[当然,现在 9 厘米显然是错误的。真正的距离更接近4.5cm
。]
我已经使用了希尼奇为单位和蒂克兹 decorations.markings
库定义了一种新的箭头样式->-
,将箭头放在线的中间。以下是完整代码:
\documentclass[border=5mm,tikz]{standalone}
\usepackage{tikz}
\usepackage{siunitx}
\usetikzlibrary{decorations.markings, arrows.meta, shapes.geometric,}
\begin{document}
\tikzset{%
->-/.style={decoration={markings, mark=at position 0.5 with {\arrow{stealth}}},
postaction={decorate}}
}
\begin{tikzpicture}[dotted/.style={black,dashed}, trapezium/.style={thick}]
\draw[trapezium,->-](0,0) --node[below]{\si{16}{cm}}++(16,0);
\draw[trapezium](0,0)--node[left=2mm]{\si{8}{cm}}(7,4);
\draw[trapezium][->-](7,4) --node[above]{\si{7}{cm}}++(7,0);
\draw[trapezium](16,0)--node[right]{\si{9}{cm}}(14,4);
\draw[dotted](14,4)--++(2.3,0);
\draw[dotted,->] (16,0) --node[right]{\si{4}{cm}}++(0,4);
\end{tikzpicture}
\end{document}
thick
最后,我还从虚线中删除了,因为我认为它们应该比组成梯形的线条更不突出。
答案2
您可以使用库提供的节点shapes.geometric
来绘制标记的路径。
\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{shapes.geometric,decorations.markings}
\begin{document}
\begin{tikzpicture}[]
\node [trapezium, trapezium right angle=45,trapezium left angle=75,
minimum size=30mm,draw,thick, label=above:7cm, label=below:16cm,
label=right:9cm, label=left:8cm] (trapezium) at (0,0){};
\draw [black,thick,dashed] (1.7,1.5) --(5,1.5);
\draw [black,thick,dashed,->] (4.7,-1.5) --(4.7,1.5);
\node [right] at (4.7,0) {4 m};
\path[postaction={decoration={markings,
mark=at position 0.5 with {\arrow{latex}}},decorate}]
([yshift=-\pgflinewidth]trapezium.top left corner) -- ([yshift=-\pgflinewidth]trapezium.top right corner);
\path[postaction={decoration={markings,
mark=at position 0.5 with {\arrow{latex}}},decorate}]
([yshift=\pgflinewidth]trapezium.bottom left corner) -- ([yshift=\pgflinewidth]trapezium.bottom right corner);
\end{tikzpicture}
\end{document}
这里的一个微妙之处是,必须根据线宽进行移动才能使箭头真正位于顶部。