向 Tikz 图表添加椭圆

向 Tikz 图表添加椭圆

我有两个 tikz 图,一个更复杂,有大量节点,节点周围有椭圆;另一个更简单,是我通过注释掉并更改原始代码制作的。

到目前为止,我无法让节点周围的椭圆形代码正常工作。以下是不太复杂的图表的代码,包括已删除节点的注释掉的代码,以及我想要重新引入的椭圆形。

有人能帮我在这个图上添加椭圆吗?特别是每个椭圆都有相同的大小和形状。

\documentclass[a4paper,11pt,twoside]{report}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,calc,arrows.meta,shapes,fit}
\usetikzlibrary{arrows,automata}
    \begin{document}
    \begin{tikzpicture}[loose/.style={inner sep=.7em}]
    \newcommand{\p}{\mathbf{p}} 
    \newcommand{\x}{\mathbf{x}} 
    \node(p){$\p$};
    \node[below=1 of p](pb){$\bar \p$};
    %\node[below=2.5 of pb](pt){$\tilde \p$};
    \node[right=4 of $(p)!0.5!(pb)$](x1){$\x(\cdot;\p,I)$};
    %\node[right =4 of $(pb)!0.5!(pt)$] (x2){$\x(\cdot;\tilde\p,I)$};
    %\node[below=2.5 of x1] (x2){$\x(\cdot;\tilde\p,I)$}; 
    \node[right =4 of x1](y){$y(\cdot;\p,I)$};
    \draw[-Latex] (p) -- node [above,loose]{$X_\Pi(\p,I)$} (x1);
    \draw[-Latex] (pb) -- node [below,loose]{$X_\Pi(\bar\p,I)$} (x1); 
    %\draw[-Latex] (pt) -- node [below,loose]{$X_\Pi(\tilde\p,I)$} (x2.west); 
    \draw[-Latex] (x1) -- node [above,loose]{$Y_\Sigma(\x(\cdot;\p,I))$} (y); 
    %\draw[-Latex] (x2) -- node [below,inner sep=1.2em]{$Y_\Sigma(\x(\cdot;\tilde\p,I))$} (y); 
    %elipses
    %\node[ellipse,draw, fit={(p)($(pb)+(1,0)$)($(pb)-(1,0)$)},inner sep=0mm,label=$\Omega$] (eP) {}; 
    %\node[ellipse,draw, fit={($(p.north)+(4,0)$)($(pt.south)+(4,0)$)(x1)($(x1)+(1,0)$)($(x1)-(1,0)$)},inner
    %sep=0mm,label=$\mathscr{F}_\Pi$] {};
    %\node[ellipse,draw,%dashed,dotted,thick
    % fit={($(p.north)!(y)!($(p.north)+(1,0)$)$)($(pt.south)!(y)!($(pt.south)+(1,0)$)$)($(y)+(1,0)$)($(y)-(1,0)$)},inner
    %sep=0mm,label=$\mathscr{G}_\Sigma$]{};
    \end{tikzpicture}
    \end{document}

到目前为止,我无法让它工作 - 我无法将椭圆添加到更简单的图表中。有人知道怎么做吗?

在此处输入图片描述

此图显示了更复杂的图。我不想要这种分支和节点结构,但这就是我希望椭圆看起来的样子。

在此处输入图片描述

答案1

x1但如果我添加必要的前言并取消注释注释掉的部分,我几乎可以得到想要的图片。只需在和中间引入一个辅助坐标即可x2

