我希望标签左右两侧留出更多空白,7
标签上方和下方留出相同的空白4
。(节点的默认形状是正方形吗?)
\documentclass{amsart}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\begin{document}
\noindent \begin{minipage}{4.5in}
\vskip0pt
\noindent \raggedright{\textbf{1.) }Determine the area of the green region in the $5 \times 8$ rectangle.}
\end{minipage}
%
\hspace{0cm}
%
\raisebox{0mm}[0mm][0mm]
{
\begin{tikzpicture}[baseline=(current bounding box.west)]
%A 5 x 8 rectangle and two line segments between opposite sides of the rectangle
%are drawn. The region bounded by the rectangle and line segments is shaded.
%(The figure is magnified by 1/3.)
\path (0,0) coordinate (A) ({(1/3)*8},0) coordinate (B) ({(1/3)*8},{(1/3)*5}) coordinate (C) (0,{(1/3)*5}) coordinate (D);
\path[fill=Cyan] (A) -- (B) -- (C) -- (D) -- cycle;
%
\coordinate (P) at ({(1/3)*1},0);
\coordinate (P') at ({(1/3)*7},{(1/3)*5});
\coordinate (Q) at (0,{(1/3)*1});
\coordinate (Q') at ({(1/3)*8},{(1/3)*4});
\coordinate (R) at ({(1/3)*4},{(1/3)*(5/2)});
%
\path[fill=green] (A) -- (P) -- (R) -- (Q) -- cycle;
\path[fill=green] (C) -- (P') -- (R) -- (Q') -- cycle;
%
\draw (A) -- (B) -- (C) -- (D) -- cycle;
\draw (P) -- (P');
\draw (Q) -- (Q');
%The lengths of the legs and two halves of the isosceles triangle are typeset.
\draw[|<->|] ($(B)!0.15cm!90:(P)$) -- ($(P)!0.15cm!-90:(B)$);
\node[fill=white, inner sep=0, font=\scriptsize] at ($($(P)!0.5!(B)$) +(0,-0.15)$){7};
\draw[|<->|] ($(B)!0.15cm!-90:(Q')$) -- ($(Q')!0.15cm!90:(B)$);
\node[fill=white, inner sep=0, font=\scriptsize] at ($($(B)!0.5!(Q')$) +(0.15,0)$){4};
\draw[|<->|] ($(D)!0.15cm!90:(P')$) -- ($(P')!0.15cm!-90:(D)$);
\node[fill=white, inner sep=0, font=\scriptsize] at ($($(P')!0.5!(D)$) +(0,0.15)$){7};
\draw[|<->|] ($(D)!0.15cm!-90:(Q)$) -- ($(Q)!0.15cm!90:(D)$);
\node[fill=white, inner sep=0, font=\scriptsize] at ($($(D)!0.5!(Q)$) +(-0.15,0)$){4};
\end{tikzpicture}
}
\end{document}
答案1
相关键是inner xsep
和。您可以通过放置节点而不是使用复杂的计算,以及通过在样式中存储重复键来inner ysep
简化很多事情。midway
\documentclass[border=3.14mm]{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[baseline=(current bounding box.west),
minilabel/.style={fill=white,midway, inner sep=0,font=\scriptsize}]
%A 5 x 8 rectangle and two line segments between opposite sides of the rectangle
%are drawn. The region bounded by the rectangle and line segments is shaded.
%(The figure is magnified by 1/3.)
\path (0,0) coordinate (A) ({(1/3)*8},0) coordinate (B) ({(1/3)*8},{(1/3)*5}) coordinate (C) (0,{(1/3)*5}) coordinate (D);
\path[fill=Cyan] (A) -- (B) -- (C) -- (D) -- cycle;
%
\coordinate (P) at ({(1/3)*1},0);
\coordinate (P') at ({(1/3)*7},{(1/3)*5});
\coordinate (Q) at (0,{(1/3)*1});
\coordinate (Q') at ({(1/3)*8},{(1/3)*4});
\coordinate (R) at ({(1/3)*4},{(1/3)*(5/2)});
%
\path[fill=green] (A) -- (P) -- (R) -- (Q) -- cycle;
\path[fill=green] (C) -- (P') -- (R) -- (Q') -- cycle;
%
\draw (A) -- (B) -- (C) -- (D) -- cycle;
\draw (P) -- (P');
\draw (Q) -- (Q');
%The lengths of the legs and two halves of the isosceles triangle are typeset.
\draw[|<->|] ($(B)!0.15cm!90:(P)$) -- ($(P)!0.15cm!-90:(B)$)
node[minilabel, inner xsep=2pt]{7};
\draw[|<->|] ($(B)!0.15cm!-90:(Q')$) -- ($(Q')!0.15cm!90:(B)$)
node[minilabel, inner ysep=2pt]{4};
\draw[|<->|] ($(D)!0.15cm!90:(P')$) -- ($(P')!0.15cm!-90:(D)$)
node[minilabel, inner xsep=2pt]{7};
\draw[|<->|] ($(D)!0.15cm!-90:(Q)$) -- ($(Q)!0.15cm!90:(D)$)
node[minilabel, inner ysep=2pt]{4};
\end{tikzpicture}
\end{document}
答案2
另一种解决方案是使用 TikZ 库arrows.meta
(用于箭头)、positioning
(用于定位坐标)和quotes
边缘标签。代码中的规定长度被视为按比例缩小x = 0.3333 cm, y = 0.3333 cm
:
\documentclass{amsart}
\usepackage[dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, % new
% calc,
% intersections,
positioning, % new
quotes} % new
\begin{document}
\noindent \begin{minipage}{4.5in}
\vskip0pt
\noindent \raggedright{\textbf{1.) }Determine the area of the green region in the $5 \times 8$ rectangle.}
\end{minipage}
{
\begin{tikzpicture}[baseline = (current bounding box.west),
node distance = 5 and 8,
x = 0.3333 cm, y = 0.3333 cm, % scale lengths units to 1/3 ...
every edge/.style = {draw,{Bar[width=3mm].Straight Barb[length=1mm]}-%
{Straight Barb[length=1mm].Bar[width=3mm]}},
every edge quotes/.style = {fill=white,
inner sep=2pt, % determine white space around label
node font=\footnotesize},
]
%A 5 x 8 rectangle and two line segments between opposite sides of the rectangle
%are drawn. The region bounded by the rectangle and line segments is shaded.
%(The figure is magnified by 1/3.)
\coordinate (A);
\coordinate[right=of A] (B);
\coordinate[above=of B] (C);
\coordinate[above=of A] (D);
\draw[fill=Cyan] (A) rectangle (C);
%
\coordinate[right=1 of A] (P);
\coordinate[left =1 of C] (P');
\coordinate[above=1 of A] (Q);
\coordinate[below=1 of C] (Q');
%
\draw[fill=green] (P) -- (P') -| (Q') -- (Q) |- cycle;
\draw ([yshift=-3mm] P) edge ["7"] ([yshift=-3mm] B)
([xshift= 3mm] B) edge ["4"] ([xshift= 3mm] Q')
([yshift= 3mm] D) edge ["7"] ([yshift= 3mm] P')
([xshift=-3mm] Q) edge ["4"] ([xshift=-3mm] D);
\end{tikzpicture}
}
\end{document}