这个想法是为了定义几种有用的形状,例如在控制方案中。
例如,可以定义一个通用的转换形状,而不是为每一次转换创建一个形状。
上图对应的代码是用
\documentclass[tikz]{minimal}
\def\alphabetatodq{
\begin{tikzpicture}
\draw[rounded corners=5pt] (0,0) rectangle (1,1);
\draw (0.1,0.1) -- (.9,.9);
\node at (0.25,0.8) {$\alpha\beta$};
\node at (0.75,0.2) {dq};
\end{tikzpicture}
}
\begin{document}
\begin{tikzpicture}
\node at (0,0) {\alphabetatodq};
\end{tikzpicture}
\end{document}
另一个要求是,在我使用 \def 进行测试之后,尝试连接它时,它会导致形状周围出现空白。
代码如下
\documentclass[tikz]{minimal}
\def\alphabetatodq{
\begin{tikzpicture}
\draw[rounded corners=5pt] (0,0) rectangle (1,1);
\draw (0.1,0.1) -- (.9,.9);
\node at (0.25,0.8) {$\alpha\beta$};
\node at (0.75,0.2) {dq};
\end{tikzpicture}
}
\begin{document}
\begin{tikzpicture}
\node (a) at (0,0) {\alphabetatodq};
\draw (-2,0) -- (a) -- (2,0);
\end{tikzpicture}
\end{document}
如果这个概念可以扩展到没有内部参数内容的形状,那么这一切都会很好。
像这样的小技巧可以解决白色周围的间距问题
\documentclass[tikz]{minimal}
\def\alphabetatodq{
\begin{tikzpicture}
\draw[rounded corners=5pt] (0,0) rectangle (1,1);
\draw (0.1,0.1) -- (.9,.9);
\node at (0.25,0.8) {$\alpha\beta$};
\node at (0.75,0.2) {dq};
\end{tikzpicture}
}
\begin{document}
\begin{tikzpicture}
\node[draw,rounded corners=5pt] (a) at (0,0) {\dctoac};
\draw (-2,0) -- (a) -- (2,0);
\end{tikzpicture}
\end{document}
答案1
以下是通过append after command
和解决的解决方案pgfinterruptpath
:
\documentclass[tikz]{standalone}
\tikzset {
convert/.style 2 args={
minimum size=1cm,
rounded corners=5pt,
append after command={
\pgfextra{
\pgfinterruptpath
\path (\tikzlastnode.north west)
node[inner sep=2pt,anchor=north west]{#1};
\path (\tikzlastnode.south east)
node[inner sep=2pt,anchor=south east]{#2};
\draw ([shift={(1mm, 1mm)}]\tikzlastnode.south west)
-- ([shift={(-1mm, -1mm)}]\tikzlastnode.north east);
\endpgfinterruptpath
}
},
}
}
\begin{document}
\begin{tikzpicture}
\node[draw,convert={DC}{AC}] (a) at (0,2) {};
\node[draw,convert={$\alpha\beta$}{dq}] (b) at (2,2) {};
\draw (-2,2) -- (a) -- (b) -- (4,2);
\end{tikzpicture}
\end{document}
答案2
这里有一个形状叫做split rectangle with rounded corners
。它实际上支持使用键为每个角设置不同的半径
/pgf/rectangle with rounded corners north west radius
,/pgf/rectangle with rounded corners north east radius
,/pgf/rectangle with rounded corners south west radius
, 和/pgf/rectangle with rounded corners south east radius
。
/pgf/rectangle with rounded corners radius
将所有半径设置为相同长度的关键。
这可能不是定义该形状的最有效方法,但它考虑到了outer sep
arators。
然而,它确实不是支持不同的外部分隔符。它还不检查角的半径是否太大。它没有考虑对角线的线宽,希望在大多数情况下这不是问题。
对角线相对于节点始终呈 45° 角。节点部分和线将被包围在一个正方形中。这个正方形将始终伸展(如果minimum width
/minimum height
大于可能的最小正方形)。
需要PGF 库intersection
来查找圆角部分的形状边框。
由于代码相当庞大,我没有在这个答案中发布它,但它可以在tikzlibraryshapes.qrr.roundedrectangle.code.tex
.
使用\usetikzlibrary{shapes.qrr.roundedrectangle}
以加载。
代码
\documentclass[tikz,convert=false]{standalone}
\usetikzlibrary{shapes.qrr.roundedrectangle}
\tikzset{
shape example/.style={
color=black!30,
draw,
fill=yellow!30,
line width=.5cm,
inner xsep=2.5cm,
inner ysep=0.5cm}
}
\begin{document}
\begin{tikzpicture}
\node[
draw,
split rectangle with rounded corners,
rectangle with rounded corners radius=5pt,
line cap=round,
minimum size=1cm
] (n) {$\alpha\beta$ \nodepart{lower} dq};
\foreach \Angle in {0,2.5,...,359} \draw (\Angle:1) -- (n);
\end{tikzpicture}
\begin{tikzpicture}\Huge
\node[draw,split rectangle with rounded corners,shape example,rectangle with rounded corners radius=50pt] (n) {\TeX t \nodepart{lower} more \TeX t};
\foreach \Pos/\Anchor/\Text in {left/below north west,above right/right north west,
above left/left north east,right/below north east,
right/above south east,below left/left south east,
below right/right south west,left/above south west}
\draw[thin] plot[mark=x,only marks] coordinates {
(n.\Anchor)} node[\Pos,text depth=+0pt,text height=+3pt] {\tiny\ttfamily\Text};
\foreach \Pos/\Anchor/\Text in {above/center,above/north,left/west,right/east,below/south,
above left/north west,below left/south west,
below right/south east,above right/north east}
\draw[thin] plot[mark=x,only marks] coordinates {
(n.\Anchor)} node[\Pos,text depth=+0pt,text height=+3pt] {\tiny\ttfamily\Text};
\foreach \Pos/\Anchor/\Text in {above left/mid west,above right/mid east,above/mid,
left/base west,right/base east,
below/base,below/text,below/lower}
\draw[thin] plot[mark=x,only marks] coordinates {
(n.\Anchor)} node[\Pos,text depth=+0pt,text height=+3pt] {\tiny\ttfamily\Text};
\end{tikzpicture}
\end{document}