我正在尝试绘制一个简单的矩形以及尺寸。这是我的第一次尝试:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\pgfmathsetmacro{\a}{2.0}
\pgfmathsetmacro{\b}{4.0}
\begin{tikzpicture}
\draw (0, 0) rectangle (\a, \b);
\draw[|<->|] (0, 0) -- ++ (\a, 0);
\node[below] at (0.5*\a, 0) {a};
\end{tikzpicture}
\end{document}
这当然会导致线条绘制在矩形的顶部,但我希望它稍微低一点。当然,我可以这样做
\draw[|<->|] (0, -5mm) -- ++ (\a, 0);
但这既不好看,也无法正确缩放。有什么建议吗?
编辑:下面是我所说的缩放不正确的一个例子:
\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}
\begin{document}
\pgfmathsetmacro{\a}{2.0}
\pgfmathsetmacro{\b}{4.0}
\begin{tikzpicture}[scale=1]
\draw (0, 0) rectangle (\a, \b);
\draw[|<->|] ([yshift=-1mm]0, 0) -- ++ (\a, 0);
\node[below] at (0.5*\a, 0) {a};
\end{tikzpicture}
\begin{tikzpicture}[scale=5]
\draw (0, 0) rectangle (\a, \b);
\draw[|<->|] ([yshift=-1mm]0, 0) -- ++ (\a, 0);
\node[below] at (0.5*\a, 0) {a};
\end{tikzpicture}
\end{document}
答案1
这是一个外行人的解决方法,我们定义一个节点,(0,0)
然后从它的南边画出箭头。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\pgfmathsetmacro{\a}{2.0}
\pgfmathsetmacro{\b}{4.0}
\begin{tikzpicture}[scale=1]
\draw (0, 0) node[below] (A){} rectangle (\a, \b);
\draw[|<->|] (A.south) -- ++ (\a, 0);
\node[below] at (0.5*\a, 0) {a};
\end{tikzpicture}
\begin{tikzpicture}[scale=3]
\draw (0, 0) node[below] (A){} rectangle (\a, \b);
\draw[|<->|] (A.south) -- ++ (\a, 0);
\node[below] at (0.5*\a, 0) {a};
\end{tikzpicture}
\end{document}
您可以通过改变inner sep
\draw (0, 0) node[inner sep=1mm,below] (A){} rectangle (\a, \b);