tikz 包 qtree 和 tikzpicture 之间的冲突

tikz 包 qtree 和 tikzpicture 之间的冲突

我正在写论文手稿,与 tikz 包有冲突。我制作了这个人工神经网络,并在基础上得到了这个结果(预期)。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}

\begin{figure}[h!]
    %\usetikzlibrary{tikz}
    %\pagestyle{empty}

    \def\layersep{2.5cm}
    \centering
    \begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep]
        \tikzstyle{every pin edge}=[<-,shorten <=1pt]
        \tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt]
        \tikzstyle{input neuron}=[neuron, fill=green!50];
        \tikzstyle{output neuron}=[neuron, fill=red!50];
        \tikzstyle{hidden neuron}=[neuron, fill=blue!50];
        \tikzstyle{annot} = [text width=4em, text centered]

        % Draw the input layer nodes
        \foreach \name / \y in {1,...,4}
        % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
            \node[input neuron, pin=left:Input \#\y] (I-\name) at (0,-\y-4.5) {};

        % Draw the first hidden layer nodes
        \foreach \name / \y in {1,...,6}
            \path[yshift=0.5cm]
                node[hidden neuron] (H1-\name) at (\layersep,-\y -4) {};


        % Draw the second hidden layer nodes
        \foreach \name / \y in {1,...,6}
        \path[yshift=0.5cm]
            node[hidden neuron] (H2-\name) at (3*\layersep,-\y -4) {};

        % Draw the output layer node
        %\node[output neuron,pin={[pin edge={->}]right:Output}, right of=H-3] (O) {};

        % Draw the input layer nodes
        \foreach \name / \y in {1,...,3}
        % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
            \node[output neuron, pin=right:Output \#\y] (O-\name) at (4*\layersep,-\y -5.) {};

        % Connect every node in the input layer with every node in the
        % hidden layer.
        \foreach \source in {1,...,4}
            \foreach \dest in {1,...,6}
                \path (I-\source) edge (H1-\dest);

        % Connect every node in the first HL with every node in the
        % second hidden layer.
        \foreach \source in {1,...,6}
        \foreach \dest in {1,...,6}
            \path (H1-\source) edge (H2-\dest);


        % Connect every node in the hidden layer with the output layer
        \foreach \source in {1,...,6}
            %\path (H-\source) edge (O);
            \foreach \dest in {1,...,3}
                \path (H2-\source) edge (O-\dest);

        % Annotate the layers
        \node[annot,above of=H1-1, node distance=1cm] (hl1) {Hidden \\ layer};
        \node[annot,above of=H2-1, node distance=1cm] (hl2) {Hidden \\ layer};
        \node[annot,left of=hl1] {Input\\layer};
        \node[annot,right of=hl2] {Output\\layer};
    \end{tikzpicture}
    \caption{Example of a multilayers perceptron}
\end{figure}

\end{document}

预期结果:

在此处输入图片描述

但最近我为手稿的另一部分实现了一些新的树状图形,得到了这样的效果:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz-qtree}

\begin{document}

\tikzset{font=\small,
    edge from parent fork down,
    level distance=1.3cm,
    every node/.style=
    {
        top color=white,
        bottom color=white,
        rectangle,rounded corners,
        minimum height=8mm,
        draw=black,
        very thick,
        drop shadow,
        align=center,
        text depth = 0pt
    },
    edge from parent/.style=
        {
            draw=black,
            thick
        }}
\begin{figure}[h!]
    \centering
    \begin{tikzpicture}
    \Tree [.{Deep learning\\techniques in time series}
            [.{Artificial Neural\\Network} ] 
            [.{Recurrent Neural\\Network}
                [.{Long Short\\Term Memory} 
                    [.{Gated Recurrent\\Unit} ]
                ]
            ]
    ]
    \end{tikzpicture}
    \caption{Global deep learning techniques for anomaly detection in times series}
    \label{fig:Global deep learning techniques for anomaly detection in times series}
\end{figure}

\end{document}

在此处输入图片描述

但最后我遇到了几个无法解决的问题:

  • 节点大小不一样。
  • qtree 的线不是垂直和水平的。
  • 神经网络没有颜色。
  • 层的名称超出了节点。

在此处输入图片描述

在此处输入图片描述

好像是数据包之间有冲突,还是需要标签来指定在哪个地方使用哪个数据包?我不知道,因为我从来没有遇到过这种情况。

完整的例子如下:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{tikz-qtree}


\begin{document}

\tikzset{font=\small,
    edge from parent fork down,
    level distance=1.3cm,
    every node/.style=
    {
        top color=white,
        bottom color=white,
        rectangle,rounded corners,
        minimum height=8mm,
        draw=black,
        very thick,
        drop shadow,
        align=center,
        text depth = 0pt
    },
    edge from parent/.style=
        {
            draw=black,
            thick
        }}
\begin{figure}[h!]
    \centering
    \begin{tikzpicture}
    \Tree [.{Deep learning\\techniques in time series}
            [.{Artificial Neural\\Network} ] 
            [.{Recurrent Neural\\Network}
                [.{Long Short\\Term Memory} 
                    [.{Gated Recurrent\\Unit} ]
                ]
            ]
    ]
    \end{tikzpicture}
    \caption{Global deep learning techniques for anomaly detection in times series}
    \label{fig:Global deep learning techniques for anomaly detection in times series}
\end{figure}


\begin{figure}[h!]
    %\usetikzlibrary{tikz}
    %\pagestyle{empty}

    \def\layersep{2.5cm}
    \centering
    \begin{tikzpicture}[shorten >=1pt,->,draw=black!50, node distance=\layersep]
        \tikzstyle{every pin edge}=[<-,shorten <=1pt]
        \tikzstyle{neuron}=[circle,fill=black!25,minimum size=17pt,inner sep=0pt]
        \tikzstyle{input neuron}=[neuron, fill=green!50];
        \tikzstyle{output neuron}=[neuron, fill=red!50];
        \tikzstyle{hidden neuron}=[neuron, fill=blue!50];
        \tikzstyle{annot} = [text width=4em, text centered]

        % Draw the input layer nodes
        \foreach \name / \y in {1,...,4}
        % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
            \node[input neuron, pin=left:Input \#\y] (I-\name) at (0,-\y-4.5) {};

        % Draw the first hidden layer nodes
        \foreach \name / \y in {1,...,6}
            \path[yshift=0.5cm]
                node[hidden neuron] (H1-\name) at (\layersep,-\y -4) {};


        % Draw the second hidden layer nodes
        \foreach \name / \y in {1,...,6}
        \path[yshift=0.5cm]
            node[hidden neuron] (H2-\name) at (3*\layersep,-\y -4) {};

        % Draw the output layer node
        %\node[output neuron,pin={[pin edge={->}]right:Output}, right of=H-3] (O) {};

        % Draw the input layer nodes
        \foreach \name / \y in {1,...,3}
        % This is the same as writing \foreach \name / \y in {1/1,2/2,3/3,4/4}
            \node[output neuron, pin=right:Output \#\y] (O-\name) at (4*\layersep,-\y -5.) {};

        % Connect every node in the input layer with every node in the
        % hidden layer.
        \foreach \source in {1,...,4}
            \foreach \dest in {1,...,6}
                \path (I-\source) edge (H1-\dest);

        % Connect every node in the first HL with every node in the
        % second hidden layer.
        \foreach \source in {1,...,6}
        \foreach \dest in {1,...,6}
            \path (H1-\source) edge (H2-\dest);


        % Connect every node in the hidden layer with the output layer
        \foreach \source in {1,...,6}
            %\path (H-\source) edge (O);
            \foreach \dest in {1,...,3}
                \path (H2-\source) edge (O-\dest);

        % Annotate the layers
        \node[annot,above of=H1-1, node distance=1cm] (hl1) {Hidden \\ layer};
        \node[annot,above of=H2-1, node distance=1cm] (hl2) {Hidden \\ layer};
        \node[annot,left of=hl1] {Input\\layer};
        \node[annot,right of=hl2] {Output\\layer};
    \end{tikzpicture}
    \caption{Example of a multilayers perceptron}
\end{figure}
\end{document}

提前致谢

答案1

树:

  • 在哪里定义edge from parent fork down
  • 要使用drop shadow你需要加载 TiZ 库shadows

神经网络:

  • 对于神经元节点的放置使用chains
  • 重写了图片元素样式
\documentclass{article}
\usepackage{tikz-qtree}
\usetikzlibrary{calc, chains, 
                positioning,
                shadows}

\begin{document}
    \begin{figure}[ht]
    \centering
\begin{tikzpicture}
\tikzset{edge from parent/.style=
    {draw,
     edge from parent path={(\tikzparentnode.south)
                            -- +(0,-8pt)
                            -| (\tikzchildnode)}
     },
    level distance=17mm, sibling distance = 11mm,
    every node/.style=
    {draw, very thick, rounded corners, fill=white,
     font=\small,
     minimum height=8mm, text width=8em, %text depth=0pt,
     align=center,
     drop shadow,
    }
            }
\Tree [.{Deep learning techniques in\\ time series}
        [.{Artificial\\ Neural Network} ]
        [.{Recurrent\\ Neural Network}
            [.{Long Short Term Memory}
                [.{Gated\\ Recurrent Unit} ]
            ]
        ]
    ]
\end{tikzpicture}
    \caption{Global deep learning techniques for anomaly detection in times series}
    \label{fig:Global deep learning techniques for anomaly detection in times series}
\end{figure}



\begin{figure}[!ht]
    \begin{tikzpicture}[shorten >=1pt,->, draw=black!50,
        node distance = 6mm and 24mm,
          start chain = going below,
every pin edge/.style = {<-,shorten <=1pt},
        neuron/.style = {circle, fill=#1,
                         minimum size=17pt, inner sep=1pt, outer sep=0pt,
                         on chain},
         annot/.style = {draw, rounded corners, text width=4em, align=center}
                        ]
% Draw the input layer nodes
\foreach \i in {1,...,4}
    \node[neuron=green!50,
          pin=180:Input \#\i] (I-\i)    {};
% Draw the hidden layers nodes
    \node[neuron=blue!50,
          above right=8mm and 24mm of I-1] (H-11)     {};
% first hide layer
\foreach \i [count=\j from 1] in {2,...,6}
    \node[neuron=blue!50,
          below=of H-1\j]      (H-1\i)    {};

% second hide layer
\foreach \i [count=\j from 1] in {1,...,6}
    \node[neuron=blue!50,
          right=of H-1\j]      (H-2\i)    {};

% Draw the output layer node
    \node[neuron=red!50,
          pin= {[pin edge=->]0:Output \#1},
          right=of $(H-22)!0.5!(H-23)$]  (O-1)   {};
\foreach \i [count=\j from 1] in {2,3}
    \node[neuron=red!50,
          pin= {[pin edge=->]0:Output \#\j},
          below=of O-\j]        (O-\i)  {$x_{\i}$};
% Connect input nodes with hidden nodes and
%  hiden nodes with output nodes with the output layer
\foreach \i in {1,...,4}
    \foreach \j in {1,...,6}
{
    \path (I-\i) edge (H-1\j);
}
\foreach \i in {1,...,6}
    \foreach \j in {1,...,6}
{
    \path (H-1\i) edge (H-2\j);
}
\foreach \i in {1,...,6}
    \foreach \j in {1,...,3}
{
    \path (H-2\i) edge (O-\j);
}
% Annotate layers
\node[annot,above=of I-1 |- H-11.center]  {Hidden layer};
\node[annot,above=of H-11.center]         {Input layer};
\node[annot,above=of H-21.center]         {Input layer};
\node[annot,above=of O-1 |- H-21.center]  {Output layer};
    \end{tikzpicture}
    \caption{Example of a multilayers perceptron}
\end{figure}
\end{document}

在此处输入图片描述

附录:
为了使 Qtree 中的节点具有不同的宽度,最简单的解决方案是更改其前导码,以便手动text width替换minimum width节点中的文本:

\documentclass{article}
\usepackage{tikz-qtree}
\usetikzlibrary{calc, chains,
                positioning,
                shadows}

\begin{document}
    \begin{figure}[ht]
    \centering
\begin{tikzpicture}
\tikzset{edge from parent/.style=
    {draw,
     edge from parent path={(\tikzparentnode.south)
                            -- +(0,-8pt)
                            -| (\tikzchildnode)}
     },
    level distance=17mm, sibling distance = 8mm,      
    every tree node/.style=
    {draw, very thick, rounded corners, fill=white,
     font=\small,
     minimum height=8mm, minimum width=8em, align=center,
     drop shadow,
     anchor=north
    }
            }
\Tree [.{Deep learning techniques\\ in time series}
        [.{Artificial\\ Neural Network} ]
        [.{Recurrent\\ Neural Network}
            [.{Long Short\\ Term Memory}
                [.{Gated\\ Recurrent Unit} ]
            ]
        ]
    ]
\end{tikzpicture}
    \caption{Global deep learning techniques for anomaly detection in times series}
    \label{fig:Global deep learning techniques for anomaly detection in times series}
\end{figure}
\end{document}

在此处输入图片描述

tikz-qtree我宁愿使用包。forest它专门用于绘制树,功能非常强大。使用它,你的树的代码是:

\documentclass{article}
\usepackage[edges]{forest}
\usetikzlibrary{shadows}

\begin{document}
    \begin{figure}[ht]
    \centering
\begin{forest}
for tree={          
%% style of nodes 
 draw, rounded corners, 
       font = \small,
/tikz/align = flush center,%
   if level = {0}{text width=11em}{text width=7em},
       fill = white,
drop shadow = {shadow xshift=2mm, shadow yshift=-2mm},
%% style of tree (edges, distances, direction)
       grow = south,
    forked edge,      % for forked edge
      s sep = 5mm,    % sibling distance
      l sep = 8mm,    % level distance
   fork sep = 4mm,    % distance from parent to branching point
           }% end for tree
[Deep learning techniques in time series
    [Artificial Neural Network]
    [Recurrent Neural Network
        [Long Short Term Memory
            [Gated Recurrent Unit]
        ]
    ]
]
\end{forest}
    \caption{Global deep learning techniques for anomaly detection in times series}
    \label{fig:Global deep learning techniques for anomaly detection in times series}
\end{figure}
\end{document}

在此处输入图片描述

答案2

为了使您的代码可编译,我删除了edge from parent fork down,然后drop shadow,

我注意到你有

\tikzset{
...
every node/.style=
top color=white,
bottom color=white,
...
}}

这将为所有后续 TikZ 图片设置选项。 - 然后是这些图片中的所有节点。top colorbottom color用于制作垂直阴影,因此将它们都设置为白色甚至毫无意义。 删除这两行,编译后得到:

树结构和彩色图

\tikzset仅用于所有后续 TikZ 图片所需的选项。仅一张图片的选项在之后直接给出,[..]例如\begin{tikzpicture}

\begin{tikzpicture}[thick]

相关内容