背景:当我绘制 时tikzpicture
,我只使用默认单位(即,我没有指定任何单位,如\draw (0,0) -- (1,1);
)。
问题:当选项中没有给出单位时[rounded corners = <value>]
,TikZ
似乎假设给定的长度以 表示pt
,即与默认单位不同。下面的例子突出显示了这种现象。(这可能是由于[rounded corners]
= [rounded corners = 4pt]
?)
\documentclass[margin=1cm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[very thick]
\draw [help lines, black!10] (-.5,-.5) grid (7.5,3.5);
\draw [rounded corners=1] (0,0) rectangle ++(3,3) node [midway] {\footnotesize\texttt{rounded corners=1}};
\draw [rounded corners=1cm] (4,0) rectangle ++(3,3) node [midway] {\footnotesize\texttt{rounded corners=1cm}};
\end{tikzpicture}
\end{document}
问题:我想将圆角半径指定为默认单位的倍数(不假设1 default unit = 1cm
)。我该如何实现?
答案1
好吧,你总是可以将默认单位保存在宏中并写入rounded corners=<number>\macroname
。或者你可以使用包装器样式定义来自动执行此操作。例如:
\documentclass[border=10pt,multi,tikz]{standalone}
\begin{document}
\tikzset{%
ebo unit/.store in=\ebounit,
ebo corners/.style={rounded corners=#1\ebounit},
}
\begin{tikzpicture}[very thick, ebo unit=cm]
\draw [help lines, black!10] (-.5,-.5) grid (7.5,3.5);
\draw [ebo corners=1] (0,0) rectangle ++(3,3) node [midway] {\footnotesize\texttt{rounded corners=1}};
\draw [rounded corners=1cm] (4,0) rectangle ++(3,3) node [midway] {\footnotesize\texttt{rounded corners=1cm}};
\begin{scope}[ebo unit=mm,blue]
\draw [ebo corners=5] (0,0) ++(.25,.25) rectangle ++(3,3) node [midway] {\footnotesize\texttt{rounded corners=5}};
\draw [rounded corners=5mm] (4,0) ++(.25,.25) rectangle ++(3,3) node [midway] {\footnotesize\texttt{rounded corners=5mm}};
\end{scope}
\begin{scope}[ebo unit=ex,green]
\draw [ebo corners=8] (0,0) ++(-.25,-.25) rectangle ++(3,3) node [midway] {\footnotesize\texttt{rounded corners=8}};
\draw [rounded corners=8ex] (4,0) ++(-.25,-.25) rectangle ++(3,3) node [midway] {\footnotesize\texttt{rounded corners=8ex}};
\end{scope}
\end{tikzpicture}
\end{document}