3

I am using this Symbols in my work. I am trying to add a legend to this code to annotate the graph. I found this code Legend in Tikzpicture So I have now the following code:

\documentclass[tikz,border=5]{standalone}
\renewcommand\familydefault\sfdefault
\usetikzlibrary{fit,shapes.geometric}
\pgfdeclarelayer{signal}
\pgfsetlayers{signal,main}
\usepackage{pgfplots}
\usetikzlibrary{arrows,arrows.meta}
\tikzset{pics/.cd,
  SBS/.style={code={
      \begin{scope}[local bounding box=#1]
      \fill [pic actions/.try] (-1,0) -- (-1/2,3) -- (1/2, 3) -- (1,0) -- cycle;
      \fill [pic actions/.try] (-1/16,2) rectangle (1/16,4);
      \fill [pic actions/.try] (0,4) circle [radius=1/4];
      \foreach \i in {-1,1}
        \fill [shift=(90:4), xscale=\i]
          \foreach \r in {1,3/2,2}{
            (-45:\r) arc (-45:45:\r) -- (45:\r-1/10)
            arc(45:-45:\r-1/10) -- cycle
          };
       \end{scope}
  }},
  SU/.style={code={ 
    \begin{scope}[local bounding box=#1]
      \fill [even odd rule, pic actions/.try] 
        (-1,-5/2) -- (-1,-1/8) -- (1,-1/8) -- (1,-5/2)
        arc (360:180:1 and 1/4) -- cycle
     (-1,5/2) -- (-1,1/8) -- (1,1/8) -- (1,5/2)
        arc (0:180:1 and 1/4) -- cycle
     (-3/4, 9/4) -- (-3/4, 3/8) -- (3/4, 3/8) -- (3/4, 9/4) 
     arc (0:180:3/4 and 1/8)-- cycle
     \foreach \i in {-1,0,1}{\foreach \j in {1,2,3}{
       (-\i*1/2-3/16,-\j/2-3/4) rectangle ++(3/8, 3/8)}}
     (-1/2,-3/4) rectangle (1/2, -1/4);
   \end{scope}
  }},
  SIGNAL/.style={code={
    \begin{scope}[local bounding box=#1]
      \fill [pic actions/.try]
      (0,-3) -- (-1,1/2) -- (1/8,1/4) -- (0,3) -- (1,-1/2) -- (-1/8,-1/4)
      -- cycle;
    \end{scope}
  }}
}

\newenvironment{customlegend}[1][]{%
    \begingroup
    % inits/clears the lists (which might be populated from previous
    % axes):
    \csname pgfplots@init@cleared@structures\endcsname
    \pgfplotsset{#1}%
}{%
    % draws the legend:
    \csname pgfplots@createlegend\endcsname
    \endgroup
}%

% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}

%%--------------------------------
% definition to insert numbers
\pgfkeys{/pgfplots/number in legend/.style={%
        /pgfplots/legend image code/.code={%
            \node at (0.295,-0.0225){#1};
        },%
    },
}
\colorlet{sky blue}{blue!60!cyan!75!black}
\colorlet{dark blue}{blue!50!cyan}
\colorlet{chameleon}{olive!75!green}
\tikzset{signal/.style={draw=gray, line width=0.2em, dashed}}
\begin{document}
\begin{tikzpicture}[x=1em,y=1em]

\begin{scope}[local bounding box=b1]
\pic [fill=sky blue] {SBS=p1};
\pic [rotate=15, fill=sky blue]  at (8,-4)  {SU=q1};
\pic [rotate=45, fill=sky blue] at (4,-2)  {SIGNAL=s1};
\node [line width=0.25em,draw=sky blue, shape=circle, fit={(p1) (q1) (s1)}] {};
\path (p1.north) node [below] {p1} (q1.north east) node [above] {q1};
\end{scope}

\begin{scope}[shift={(25,5)}, x=1em*7/8, y=1em*7/8, local bounding box=b2]
\pic [fill=chameleon] {SBS=p2};
\pic [fill=chameleon]  at (-8,0)  {SU=q2};
\pic [rotate=-75, fill=chameleon] at (-4,1)  {SIGNAL=s2};
\node [line width=0.25em,draw=chameleon, inner sep=1em,
  shape=circle, fit={(p2) (q2) (s2)}] {};
\path (p2.south) node [below] {p2} (q2.south) node [below] {q2};
\end{scope}

\begin{scope}[shift={(5,20)}, x=1em*5/8, y=1em*5/8, local bounding box=b3]
\pic [fill=violet] {SBS=p3};
\pic [fill=violet]  at (8,0)  {SU=q3};
\pic [rotate=-75, fill=violet] at (4,1)  {SIGNAL=s3};
\node [line width=0.25em,  draw=violet, inner sep=1em,
  shape=circle, fit={(p3) (q3) (s3)}] {};
\path (p3.north) node [above] {p3} (q3.south) node [below] {q3};
\end{scope}

\begin{pgfonlayer}{signal}
\draw [signal] 
  (p1) -- (q3) -- (p2) -- (q1) -- (p3) -- (q2) -- (p1) 
  (p1) -- (q1) (p2) -- (q2) (p3) -- (q3);
\end{pgfonlayer}

\node [regular polygon, regular polygon sides=6, fit={(b1) (b2) (b3)},
draw=black, line width=0.25em, inner sep=-2em]
{};

\pic [fill=dark blue] at (-5, 10) {SBS};
\pic [fill=dark blue] at (25, 15) {SBS};
\pic [fill=dark blue] at (20, -8) {SU};

\begin{customlegend}[
      legend entries={ % <= in the following there are the entries
      Interference, 
      Strong Signal,
      Weak Signal
      },
      scale=4,
      % legend cell align={left},
      legend style={at={(-4,0)}}] % <= to define position and font legend
      % the following are the "images" and numbers in the legend
      \addlegendimage{-triangle 45,red,line width=0.3mm}
      \addlegendimage{-triangle 45,cyan,line width=0.3mm}
      \addlegendimage{-triangle 45,cyan,dashed,line width=0.3mm}
\end{customlegend}

\end{tikzpicture}
\end{document}

My problem is that the legend is always small, I tried to add nodes={scale=2} which increased the text but not the arrows. I have also some emoticons in my legend which appear really small.

How can I make the legend large (including the text and the symbols and nodes and emoticons)?

Is there any better way to add a legend? Say I would like to add the phone to the legend how can I do this?

I have this result so far:

enter image description here

Zir
  • 185

1 Answers1

2

I can only offer a workaround as I do not understand macros like pgfplots@init@cleared@structures or \csname pgfplots@createlegend\endcsname well enough to dare playing with them. So I propose to draw the legend by hand.

\documentclass[tikz,border=5]{standalone}
\renewcommand\familydefault\sfdefault
\usetikzlibrary{fit,shapes.geometric}
\pgfdeclarelayer{signal}
\pgfdeclarelayer{legend back}
\pgfdeclarelayer{legend}
\pgfsetlayers{signal,main,legend back,legend}
\usetikzlibrary{arrows,arrows.meta}
\tikzset{pics/.cd,
  SBS/.style={code={
      \begin{scope}[local bounding box=#1]
      \fill [pic actions/.try] (-1,0) -- (-1/2,3) -- (1/2, 3) -- (1,0) -- cycle;
      \fill [pic actions/.try] (-1/16,2) rectangle (1/16,4);
      \fill [pic actions/.try] (0,4) circle [radius=1/4];
      \foreach \i in {-1,1}
        \fill [shift=(90:4), xscale=\i]
          \foreach \r in {1,3/2,2}{
            (-45:\r) arc (-45:45:\r) -- (45:\r-1/10)
            arc(45:-45:\r-1/10) -- cycle
          };
       \end{scope}
  }},
  SU/.style={code={ 
    \begin{scope}[local bounding box=#1]
      \fill [even odd rule, pic actions/.try] 
        (-1,-5/2) -- (-1,-1/8) -- (1,-1/8) -- (1,-5/2)
        arc (360:180:1 and 1/4) -- cycle
     (-1,5/2) -- (-1,1/8) -- (1,1/8) -- (1,5/2)
        arc (0:180:1 and 1/4) -- cycle
     (-3/4, 9/4) -- (-3/4, 3/8) -- (3/4, 3/8) -- (3/4, 9/4) 
     arc (0:180:3/4 and 1/8)-- cycle
     \foreach \i in {-1,0,1}{\foreach \j in {1,2,3}{
       (-\i*1/2-3/16,-\j/2-3/4) rectangle ++(3/8, 3/8)}}
     (-1/2,-3/4) rectangle (1/2, -1/4);
   \end{scope}
  }},
  SIGNAL/.style={code={
    \begin{scope}[local bounding box=#1]
      \fill [pic actions/.try]
      (0,-3) -- (-1,1/2) -- (1/8,1/4) -- (0,3) -- (1,-1/2) -- (-1/8,-1/4)
      -- cycle;
    \end{scope}
  }}
}


\colorlet{sky blue}{blue!60!cyan!75!black}
\colorlet{dark blue}{blue!50!cyan}
\colorlet{chameleon}{olive!75!green}
\tikzset{signal/.style={draw=gray, line width=0.2em, dashed}}
\begin{document}
\begin{tikzpicture}[x=1em,y=1em]

\begin{scope}[local bounding box=b1]
\pic [fill=sky blue] {SBS=p1};
\pic [rotate=15, fill=sky blue]  at (8,-4)  {SU=q1};
\pic [rotate=45, fill=sky blue] at (4,-2)  {SIGNAL=s1};
\node [line width=0.25em,draw=sky blue, shape=circle, fit={(p1) (q1) (s1)}] {};
\path (p1.north) node [below] {p1} (q1.north east) node [above] {q1};
\end{scope}

\begin{scope}[shift={(25,5)}, x=1em*7/8, y=1em*7/8, local bounding box=b2]
\pic [fill=chameleon] {SBS=p2};
\pic [fill=chameleon]  at (-8,0)  {SU=q2};
\pic [rotate=-75, fill=chameleon] at (-4,1)  {SIGNAL=s2};
\node [line width=0.25em,draw=chameleon, inner sep=1em,
  shape=circle, fit={(p2) (q2) (s2)}] {};
\path (p2.south) node [below] {p2} (q2.south) node [below] {q2};
\end{scope}

\begin{scope}[shift={(5,20)}, x=1em*5/8, y=1em*5/8, local bounding box=b3]
\pic [fill=violet] {SBS=p3};
\pic [fill=violet]  at (8,0)  {SU=q3};
\pic [rotate=-75, fill=violet] at (4,1)  {SIGNAL=s3};
\node [line width=0.25em,  draw=violet, inner sep=1em,
  shape=circle, fit={(p3) (q3) (s3)}] {};
\path (p3.north) node [above] {p3} (q3.south) node [below] {q3};
\end{scope}

\begin{pgfonlayer}{signal}
\draw [signal] 
  (p1) -- (q3) -- (p2) -- (q1) -- (p3) -- (q2) -- (p1) 
  (p1) -- (q1) (p2) -- (q2) (p3) -- (q3);
\end{pgfonlayer}

\node [regular polygon, regular polygon sides=6, fit={(b1) (b2) (b3)},
draw=black, line width=0.25em, inner sep=-2em]
{};

\pic [fill=dark blue] at (-5, 10) {SBS};
\pic [fill=dark blue] at (25, 15) {SBS};
\pic [fill=dark blue] at (20, -8) {SU};

\begin{scope}[local bounding box=legend,shift={(-8,0)},scale=3,transform shape]
\begin{pgfonlayer}{legend}
\pgfgettransformentries{\myscale}{\tmp}{\tmp}{\tmp}{\tmp}{\tmp}
\coordinate (aux) at (-0.1,0);
\draw[-triangle 45,red,line width=\myscale*0.3mm] (0,0) -- ++ (1.6,0) 
node[right,black]{Interference};
\draw[-triangle 45,cyan,line width=\myscale*0.3mm] (0,-1) -- ++ (1.6,0) 
node[right,black]{Strong Signal};
\draw[-triangle 45,cyan,dashed,line width=\myscale*0.3mm] (0,-2) -- ++ (1.6,0) 
node[right,black]{Weak Signal};
\end{pgfonlayer}
\end{scope}
\begin{pgfonlayer}{legend back}
\node[fit=(legend) (aux),fill=white,thick,draw,inner sep=2pt]{};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}

enter image description here

Let me remark that you are loading both arrows and arrows.meta, but only use arrows. Even though it does not really matter too much here, I'd recommend using arrows.meta instead.

  • Thank you. If I would like to add the phone SU for example to the legend, is it possible with \pic [fill=black] at (11,1) {SU};? – Zir Nov 11 '18 at 22:32
  • 1
    @Zir I would just add \pic[scale=0.2] at (0.5,-3) {SU} (1.6,-3) node[right]{phone}; before the \end{pgfonlayer} of the legend scope. This code is set up in such a way that the legend box will just grow accordingly. –  Nov 11 '18 at 22:43