如何在 tikzpicture 周围绘制虚线矩形?

如何在 tikzpicture 周围绘制虚线矩形?

我想在整个 tikzpicture 周围绘制一个虚线矩形,但我不知道该怎么做。我认为完美的解决方案是使用fit库,但我不知道怎么做。

这是我的代码:

 \begin{figure}[!htbp]
 \tikzstyle{every state}=[inner color= white,outer color= white,draw= black,text=black, drop shadow]
        \hspace*{0.5\linewidth}
        \begin{tikzpicture}[ 
            scale=.8,
            ornode/.style={draw=sthlmRed, regular polygon, regular polygon sides=3, fill=sthlmRed!20}, 
            andnode/.style={draw=sthlmGreen, circle, fill=sthlmGreen!20}]
 
        \draw[draw=black] (0,5.5) rectangle ++(3,5);
            \node[state, ornode] (q_0) at (-3,0) {$s^n_\tau$};
             
                \node[state, andnode, scale=.85] (q_1) at (-6, -1.5) {$u^n_\tau$};                          
                    \node[state, ornode, text=white, scale=.6] (q_11) at (-7.5, -3) {$s_\tau$};
                    \node[scale=.6] at (-7.5, -3) {$s^{n-1}_\tau$};
                        \node (q_111) at (-8.25,-4.5) {};
                        \node (q_112) at (-6.75,-4.5) {};
                        \node (q_113) at (-7.2,-4) {};
                        \node (q_114) at (-7.8,-4) {};
                    \node[state, ornode, text=white, scale=.6] (q_12) at (-4.5, -3) {$s_\tau$}; 
                    \node[scale=.6] at (-4.5, -3) {$s^{n-1}_\tau$}; 
                        \node (q_121) at (-5.25,-4.5) {};
                        \node (q_122) at (-3.75,-4.5) {};
                        \node (q_123) at (-4.2,-4) {};
                        \node (q_124) at (-4.8,-4) {};
                    \node (q_13) at (-6.3, -2.5) {}; 
                    \node (q_14) at (-5.7, -2.5) {}; 
                    \node at (-6,-5) {$\cdots$};
                    \node at (-6,-3) {$\cdots$};
                    
                \node[state, andnode, scale=.85] (q_2) at (0, -1.5) {$u^n_\tau$}; 
                    \node[state, ornode, text=white, scale=.6] (q_21) at (1.5, -3) {$s_\tau$}; 
                    \node[scale=.6] at (1.5, -3) {$s^{n-1}_\tau$}; 
                        \node (q_211) at (2.25,-4.5) {};
                        \node (q_212) at (0.75,-4.5) {};
                        \node (q_213) at (1.2,-4) {};
                        \node (q_214) at (1.8,-4) {};
                    \node[state, ornode, text=white, scale=.6] (q_22) at (-1.5, -3) {$s_\tau$}; 
                    \node[scale=.6]at (-1.5, -3) {$s^{n-1}_\tau$}; 
                        \node (q_221) at (-0.75,-4.5) {};
                        \node (q_222) at (-2.25,-4.5) {};
                        \node (q_223) at (-1.8,-4) {};
                        \node (q_224) at (-1.2,-4) {};
                    \node (q_23) at (0.3, -2.5) {}; 
                    \node (q_24) at (-0.3, -2.5) {}; 
                    \node at (0,-5) {$\cdots$};
                    \node at (0,-3) {$\cdots$};

                \node[] (q_3) at (-3.3, -1.5) {}; 
                \node[] (q_4) at (-2.7, -1.5) {}; 
                \node at (-3,-1.5) {$\cdots$};
                \node at (-3,-3) {$\cdots$};
                \node at (-3,-5) {$\cdots$};
                
                
            \path[-, draw=black] (q_0) edge node[fill=white, scale=.7] {$u^n$}   (q_1) 
                                    edge  node[fill=white, scale=.7] {$u^n$} (q_2) 
                                    edge  (q_3) 
                                    edge  (q_4) 
                                    (q_1) edge node[fill=white, scale=.7] {$o^{n-1}_\tau$} (q_11)
                                              edge node[fill=white, scale=.7] {$o^{n-1}_\tau$} (q_12)
                                          edge (q_13)
                                          edge (q_14)
                                    (q_2) edge node[fill=white, scale=.7] {$o^{n-1}_\tau$} (q_21)
                                              edge node[fill=white, scale=.7] {$o^{n-1}_\tau$} (q_22)
                                          edge (q_23)
                                          edge (q_24)
                                          (q_11) edge (q_111)
                                                       edge (q_112)
                                                       edge (q_113)
                                                       edge (q_114)
                                          (q_12) edge (q_121)
                                                       edge (q_122)
                                                       edge (q_123)
                                                       edge (q_124)
                                          (q_21) edge (q_211)
                                                       edge (q_212)
                                                       edge (q_213)
                                                       edge (q_214)
                                          (q_22) edge (q_221)
                                                       edge (q_222)
                                                       edge (q_223)
                                                       edge (q_224); 
 
        \end{tikzpicture} 
\end{figure}    

答案1

您可以使用的锚点current bounding box在 周围绘制一个矩形tikzpicture。您应该确保在相关 的最后一个宏中调用此方法tikzpicture。如果边框太紧,您也可以调整它。

由于我无法正确编译你的代码,我用鸭子来代替......

\documentclass[border=10pt]{standalone}
\usepackage{tikzducks}

\begin{document}
\begin{tikzpicture}

\duck

% as last macro of the tikzpicture enviroment:
\draw[dashed] (current bounding box.north west) rectangle (current bounding box.south east); 

\end{tikzpicture} 
\end{document}

在此处输入图片描述


\documentclass[border=10pt]{standalone}
\usepackage{tikzducks}

\begin{document}
\begin{tikzpicture}

\duck

\draw[dashed] ([shift={(-5pt,5pt)}]current bounding box.north west) rectangle ([shift={(5pt,-5pt)}]current bounding box.south east);

\end{tikzpicture} 
\end{document}

在此处输入图片描述

答案2

你是指fit图书馆吗?是的,你可以使用它:

\node[dashed, draw, fit=(current bounding box), inner sep=5pt] {};

将此作为 之前的最后一行添加\end{tikzpicture},当然也将\usetikzlibrary{fit}其添加到序言中。inner sep根据需要调整 。

相关内容