\documentclass[border=3.14mm,tikz]{standalone}
\usepackage{mathrsfs}
\usetikzlibrary{positioning,calc,arrows.meta,shapes.geometric,fit}
\begin{document}
\begin{tikzpicture}[loose/.style={inner sep=.7em}]
\newcommand{\p}{\mathbf{p}} 
\newcommand{\x}{\mathbf{x}} 
\node(p){$\p$};
\node[below=1 of p](pb){$\bar \p$};
\node[below=2.5 of pb](pt){$\tilde \p$};
\node[right=4 of $(p)!0.5!(pb)$](x1){$\x(\cdot;\p,I)$};
\node[right =4 of $(pb)!0.5!(pt)$] (x3){$\x(\cdot;\tilde\p,I)$};
\node[below=2.5 of x1] (x2){$\x(\cdot;\tilde\p,I)$};
\coordinate (x) at ($(x1.east)!0.5!(x2.east)$); 
\node[right =4 of x](y){$y(\cdot;\p,I)$};
\draw[-Latex] (p) -- node [above,loose]{$X_\Pi(\p,I)$} (x1);
\draw[-Latex] (pb) -- node [below,loose]{$X_\Pi(\bar\p,I)$} (x1); 
\draw[-Latex] (pt) -- node [below,loose]{$X_\Pi(\tilde\p,I)$} (x2.west); 
\draw[-Latex] (x1) -- node [above,loose]{$Y_\Sigma(\x(\cdot;\p,I))$} (y); 
\draw[-Latex] (x2) -- node [below,inner sep=1.2em]{$Y_\Sigma(\x(\cdot;\tilde\p,I))$} (y); 
%elipses
\node[ellipse,draw, fit={(p)($(pb)+(1,0)$)($(pb)-(1,0)$)},inner sep=0mm,label=$\Omega$] (eP) {}; 
\node[ellipse,draw, fit={($(p.north)+(4,0)$)($(pt.south)+(4,0)$)(x1)($(x1)+(1,0)$)($(x1)-(1,0)$)},inner
sep=0mm,label=$\mathscr{F}_\Pi$] {};
\node[ellipse,draw,%dashed,dotted,thick
 fit={($(p.north)!(y)!($(p.north)+(1,0)$)$)($(pt.south)!(y)!($(pt.south)+(1,0)$)$)($(y)+(1,0)$)($(y)-(1,0)$)},inner
sep=0mm,label=$\mathscr{G}_\Sigma$]{};
\end{tikzpicture}
\end{document}

在此处输入图片描述

不含注释掉的内容:

\documentclass[border=3.14mm,tikz]{standalone}
\usepackage{mathrsfs}
\usetikzlibrary{positioning,calc,arrows.meta,shapes.geometric,fit}
\begin{document}
\begin{tikzpicture}[loose/.style={inner sep=.7em},
oval/.style={ellipse,draw}]
\newcommand{\p}{\mathbf{p}} 
\newcommand{\x}{\mathbf{x}} 
\node(p){$\p$};
\node[below=1 of p](pb){$\bar \p$};
\node[oval,fit=(p) (pb)]{};
\node[right=4 of $(p)!0.5!(pb)$](x1){$\x(\cdot;\p,I)$};
\node[oval,fit=(x1)]{};
\node[right =4 of x1](y){$y(\cdot;\p,I)$};
\draw[-Latex] (p) -- node [above,loose]{$X_\Pi(\p,I)$} (x1);
\draw[-Latex] (pb) -- node [below,loose]{$X_\Pi(\bar\p,I)$} (x1); 
\draw[-Latex] (x1) -- node [above,loose]{$Y_\Sigma(\x(\cdot;\p,I))$} (y); 
\node[oval,fit=(y)]{};
\end{tikzpicture}
\end{document}

在此处输入图片描述

至于您的评论:您可以通过减小inner sep(如果需要,甚至可以减小到负值)来轻松调整椭圆的“紧密度”。除了第一个椭圆之外,您根本不需要使用 fit。

