在 tikz 中围绕节点绘制框

在 tikz 中围绕节点绘制框

我想创建这样的东西:

来自以下不带框的代码:

\documentclass{beamer}

%layout
\mode<presentation>{
\usetheme{Madrid}
}
\usepackage{tikz}
\begin{document}
\begin{frame}
\begin{center}
\begin{tikzpicture}[node distance={20mm}, thick, main/.style = {draw, circle}] 
\node[main] (1) {$C$}; 
\node[main] (2) [right of=1] {A}; 
\node[main] (3) [below right of=2] {B}; 
\node[main] (4) [above right of=2] {$\neg$B}; 
\node[main] (5) [below right of=4] {$\neg$A}; 
\node[main] (6) [right of=5] {$\neg$C};
\draw[->] (1) -- (2);
\draw[->] (2) to [bend right=30] (3);
\draw[->] (2) -- (4);
\draw[->] (3) to [bend right=30](2);
\draw[->] (3) -- (5);
\draw[->] (4) to [bend right=30] (5);
\draw[->] (5) to [bend right=30] (4);
\draw[->] (5) -- (6);
\end{tikzpicture} 
\end{center}
\end{frame}
\end{document}

答案1

使用fit库,也不要忘记使用rotate fit命令来允许非水平拟合。我还稍微更改了主要节点描述,让所有节点都具有相同的大小。

使用 fit 在节点周围绘制框

\documentclass{beamer}

%layout
\mode<presentation>{
\usetheme{Madrid}
}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}
    \begin{frame}
        \begin{center}
            \begin{tikzpicture}[
                node distance={20mm},
                thick,
                main/.style = {draw, circle, inner sep=2pt, minimum size=8mm},
                box/.style = {draw,red,inner sep=10pt,rounded corners=5pt}] 
                
                \node[main] (1) {$C$}; 
                \node[main] (2) [right of=1] {A}; 
                \node[main] (3) [below right of=2] {B}; 
                \node[main] (4) [above right of=2] {$\neg$B}; 
                \node[main] (5) [below right of=4] {$\neg$A}; 
                \node[main] (6) [right of=5] {$\neg$C};
                \draw[->] (1) -- (2);
                \draw[->] (2) to [bend right=30] (3);
                \draw[->] (2) -- (4);
                \draw[->] (3) to [bend right=30](2);
                \draw[->] (3) -- (5);
                \draw[->] (4) to [bend right=30] (5);
                \draw[->] (5) to [bend right=30] (4);
                \draw[->] (5) -- (6);
            
                \node[box,fit=(1)] {};
                \node[box,rotate fit=45,fit=(2)(3)] {};
                \node[box,rotate fit=45,fit=(4)(5)] {};
                \node[box,fit=(6)] {};
            \end{tikzpicture} 
        \end{center}
    \end{frame}
\end{document}

编辑

快速编辑一下,让您知道,由于这里包含A和的两个节点之间的角度B为 45 度,因此无需计算任何内容。但如果将来您想在两个不那么简单的节点之间执行相同操作,则必须先计算角度,如下所示:

\pgfextractangle{\angle}{2}{3}
\node[box,rotate fit=\angle,fit=(2)(3)] {};

定义\pgfextractangle如下:

\newcommand{\pgfextractangle}[3]{%
    \pgfmathanglebetweenpoints{\pgfpointanchor{#2}{center}}
                              {\pgfpointanchor{#3}{center}}
    \global\let#1\pgfmathresult  
}

相关内容