椭圆节点的理想形状

椭圆节点的理想形状

在问题中这里问题是手动调整椭圆节点的纵横比。我更感兴趣的是找到一种基于客观标准自动化该过程的方法。我将展示自己的答案,但我对其他想法感兴趣。

椭圆形节点比矩形节点更大(取决于inner sep),因此更难紧密地包装在一起。

这显示了默认的 TikZ 椭圆节点。

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes}

\begin{document}

\begin{tikzpicture}
\node[ellipse,draw,inner sep=0pt] at (0,0) {test};
\node[ellipse,draw,inner sep=0pt] at (0,-1) {wide test};
\node[ellipse,draw,inner sep=0pt] at (0,-2) {a very very wide test};
\end{tikzpicture}
\end{document}

默认

答案1

Latex 数学

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes}

\newlength{\tempw}
\newlength{\temph}
\newcommand{\minarea}[2][\empty]{% #1 = draw keywords (optional), #2 = node name
  \pgfextractx{\tempw}{\pgfpointdiff{\pgfpointanchor{#2}{west}}{\pgfpointanchor{#2}{east}}}%
  \pgfextracty{\temph}{\pgfpointdiff{\pgfpointanchor{#2}{south}}{\pgfpointanchor{#2}{north}}}%
  \draw[#1] (#2) ellipse[x radius={0.707\tempw},y radius={0.707\temph}];
}
\newcommand{\mintotal}[2][\empty]{% #1 = draw keywords (optionsl), #2 = node name
  \pgfextractx{\tempw}{\pgfpointdiff{\pgfpointanchor{#2}{west}}{\pgfpointanchor{#2}{east}}}%
  \pgfextracty{\temph}{\pgfpointdiff{\pgfpointanchor{#2}{south}}{\pgfpointanchor{#2}{north}}}%
  \pgfmathparse{atan(pow(\temph/\tempw,1/3))}%
  \let\tempt=\pgfmathresult
  \draw[#1] (#2) ellipse[x radius={0.5\tempw/cos(\tempt)},y radius={0.5\temph/sin(\tempt)}];
}
\newcommand{\minmax}[2][\empty]{% #1 = draw keywords (optionsl), #2 = node name
  \pgfextractx{\tempw}{\pgfpointdiff{\pgfpointanchor{#2}{west}}{\pgfpointanchor{#2}{east}}}%
  \pgfextracty{\temph}{\pgfpointdiff{\pgfpointanchor{#2}{south}}{\pgfpointanchor{#2}{north}}}%
  \pgfmathparse{\temph/\tempw}%
  \let\aspect=\pgfmathresult
  \def\tempt{45.0}%
  \foreach \i in {1,2,3} {%
    \pgfmathparse{0.5*(sin(\tempt) - \aspect*cos(\tempt)) + (\aspect-1)*sin(2*\tempt)}%
    \let\a=\pgfmathresult
    \pgfmathparse{-cos(\tempt) - \aspect*sin(\tempt) - (\aspect-1)*cos(2*\tempt)}%
    \let\b=\pgfmathresult
    \pgfmathparse{-sin(\tempt) + \aspect*cos(\tempt) - 0.5*(\aspect-1)*sin(2*\tempt)}%
    \let\c=\pgfmathresult
    \pgfmathparse{\b*\b - 4*\a*\c}%
    \let\d=\pgfmathresult
    \ifdim \d pt<0pt\relax \pgfmathparse{-\c/\b}%
    \else
      \ifdim \b pt<0pt\relax \pgfmathparse{0.5*(-\b-sqrt(\d))/\a}%
      \else \pgfmathparse{0.5*(-\b+sqrt(\d))/\a}%
      \fi
    \fi
    \let\e=\pgfmathresult
    \pgfmathparse{\tempt + deg(\e)}%
    \global\let\tempt=\pgfmathresult
  }%
  \draw[#1] (#2) ellipse[x radius={0.5\tempw/cos(\tempt)},y radius={0.5\temph/sin(\tempt)}];
}
\begin{document}

\begin{tikzpicture}
\node[fill=yellow,inner sep=0pt] (A) at (0,0) {test};
\minarea{A}\mintotal[red]{A}\minmax[green]{A}
\node[fill=yellow,inner sep=0pt] (B) at (0,-1) {wide test};
\minarea{B}\mintotal[red]{B}\minmax[green]{B}
\node[fill=yellow,inner sep=0pt] (C) at (0,-2) {a very very wide test};
\minarea{C}\mintotal[red]{C}\minmax[green]{C}
\end{tikzpicture}
\end{document}

演示

答案2

John Kormylo 的回答令人惊讶,但不是一个inner xsep=0pt(请注意X九月)够了吗?

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes}

\begin{document}

    \begin{tikzpicture}
    \node[ellipse,draw,inner xsep=0pt] at (0,0) {test};
    \node[ellipse,draw,inner xsep=0pt] at (0,-1) {wide test};
    \node[ellipse,draw,inner xsep=0pt] at (0,-2) {a very very wide test};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容