Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is there a Computer Science library for TikZ?

I am looking for a library for drawing standard data structures (array, linked list, stack, queue, pointers, trees, etc) in a consistent way.

I would also be happy to hear how hard would it be to create such a library for an intermediate LaTeX user (understands some macros, can't write macros independently) in terms of work-days.

share|improve this question
    
I think that such a library does not exist but I'm not sure. Do you have a link to show us what you desire. Something like that : introcs.cs.princeton.edu/java/43stack ? –  Alain Matthes Feb 22 '12 at 8:53
    
Yes, something like that. I have created some illustrations in TikZ without the use of such a package, and I would like to either write my own package or use an existing one to achieve better consistency and make life easier. The document in question is at ilijapavlic.com/spa/Skripta.pdf –  ipavlic Feb 22 '12 at 9:01
    
CTAN lists several packages that may be useful for computer scientists here, but unfortunately none provides constructions for standard data structure types. –  Daniel C. Mar 26 '12 at 16:54
    
@ipavlic: See also the question [Inconsistent TikZ multipart node anchors][1]. [1]: tex.stackexchange.com/questions/44257/… –  StrawberryFieldsForever Jul 25 '12 at 17:50
add comment

1 Answer 1

I agree with Altermundus that there isn't such a library. FYI, I'm inlcuding some code for an incremental linked list presentation in beamer. The code is pretty much quick-and-dirty and I'd do it differently if I had more time. Still, it may give you some ideas.

\documentclass{beamer}

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

\tikzset{box style/.style={minimum height=1.5em,minimum width=1.0cm,inner sep=1pt,outer sep=0pt,draw}}                                                                                
\tikzset{boxes style/.style={minimum height=1.5em,minimum width=1.0cm,outer sep=0pt,draw,shape=rectangle split,rectangle split horizontal,rectangle split parts=2,rectangle split ignore empty parts=false,rectangle split empty part width=0.0cm}}                                
\tikzset{left up down/.style={rounded corners,to path={%                                 
           --  ($(\tikztostart) + (+0.40,0.00)$)                                         
           -- ++(+0.00,+0.40) coordinate (tmp1)                                          
           -- ($(tmp1-|\tikztotarget)+(-0.40,+0.00)$) coordinate (tmp2)                  
           -- (tmp2 |- \tikztotarget)                                                    
           -- (\tikztotarget)%                                                           
        }}}                                                                              
\tikzset{right up down/.style={rounded corners,to path={%                                
           --  ($(\tikztostart) + (+0.40,0.00)$)                                         
           -- ++(+0.00,+0.50) coordinate (tmp1)                                          
           -- ($(tmp1-|\tikztotarget)+(-0.40,+0.00)$) coordinate (tmp2)                  
           -- (tmp2 |- \tikztotarget)                                                    
           -- (\tikztotarget)%                                                           
        }}}                                                                              
\tikzset{left down up/.style={rounded corners,to path={%                                 
           --  ($(\tikztostart) + (+0.40,0.00)$)                                         
           -- ++(+0.00,-0.40) coordinate (tmp1)                                          
           -- ($(tmp1-|\tikztotarget)+(-0.40,+0.00)$) coordinate (tmp2)                  
           -- (tmp2 |- \tikztotarget)                                                    
           -- (\tikztotarget)%                                                           
        }}}                                                                              
\tikzset{right down up/.style={rounded corners,to path={%                                
           --  ($(\tikztostart) + (+0.40,0.00)$)                                         
           -- ++(+0.00,-0.50) coordinate (tmp1)                                          
           -- ($(tmp1-|\tikztotarget)+(-0.40,+0.00)$) coordinate (tmp2)                  
           -- (tmp2 |- \tikztotarget)                                                    
           -- (\tikztotarget)%                                                           
        }}}                                                                              
\tikzset{right up/.style={rounded corners,to path={%                                     
           -- ($(\tikztostart-|\tikztotarget)+(-0.40,+0.00)$) coordinate (tmp)           
           -- (tmp |- \tikztotarget)                                                     
           -- (\tikztotarget)%                                                           
        }}}                                                                              
\tikzset{left down/.style={rounded corners,to path={%                                    
           -- ($(\tikztostart)+(+0.40,+0.00)$) coordinate (tmp)                          
           -- (tmp |- \tikztotarget)                                                     
           -- (\tikztotarget)%                                                           
        }}}                                                                              
\def\nullShape[#1]#2{\fill[#1] (#2) circle(1pt);                                         
                     \draw[#1] (#2)                                                      
                          -- ++(+0.30,+0.00) -- ++(+0.00,-0.50)                          
                             ++(-0.15,+0.20) -- ++(+0.15,+0.00) coordinate (c1)          
                                             -- ++(+0.15,+0.00)                          
                             (c1)                                                        
                             ++(-0.10,-0.10) -- ++(+0.10,+0.00) coordinate (c2)          
                                             -- ++(+0.10,+0.00);                         
                     \fill[#1] (c1) circle (0.9pt) (c2) circle (0.9pt);}                 
\def\doNullNode[#1]#2{\nullShape[#1]{#2.east}}                                           
\def\doConnectRight[#1] (#2) (#3){%                                                      
   \draw[->,#1] (#2.east) to (#3.west);                                                  
}                                                                                        
\def\doConnectLeft[#1] (#2) (#3){%                                                       
   \draw[->,#1] (#2.east) to (#3.west);                                                  
}                                                                                        
\makeatletter                                                                            
\def\nullNode{\@ifnextchar[%                                                             
                 \doNullNode%                                                            
                 {\doNullNode[]}}                                                        
\def\connectRight{\@ifnextchar[%                                                         
                 {\doConnectRight}{\doConnectRight[] }}                                  
\def\connectLeft{\@ifnextchar[%                                                          
                 {\doConnectLeft}{\doConnectLeft[] }}                                    
\makeatother                                                                             

\begin{document}
%               
%               
\begin{frame}[fragile]
\frametitle{Simulation}
\begin{block}{\texttt{Java}}
\begin{scriptsize}          
\begin{semiverbatim}        
private static NodeList qsort( NodeList list, NodeList tail ) \{
    \alert<1>{if (list != null) \{}                             
        \alert<2>{Partition p = new Partition( list.head, list.tail );}
        list.tail = qsort( p.gt, tail );                               
        return qsort( p.leq, list );                                   
    \} else \{                                                         
        return tail;                                                   
    \}                                                                 
\}                                                                     
\end{semiverbatim}                                                     
\end{scriptsize}                                                       
\end{block}                                                            
\begin{tikzpicture}[>=open triangle 60,scale=0.7]                      
\draw (0,0)        node(list)[box style] {\texttt{list}}               
      (list.south) +(+0.00,-0.20) node(tail) [anchor=north,box style] {\texttt{tail}}
      (list.east)  +(+1.00,+0.00) node(2)    [anchor=west,boxes style] {\texttt{2}}  
      (2.east)     +(+1.00,+0.00) node(3)    [anchor=west,boxes style] {\texttt{3}}  
      (3.east)     +(+1.00,+0.00) node(1)    [anchor=west,boxes style] {\texttt{1}}  
      (1.east)     +(+1.00,+0.00) node(0)    [anchor=west,boxes style] {\texttt{0}}  
      (0.east)     +(+1.00,+0.00) node(4)    [anchor=west,boxes style] {\texttt{4}}  
    ;                                                                                
\foreach \slide/\command in                                                          
         {1-2/{\nullNode{tail}},%                                                    
          1-2/{\nullNode{4}},%                                                       
          1-2/{\connectRight (list) (2)},%                                           
          1-2/{\connectRight (2) (3)},%                                              
          1-2/{\connectRight (3) (1)},%                                              
          1-2/{\connectRight (1) (0)},%                                              
          1-2/{\connectRight (0) (4)}%                                               
         } {                                                                         
    \onslide+<\slide>{                                                               
        \command                                                                     
    }                                                                                
}                                                                                    
\end{tikzpicture}                                                                    
\end{frame}                                                                          
%                                                                                    
%                                                                                    

%
%
\begin{frame}[fragile]
\frametitle{\texttt{Partition( )}: \texttt{item == 2}}
\begin{block}{\texttt{Java}}                          
\begin{scriptsize}                                    
\begin{semiverbatim}                                  
public Partition( Comparable item, NodeList list ) \{ 
    \alert<1,7,13,19,25>{while (list != null) \{}     
        \alert<2,8,14,20>{NodeList next = list;}      
        \alert<3,9,15,21>{list = list.tail;}          
        \alert<4,10,16,22>{if (item.compareTo( next.head ) >= 0) \{}
            \alert<11,17>{next.tail = leq;}                         
            \alert<12,18>{leq = next;}                              
        \} else \{                                                  
            \alert<5,23>{next.tail = gt;}                           
            \alert<6,24>{gt = next;}                                
        \}                                                          
    \}                                                              
\}                                                                  
\end{semiverbatim}                                                  
\end{scriptsize}                                                    
\end{block}                                                         
\vfill                                                              
\begin{tikzpicture}[>=open triangle 60,scale=0.7]                   
\draw (0,0)        node(list)[box style] {\texttt{list}}            
      (list.south) +(+0.00,-0.20) node(next) [anchor=north,box style] {\texttt{next}}
      (next.south) +(+0.00,-0.20) node(leq)  [anchor=north,box style] {\texttt{leq}} 
      (leq.south)  +(+0.00,-0.20) node(gt)   [anchor=north,box style] {\texttt{gt}}  
      (list.east)  +(+1.00,+0.00) node(2)    [anchor=west,boxes style] {\texttt{2}}  
      (2.east)     +(+1.00,+0.00) node(3)    [anchor=west,boxes style] {\texttt{3}}  
      (3.east)     +(+1.00,+0.00) node(1)    [anchor=west,boxes style] {\texttt{1}}  
      (1.east)     +(+1.00,+0.00) node(0)    [anchor=west,boxes style] {\texttt{0}}  
      (0.east)     +(+1.00,+0.00) node(4)    [anchor=west,boxes style] {\texttt{4}}  
    ;                                                                                
\foreach \slide/\command in                                                          
         {1/{\nullNode{next}},%                                                      
          2/{\connectRight[right up,red]  (next) (3)},%                              
          3-7/{\connectRight[right up]    (next) (3)},%                              
          8/{\connectRight[right up,red]  (next) (1)},%                              
          9-13/{\connectRight[right up]   (next) (1)},%                              
          14/{\connectRight[right up,red] (next) (0)},%                              
          15-19/{\connectRight[right up]  (next) (0)},%                              
          20/{\connectRight[right up,red] (next) (4)},%                              
          21-25/{\connectRight[right up]  (next) (4)},%                              
          1-11/{\nullNode{leq}},%                                                    
          12/{\connectRight[right up,red] (leq)  (1)},%                              
          13-17/{\connectRight[right up]  (leq)  (1)},%                              
          18/{\connectRight[right up,red] (leq)  (0)},%                              
          19-/{\connectRight[right up]    (leq)  (0)},%                              
          1-5/{\nullNode{gt}},%                                                      
          6/{\connectRight[right up,red]  (gt) (3)},%                                
          7-23/{\connectRight[right up]   (gt) (3)},%                                
          24/{\connectRight[right up,red] (gt) (4)},%                                
          25-/{\connectRight[right up]    (gt) (4)},%                                
          1-2/{\connectRight[right up down] (list) (3)},%                            
          3/{\connectRight[right up down,red]    (list) (1)},%                       
          4-8/{\connectRight[right up down]      (list) (1)},%                       
          9/{\connectRight[right up down,red]    (list) (0)},%                       
          10-14/{\connectRight[right up down]    (list) (0)},%                       
          15/{\connectRight[right up down,red]   (list) (4)},%                       
          16-20/{\connectRight[right up down]    (list) (4)},%                       
          21/{\nullNode[red]{list}},%                                                
          22-/{\nullNode{list}},%                                                    
          1-/{\connectRight                  (2) (3)},%                              
          1-16/{\connectRight                (0) (4)},%                              
          17/{\connectLeft[left up down,red] (0) (1)},%                              
          18-/{\connectLeft[left up down]    (0) (1)},%                              
          1-10/{\connectRight (1)    (0)},%                                          
          11/{\nullNode[red]{1}},%                                                   
          12-/{\nullNode{1}},%                                                       
          1-4/{\connectRight (3)    (1)},%                                           
          5/{\nullNode[red]{3}},%                                                    
          6-/{\nullNode{3}},%                                                        
          1-22/{\nullNode{4}},%                                                      
          23/{\connectLeft[left down up,red] (4) (3)},%                              
          24-/{\connectLeft[left down up]    (4) (3)},%                              
          26/%                                                                       
         } {                                                                         
    \onslide+<\slide>{                                                               
        \command                                                                     
    }                                                                                
}                                                                                    
\end{tikzpicture}                                                                    
\end{frame}                                                                          
%                                                                                    
%                                                                                    

%
%
\begin{frame}[fragile]
\frametitle{Simulation}
\begin{block}{\texttt{Java}}
\begin{scriptsize}          
\begin{semiverbatim}        
private static NodeList qsort( NodeList list, NodeList tail ) \{
    if (list != null) \{                                        
        \alert<1>{Partition p = new Partition( list.head, list.tail );}
        \alert<3>{list.tail = \alert<2>{qsort( p.gt, tail )};}         
        \alert<5>{return \alert<4>{qsort( p.leq, list )};}             
    \} else \{                                                         
        return tail;                                                   
    \}                                                                 
\}                                                                     
\end{semiverbatim}                                                     
\end{scriptsize}                                                       
\end{block}                                                            
\vfill                                                                 
\begin{tikzpicture}[>=open triangle 60,scale=0.7]                      
\draw (0,0)        node(list)[box style] {\texttt{list}}               
      (list.south) +(+0.00,-0.20) node(tail) [anchor=north,box style] {\texttt{tail}}
      (tail.south) +(+0.00,-0.20) node(leq)  [anchor=north,box style] {\texttt{p.leq}}
      (leq.south)  +(+0.00,-0.20) node(gt)   [anchor=north,box style] {\texttt{p.gt}} 
      (list.east)  +(+1.00,+0.00) node(2)    [anchor=west,boxes style] {\texttt{2}}   
      (2.east)     +(+1.00,+0.00) node(3)    [anchor=west,boxes style] {\texttt{3}}   
      (3.east)     +(+1.00,+0.00) node(1)    [anchor=west,boxes style] {\texttt{1}}
      (1.east)     +(+1.00,+0.00) node(0)    [anchor=west,boxes style] {\texttt{0}}
      (0.east)     +(+1.00,+0.00) node(4)    [anchor=west,boxes style] {\texttt{4}}
    ;
  \onslide+<5>{
  \draw (leq.east)   +(+1.00,+0.00) node(return)[anchor=west,box style]  {\texttt{return}};
  }
\foreach \slide/\command in
         {1-2/{\nullNode{tail}},%
          1-4/{\nullNode{1}},%
          3-/{\nullNode[red]{4}},%
          1-2/{\nullNode{3}},%
          1-4/{\connectRight                      (list)   (2)},
          1-2/{\connectRight                      (2)      (3)},
          3-/{\connectRight[red]                  (2)      (3)},
          3-/{\connectRight[right up down,red]    (3)      (4)},
          1-2/{\connectRight[right down up]       (4)      (3)},
      1-4/{\connectRight[left up down]        (0)      (1)},
          5-/{\connectRight[left up down,blue]    (0)      (1)},
          5-/{\connectRight[right down up,orange] (1)      (2)},
          1-4/{\connectRight[right up]            (leq)    (0)},
          1-2/{\connectRight[right up]            (gt)     (4)},
          5-/{\connectRight[right up]             (return) (0)}} {
    \onslide+<\slide>{
        \command
    }
}
\end{tikzpicture}
\end{frame}
%
%

\end{document}

one slide

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.