如何将图例放置在以下 tikz 图中合适的位置?

如何将图例放置在以下 tikz 图中合适的位置?

我有以下图表代码tikz。我不想单独标记所有边,而是想放置一个图例框,用相似的颜色表示边。在此图中,图例框将包含以下项目

1. red line for value `b` 
2. blue line for value `a`
3. black lines for value `c`. 

另外我怎样才能改变默认白色背景decision命令中的标签透明的以避免在重叠时遗漏某些边缘部分。使用其他命令或tikzpicture环境是否有帮助减少代码?

\documentclass{standalone}
\usepackage{tikz-cd}
\tikzset{SmallNode/.style={draw,inner sep=0pt},%<- add circle here for circle nodes
every edge quotes/.style={fill=white}
}
\begin{document}
\begin{tikzpicture}
\node[SmallNode] (00) at (-0.5,1.5) {$(0,0)$};
\node[SmallNode] (0) at (-0.5,0) {$0$};
\node[SmallNode] (1) at (1,0) {$(1)$};
\node[SmallNode] (2) at (2.5,0) {$(2)$};
\node[SmallNode] (3) at (4.0,0) {$(3)$};
\node[] (4) at (4.8,0) {$\textbf{\dots}$};
\node[SmallNode] (5) at (6,0) {$(y-1)$};
\node[SmallNode] (6) at (8.0,0) {$(y)$};
\node[SmallNode] (7) at (9.9,0) {$(y+1)$};
\node[SmallNode] (8) at (11.9,0) {$(y+2)$};
\node[] (9) at (12.8,0) {$\textbf{\dots}$};
\node[SmallNode] (10) at (11.8,-3) {$(x-b-1)$};
\node[SmallNode] (11) at (8.9,-3) {$(x-b)$};
\node[SmallNode] (12) at (6.2,-3) {$(x-b+1)$};
\node[] (13) at (5.0,-3) {$\textbf{\dots}$};
\node[SmallNode] (14) at (3.8,-3) {$(x-1)$};
\node[SmallNode] (15) at (1.5,-3) {$(x)$};
\node[SmallNode] (16) at (-0.5,-3) {$(x+1)$};
\draw[->,blue] (0) edge ["a",bend left] (00) (00)edge ["a"] (0) ;
\draw[->] (1) edge ["b",bend left,auto=left] (0) (0) edge ["a"] (1);
\draw[->] (2) edge [bend left] (0) (1) edge ["a"] (2);
\draw[->] (3) edge [bend left] (0)(2) edge["a"] (3);
\draw[->] (5) edge [bend left] (0) edge ["a"] (6);
\draw[->] (6) edge [bend left] (0) edge ["a",black] (7);
\draw[->,blue] (7) edge [bend right,auto=right,"b"] (1) edge ["a",black] (8)(6) edge [bend right,"b",auto =right] (1);
\draw[->,red] (8) edge [bend right,auto=right] (2)(7) edge [bend right,auto=right,"a"] (2);
\draw[->] (10) edge ["a"] (11);
\draw[->] (11) edge ["a"] (12);
\draw[-,dashed,bend left,auto=left] (9) edge (10);
\draw[->] (14) edge ["a"] (15) edge [bend left,auto=left,"b",blue] (10);
\draw[->] (15) edge ["a"] (16) edge [bend right,auto=right,"b",blue] (11)(15) edge [bend left,auto=left,"b",blue] (10);
\draw[->,blue] (16) edge ["a",bend right,auto=right] (11);
\end{tikzpicture}
\end{document}

答案1

如果我正确理解了你的问题,那么你正在寻找这个:

在此处输入图片描述

由于您没有指出连接线的位置c,并且使用的标签和颜色在您的 mwe 中不匹配,所以我可能用错误的颜色画了这些线。然而,对您来说,这应该不是什么问题。

我也稍微清理了一下你的代码并使其更短:

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}%{tikz-cd}
\usetikzlibrary{chains,
                fit,
                positioning}
\tikzset{
every edge quotes/.style={fill=white, fill opacity=0.8, text opacity=1,
                          font=\footnotesize}
}
\begin{document}
    \begin{tikzpicture}[
node distance = 21mm and 7mm,
  start chain = going right
                        ]
    \begin{scope}[every node/.style={draw, inner xsep=1pt, inner ysep=2pt, on chain}]
% top chain
\node   (00)    {$(0,0)$};
\node[below=of 00]  (0) {$0$};
\node   (1)     {$(1)$};
\node   (2)     {$(2)$};
\node   (3)     {$(3)$};
\node[draw=none]    (4) {$\textbf{\dots}$};
\node   (5)     {$(y{-}1)$};
\node   (6)     {$(y)$};
\node   (7)     {$(y{+}1)$};
\node   (8)     {$(y{+}2)$};
\node[draw=none]    (9) {$\textbf{\dots}$};
% bottom chain
\node[below=of 0]   (16)    {$(x+1)$};
\node   (15)    {$(x)$};
\node   (14)    {$(x{-1})$};
\node[draw=none]    (13)    {$\textbf{\dots}$};
\node   (12)    {$(x{-}b{+}1)$};
\node   (11)    {$(x{-}b)$};
\node   (10)    {$(x{-}b{-}1)$};
    \end{scope}
% legend
\coordinate[below=1mm of 00.south -| 7]  (L);
\draw[blue] (L) -- + (1,0)               node[right] {a};
\draw[red]  ([yshift=-3mm] L) -- + (1,0) node[right] {b};
\draw       ([yshift=-6mm] L) -- + (1,0) node[right] (LL)  {c};
\node[draw, yshift=0.5ex, fit=(L) (LL)] {};
% connections
%  a
\draw[->,blue]  (0)     edge [bend left]    (00)
                (00)    edge                (0)
                (6)     edge [bend right]   (1)
                (7)     edge [bend right]   (1)
% botttom
                (14)    edge [bend left]    (10)
                (15)    edge [bend left]    (10)
                (15)    edge [bend right]   (11)
                (16)    edge [bend right]   (11);
% b
\draw[->,red]   (7)     edge [bend right]   (2)
                (8)     edge [bend right]   (2);
% c
\foreach \i in {1,2,3, 6,7,8, 11,12, 15,16}
{
\pgfmathsetmacro{\ii}{int(\i-1)}
\draw[->]   (\ii) -- (\i);
}
%
\draw[-,dashed] (9) edge [bend left] (10);
    \end{tikzpicture}
\end{document}

相关内容