rounded rectangle
下面的代码使用tikz 库生成一个右侧边缘略微弯曲的节点shapes.misc
。它看起来是这样的:
节点的高度是正确的,但它比我想要的要宽得多。我希望它的宽度刚好足以容纳标签,更像这样:
理想情况下,我希望能够独立于宽度改变节点的高度,这样我就可以使以下两个节点彼此具有相同的宽度(至少大致相同),同时保持不同的高度。
似乎rounded rectangle
s 具有由 的值决定的最小纵横比rounded rectangle arc length
,因此曲线越细小,框就越宽。有没有办法覆盖此行为?
这是 MWE 代码,它产生上图最后一张图中的两个节点:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.misc}
\begin{document}
\begin{tikzpicture}
\node [
draw,
rounded rectangle,
rounded rectangle left arc=none,
minimum height = 3em,
rounded rectangle arc length = 90,
] at (0,1)
{$f$};
\node [
draw,
rounded rectangle,
rounded rectangle left arc=none,
minimum height = 1.5em,
rounded rectangle arc length = 90,
] at (0,0)
{$g$};
\end{tikzpicture}
\end{document}
答案1
这是一种解决方法,但我认为它可能很有用。将自己的形状绘制为 怎么样pic
?它不是节点,但很简单,我们可以根据需要使其可自定义。
在以下示例中,我pic
使用两个参数创建了此示例:它必须包含的文本和圆弧的角度。如果我们固定角度(或者如果我们排除 0 和 180,那么我们就不需要句子了\ifnum
),那么可能会容易得多。
\documentclass[border=2mm]{standalone}
\usepackage {tikz}
\usetikzlibrary{calc}
\tikzset
{%
pics/rounded rectangle/.style 2 args={% text, angle (0 <= angle <= 180)
code={%
\node (O) at (0,0) {#1};
\ifnum #2 = 0
\path[pic actions] (O.north east) rectangle (O.south west);
\else\ifnum #2 = 180
\path[pic actions] let \p1=(O.north east),
\p2=(O.south west),
\n1={0.5*(\y1-\y2)} % radius
in (O.north west) -- ($(O.north east)-(\n1,0)$) arc (0.5*#2:-0.5*#2:\n1) -| cycle;
\else
\path[pic actions] let \p1=(O.north east),
\p2=(O.south west),
\n1={0.5*(\y1-\y2)/sin(0.5*#2)}, % radius
\n2={0.5*(\y1-\y2)/tan(0.5*#2)} % delta x in the arc
in (O.north west) -- ($(O.north east)+(\n2-\n1,0)$) arc (0.5*#2:-0.5*#2:\n1) -| cycle;
\fi\fi
}},
}
\begin{document}
\begin{tikzpicture}
\pic [
draw,
minimum height = 3em,
] at (0,1) {rounded rectangle={$f$}{90}};
\pic [
draw,
minimum height = 1.5em,
] at (0,0) {rounded rectangle={$g$}{90}};
\pic [
draw,
minimum height = 2em,
minimum width = 5em
] at (0,-1) {rounded rectangle={$f$}{120}};
\pic [
draw
] at (0,-2) {rounded rectangle={$ffffffffff$}{50}};
\end{tikzpicture}
\end{document}
通过上述代码我们得到: