设置 Nodepart 样式的 TikZ 样式

设置 Nodepart 样式的 TikZ 样式

下面,我使用用户定义的宏来设置类节点(\texttt{\textbf{#1}})、接口(\texttt{\textit{#1}})的节点部分的类型样式。

使用该\tikzset命令,我还为类或接口定义了样式,这些样式定义了这些节点部分应如何对齐和填充。有没有正确的方法来设置节点部分的类型样式?

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{arrows}
\usetikzlibrary{calc}  

\usepackage{aeguill}
\newcommand*{\stereotype}[1]{
    \guillemotleft {#1}\guillemotright%
}                                      

\tikzset{class or interface/.style={%
           draw,%                    
           shape=rectangle split,%   
           rectangle split parts=3,% 
           rectangle split part align={center,left,left},%
           rectangle split part fill={#1!30!white,#1!20!white,#1!10!white}},
         interface/.style={class or interface=green},                       
         concrete class/.style={class or interface=red}}                    
\newtheorem{exercise}{Exercise}                                             

\newcommand*\umlClassStyle[1]{\texttt{\textbf{#1}}}
\newcommand*\umlAbstractClassStyle[1]{\texttt{\textit{#1}}}
\newcommand*\umlInterfaceStyle[1]{\texttt{\textit{#1}}}    

\begin{document}

%
%
\begin{frame}[fragile]
   \frametitle{Class Diagram of the Observer Pattern}
   \begin{tikzpicture}[every text node part/.style={align=center}%
                      ,every two node part/.style={align=center}% 
                      ,every three node part/.style={align=left}% 
                      ,node distance=2]                              
      \path (0,0) node(Subject)[interface,anchor=east]               
                  {\nodepart{text}                                   
                     \stereotype{interface}\\                        
                     \umlInterfaceStyle{Subject}                     
                   \nodepart{two}                                    
                   \nodepart{three}\tabular{@{}l}                    
                     \texttt{attach( Observer o )}\\                 
                     \texttt{detach( Observer o )}\\                 
                     \texttt{notify( )}                              
                   \endtabular}                                      
            (Subject.south)                                          
           +(+0.0,-1.5)node(ConcreteSubject)[concrete class,anchor=north]
                  {\nodepart{text}                                       
                     \umlClassStyle{ConcreteSubject}                     
                   \nodepart{two}                                        
                     \texttt{Observer[] observers}                       
                   \nodepart{three}\tabular{@{}l}                        
                     \texttt{attach( Observer o )}\\                     
                     \texttt{detach( Observer o )}\\                     
                     \texttt{notify( )}                                  
                   \endtabular}                                          
            (Subject.north east)
           +(+1.5,+0.0)node(ConcreteObserver)[concrete class,anchor=north west]
                  {\nodepart{text}
                     \umlClassStyle{ConcreteObserver}
                   \nodepart{two}
                     \texttt{Subject subject}
                   \nodepart{three}
                     \texttt{update( Object o )} }
            (ConcreteObserver.south |- ConcreteSubject.north)
                  node(Observer)[interface,anchor=north]
                  {\nodepart{text}
                     \stereotype{interface} \\
                     \umlInterfaceStyle{Observer}
                   \nodepart{two}
                   \nodepart{three}
                     \texttt{update( Object o )} };
      \draw[->,>=open triangle 90,dashed]
           (ConcreteSubject) -- (Subject)
       node[pos=0.1,anchor=south east] {\scriptsize{implements}};
      \draw[->,>=open triangle 90,dashed]
           (ConcreteObserver) -- (Observer)
       node[pos=0.1,anchor=north east] {\scriptsize{implements}};
      \draw[<->,arrows=open diamond-angle 90]
           (ConcreteSubject.text east) -- (ConcreteSubject.text east -| Observer.west)
       node[pos=0.1,anchor=south west] {\scriptsize{has a}}
       node[very near end,anchor=south] {\scriptsize{$n$}};
      \draw[<->,arrows=open diamond-angle 90]
           (ConcreteObserver.text west) -- (ConcreteObserver.text west -| Subject.east)
       node[pos=0.1,anchor=south east] {\scriptsize{has a}}
       node[very near end,anchor=south] {\scriptsize{$1$}};
   \end{tikzpicture}
\end{frame}
%
%

\end{document}

观察者模式

答案1

您可以使用常规 TikZ 键选择节点部分的字体font=<font switch like \bfseries>。请注意,默认的 Computer Modern 字体没有粗体打字机字体。您可以使用 Courier 字体(请参阅将 \ttfamily 与 \bfseries 结合使用(或如何在等宽字体中启用粗体))。

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{arrows}
\usetikzlibrary{calc}  




\usepackage{aeguill}
\newcommand*{\stereotype}[1]{
    \guillemotleft {#1}\guillemotright%
}                                      

\tikzset{class or interface/.style={%
        draw,%                    
        shape=rectangle split,%   
        rectangle split parts=3,% 
        rectangle split part align={center,left,left},%
        rectangle split part fill={#1!30!white,#1!20!white,#1!10!white},
        every two node part/.style={align=center, font=\ttfamily},
                     every three node part/.style={align=left, font=\ttfamily}, 
                     node distance=2
    },
         interface/.style={
        class or interface=green,
        every one node part/.style={align=center, font=\ttfamily\itshape}
    },                       
         concrete class/.style={
        class or interface=red,
        every one node part/.style={align=center, font=\ttfamily\bfseries}
    }
}                    
\newtheorem{exercise}{Exercise}                                             


\begin{document}
\renewcommand{\ttdefault}{pcr} % Choose typewriter font that supports bold
%
%
\begin{frame}[fragile]
   \frametitle{Class Diagram of the Observer Pattern}
   \begin{tikzpicture}]                              
      \path (0,0) node(Subject)[interface,anchor=east]               
                  {\nodepart{one}                                   
                     \stereotype{interface}\\                        
                     Subject                     
                   \nodepart{two}                                    
                   \nodepart{three}\tabular{@{}l}                    
                     attach( Observer o )\\                 
                     detach( Observer o )\\                 
                     notify( )                              
                   \endtabular}                                      
            (Subject.south)                                          
           +(+0.0,-1.5)node(ConcreteSubject)[concrete class,anchor=north]
                  {\nodepart{one}                                       
                     ConcreteSubject
                   \nodepart{two}                                        
                    Observer[] observers
                   \nodepart{three}\tabular{@{}l}                        
                     attach( Observer o )\\                     
                     detach( Observer o )\\                     
                     notify( )                     
                   \endtabular};
   \end{tikzpicture}
\end{frame}
%
%

\end{document}

答案2

基于非常有趣杰克答案,但需要付出一些努力才能使代码更简洁(使用最近的Beamer v3.41Tikz v 3.0.1a):

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{arrows, calc, positioning, shapes}% added positioning
\usepackage{aeguill}% for defining of quotation marks
\newcommand*{\stereotype}[1]{
    \guillemotleft {#1}\guillemotright%
                            }
\renewcommand{\ttdefault}{pcr} % Choose typewriter font that supports bold face
\tikzset{
box/.style={% simplified
        draw,
        shape=rectangle split,
        rectangle split parts=3,
        align=center,
        rectangle split part fill={#1!30!white,#1!20!white,#1!10!white},
        font=\ttfamily,
        every three node part/.style={align=left}
            },
        node distance=1
        }% end of tikzset

\begin{document}
%
\begin{frame}
\frametitle{Class Diagram of the Observer Pattern}
\centering
   \begin{tikzpicture}
\node (Subject)         [box=green] {%
    \nodepart{one}\itshape  \stereotype{interface}\\
                            Subject
    \nodepart{two}
    \nodepart{three}        attach( Observer o )\\
                            detach( Observer o )\\
                            notify( )
                                    };
\node (ConcreteSubject) [box=red,below=of Subject]   {%
    \nodepart{one}\bfseries ConcreteSubject
    \nodepart{two}          Observer[] observers
    \nodepart{three}        attach( Observer o )\\
                            detach( Observer o )\\
                            notify( )
                                    };
   \end{tikzpicture}
\end{frame}
%
\end{document}

在此处输入图片描述

相关内容