答案1
为此使用普通矩形。请注意,您的起点应该是0
而不是1
。还请注意,我过去常常x=-1cm
反转长度。
\documentclass[tikz,border=5pt]{standalone}
\begin{document}
\begin{tikzpicture}[rounded corners, x=-1cm, below, minimum height=2em]
\pgfmathtruncatemacro{\n}{5}
\draw (0,0) node{1} rectangle (\n,1) node at (\n,0) {$n$};
\draw [fill=red] (\n/4,0) node{$n/4$} rectangle (\n/2,1) node at (\n/2,0) {$n/2$};
\end{tikzpicture}
\end{document}
答案2
对 nice 的(略微)修改(并且非常简洁)阿博阿马尔回答:
\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[rounded corners, minimum height=2em, x=-1cm, below]
\pgfmathtruncatemacro{\n}{5}
\draw (0,0) rectangle (\n,1);
\draw[fill=red] (0.25*\n,0) rectangle (0.5*\n,1);
\foreach \i/\j in {1/n, 0.5/{n/2}, 0.25/{n/4}, 0/1}
{
\node at (\i*\n,0) {$\j$};
}
\end{tikzpicture}
\end{document}