我一直在尝试自定义圆弧,但似乎不知道如何自动设置圆弧半径。目标是创建一个节点,该节点限制两个半圆并根据需要变宽
末尾有所需图片的链接
https://i.stack.imgur.com/E4qag.jpg
\usepackage{xparse}
\NewDocumentCommand{\mynode}{%
O{}
m
m
m
O{}
}{
{
\node [#1] (#2) at #3 {#4};
\def\ra{.3cm}
\draw [#5] (#2.north west) arc
[
start angle=90,
end angle=270,
x radius=\ra,
y radius=\ra
] ;
\draw [#5] (#2.north east)to [in=0,out=180] (#2.north west);
\draw [#5] (#2.south east)to [in=0,out=180] (#2.south west);
\draw [#5] (#2.south east) arc
[
start angle=270,
end angle=450,
x radius=\ra,
y radius=\ra
] ;
}
}
[![This is what I want to make][2]][2]
[1]: https://i.stack.imgur.com/E4qag.jpg
[2]: https://i.stack.imgur.com/iuZgn.jpg
答案1
这使用\pgfextracty
等来获取节点的高度。我还将所有绘图合并到一条路径中。
\documentclass{standalone}
\usepackage{tikz}
\usepackage{xparse}
\newlength{\mydiameter}
\NewDocumentCommand{\mynode}{% #1 = node options (optional), #2 = node name, #3 = coordinates, #4 = text, #5 = draw options (optional)
O{}
m
m
m
O{}
}{%
{%
\node [#1] (#2) at #3 {#4};
\pgfextracty{\mydiameter}{\pgfpointdiff{\pgfpointanchor{#2}{south}}{\pgfpointanchor{#2}{north}}}%
\draw [#5] (#2.north west) arc
[
start angle=90,
end angle=270,
radius={0.5\mydiameter}
] -- (#2.south east) arc
[
start angle=-90,
end angle=90,
radius={0.5\mydiameter}
] -- cycle;
}%
}
\begin{document}
\begin{tikzpicture}
\mynode{A}{(0,0)}{Test}
\end{tikzpicture}
\end{document}