\documentclass[border=3.14mm,tikz]{standalone}
\usepackage{mathrsfs}
\usetikzlibrary{positioning,calc,arrows.meta,shapes.geometric,fit}
\begin{document}
\begin{tikzpicture}[loose/.style={inner sep=.7em},
oval/.style={ellipse,draw}]
\newcommand{\p}{\mathbf{p}} 
\newcommand{\x}{\mathbf{x}} 
\node(p){$\p$};
\node[below=1 of p](pb){$\bar \p$};
\node[oval,inner sep=-2pt,fit=(p) (pb)]{};
\node[right=4 of $(p)!0.5!(pb)$,oval](x1){$\x(\cdot;\p,I)$};
\node[right =4 of x1,oval](y){$y(\cdot;\p,I)$};
\draw[-Latex] (p) -- node [above,loose]{$X_\Pi(\p,I)$} (x1);
\draw[-Latex] (pb) -- node [below,loose]{$X_\Pi(\bar\p,I)$} (x1); 
\draw[-Latex] (x1) -- node [above,loose]{$Y_\Sigma(\x(\cdot;\p,I))$} (y); 
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

  • 我尝试重现你的第二张图片(因为我理解你的问题,你正在寻找它)。
  • 对于节点放置我使用matrix。这样椭圆的大小就可以轻松达到相同的高度
  • 对于“椭圆”,我使用ellipse形状

    \documentclass[tikz, margin=3mm]{standalone}
    \usetikzlibrary{arrows.meta, calc, fit, matrix, positioning, shapes.geometric}
    
    \begin{document}
        \begin{tikzpicture}[
     arr/.style = {-Stealth, shorten >=2pt},
      EL/.style = {%Edge Label
                   inner sep=2pt, font=\small, sloped},
    oval/.style = {ellipse, draw, inner xsep=#1}
                        ]
    \newcommand{\p}{\mathbf{p}}
    \newcommand{\x}{\mathbf{x}}
    \matrix (m) [matrix of math nodes,
                 nodes in empty cells,
                 nodes = {minimum height=3ex, inner sep=1pt, anchor=center},
                 column sep =6em,
                 row sep=1ex]
    {
    \p  &                           &                           \\
        & \x(\cdot;\p,I)            &                           \\
    \p  &                           &                           \\
        &                           & y(\cdot;\p,I)             \\
        & \x(\cdot;\tilde{\p},I)    &                           \\
    \p  &                           &                           \\
    };
    %
    \node[oval= 4pt, fit=(m-1-1) (m-6-1), label=$\Omega$] {};
    \node[oval=-2pt, fit=(m-1-2) (m-2-2) (m-6-2), label=$\mathcal{F}$] {};
    \node[oval=-2pt, fit=(m-1-3) (m-4-3) (m-6-3), label=$\mathcal{G}_\Sigma$] {};
    %
    \draw[arr] (m-1-1) -- node[EL, above] {$X_\Pi(\p,I)$}         (m-2-2.west);
    \draw[arr] (m-3-1) -- node[EL, below] {$X_\Pi(\tilde{\p},I)$} (m-2-2.west);
    \draw[arr] (m-6-1) -- node[EL, below] {$X_\Pi(\tilde{\p},I)$} (m-5-2.west);
    %
    \draw[arr] (m-2-2) -- node[EL, above] {$Y_\Sigma(\p,I)$}         (m-4-3.west);
    \draw[arr] (m-5-2) -- node[EL, below] {$Y_\Sigma(\tilde{\p},I)$} (m-4-3.west);
        \end{tikzpicture}
    \end{document}
    

在此处输入图片描述

答案3

我的室友帮助了我,这是他的代码。在此处输入图片描述

\begin{tikzpicture}[loose/.style={inner sep=.7em}, oval/.style={ellipse,draw}]
\newcommand{\p}{\mathbf{p}}
\newcommand{\x}{\mathbf{x}}
\node(p){$\p$};
\node[below=1 of p](pb){$\bar \p$};
\node[right=4 of $(p)!0.5!(pb)$](x1){$\x(\cdot;\p,I)$};
\node[right =4 of x1](y){$y(\cdot;\p,I)$}; \def\ovalwidth{0.8}%Just used for the left one, adjust to make roughly match the others which are already wide 
\def\ovalheight{0.4}%excess height, all will get the same height.
\coordinate(ovaltop) at ($(p.north)+(0,\ovalheight)$);
\coordinate(ovalbot) at ($(pb.south)-(0,\ovalheight)$); \node[oval,inner sep=2pt,fit={(ovaltop)($(pb)+(\ovalwidth,0)$)($(pb)-(\ovalwidth,0)$)(ovalbot)},label=$\Omega$]{};
\node[oval,inner
sep=2pt,fit={(x1)(x1|-ovaltop)(x1|-ovalbot)},label=$\mathscr{F}_\Pi(I)\subset\mathscr{F}_\Pi$]{};
\node[oval,inner
sep=2pt,fit={(y)(y|-ovaltop)(y|-ovalbot)},label=$\mathscr{G}_\Pi(I)\subset\mathscr{G}_\Sigma$]{};
\draw[-Latex] (p) -- node [above,loose]{$X_\Pi(\p,I)$} (x1); \draw[-Latex] (pb) -- node [below,loose]{$X_\Pi(\bar\p,I)$} (x1); \draw[-Latex] (x1) -- node [above,loose]{$Y_\Sigma(\x(\cdot;\p,I))$} (y); \end{tikzpicture}

相关内容