2

I read this. They suggest using transform canvas, which is rescaling but also it moves the picture where there is text (see figure). I also tried using \resizebox{}{} or \scalebox{}{}, bu that just makes the picture disappear.

This is how I generate the picture

\begin{center}
\begin{tikzpicture}[thick,scale=1.2,transform canvas={scale=1.2}, 
     clear/.style={ 
         draw=none,
         fill=none
     },
     net/.style={
         matrix of nodes,
         nodes={ draw, circle, inner sep=10pt },
         nodes in empty cells,
         column sep=2cm,
         row sep=-9pt
     },
     >=latex
]
% define matrix mat to hold nodes
% using net as default style for cells
\matrix[net] (mat)
{
% Define layer headings
|[clear]| \parbox{1.3cm}{\centering Input\\layer} 
    & |[clear]| \parbox{1.3cm}{\centering Hidden\\layer} 
    & |[clear]| \parbox{1.3cm}{\centering Output\\layer} \\
|[clear]| &      & |[clear]| \\         
  &     |[clear]|   & |[clear]| \\
  |[clear]| &   &|[clear]| \\
  & |[clear]| & |[clear]| \\
  |[clear]| & & \\
  & |[clear]| & |[clear]| \\
  |[clear]| & & |[clear]| \\
   & |[clear]| & |[clear]| \\
   |[clear]| & & |[clear]| \\
};


\draw[-] (mat-3-1) -- node[above] {$s_t^1$} +(-1.5cm,0);
\draw[-] (mat-5-1) -- node[above] {$s_t^2$} +(-1.5cm,0);
\draw[-] (mat-7-1) -- node[above] {$s_t^3$} +(-1.5cm,0);
\draw[-] (mat-9-1) -- node[above] {$s_t^4$} +(-1.5cm,0);
% lines from a_{i}^{0} to each a_{j}^{1}
\foreach \ai in {3,5,...,9} {
    \foreach \aii in {2,4,6,8,10}
        \draw[-] (mat-\ai-1) -- (mat-\aii-2);
        }
% lines from a_{i}^{1} to a_{0}^{2}
\foreach \ai in {2,4,6,8,10}
  \draw[-] (mat-\ai-2) -- (mat-6-3);

% right most line with Output label
\draw[->] (mat-6-3) -- node[above] {Output} +(2cm,0);
\end{tikzpicture}
\end{center}

Does anyone have any ideas how to fix that?

3
  • I would like to rescale. In that case I just used 1, but I want to be able to use 0.5,2, etc.
    – Schach21
    Commented May 16, 2020 at 0:52
  • transform canvas is sort of a dirty trick that does not move the object, but moves the camera. \scalebox, I believe, is also a camera-moving trick. It is pretty common that after several moving commands the camera is facing at somewhere out of bounds, hence you see nothing.
    – Symbol 1
    Commented May 16, 2020 at 0:57
  • Do you know how I could fix it then?
    – Schach21
    Commented May 16, 2020 at 0:59

1 Answer 1

2

One way to be able to use \scalebox and the like here is to add an ampersand replacement to the matrix.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{center}
\scalebox{0.5}{\begin{tikzpicture}[thick,
     clear/.style={ 
         draw=none,
         fill=none
     },
     net/.style={ampersand replacement=\&,
         matrix of nodes,
         nodes={ draw, circle, inner sep=10pt },
         nodes in empty cells,
         column sep=2cm,
         row sep=-9pt
     },
     >=latex
]
% define matrix mat to hold nodes
% using net as default style for cells
\matrix[net] (mat)
{
% Define layer headings
|[clear]| \parbox{1.3cm}{\centering Input\\layer} 
    \& |[clear]| \parbox{1.3cm}{\centering Hidden\\layer} 
    \& |[clear]| \parbox{1.3cm}{\centering Output\\layer} \\
|[clear]| \&      \& |[clear]| \\         
  \&     |[clear]|   \& |[clear]| \\
  |[clear]| \&   \&|[clear]| \\
  \& |[clear]| \& |[clear]| \\
  |[clear]| \& \& \\
  \& |[clear]| \& |[clear]| \\
  |[clear]| \& \& |[clear]| \\
   \& |[clear]| \& |[clear]| \\
   |[clear]| \& \& |[clear]| \\
};
\draw[-] (mat-3-1) -- node[above] {$s_t^1$} +(-1.5cm,0);
\draw[-] (mat-5-1) -- node[above] {$s_t^2$} +(-1.5cm,0);
\draw[-] (mat-7-1) -- node[above] {$s_t^3$} +(-1.5cm,0);
\draw[-] (mat-9-1) -- node[above] {$s_t^4$} +(-1.5cm,0);
% lines from a_{i}^{0} to each a_{j}^{1}
\foreach \ai in {3,5,...,9} {
    \foreach \aii in {2,4,6,8,10}
        \draw[-] (mat-\ai-1) -- (mat-\aii-2);
        }
% lines from a_{i}^{1} to a_{0}^{2}
\foreach \ai in {2,4,6,8,10}
  \draw[-] (mat-\ai-2) -- (mat-6-3);
% right most line with Output label
\draw[->] (mat-6-3) -- node[above] {Output} +(2cm,0);
\end{tikzpicture}}
\end{center}
\end{document}

One should say that this cures the symptoms. Yet to have a more elegant solution you may want to (fundamentally) rewrite the code, starting from questioning the use of the \parboxes in a matrix.

One way of getting a very similar output is

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{center}
\begin{tikzpicture}[scale=0.8,transform shape,
neuron/.style={circle,inner sep=1em,draw}]
 \begin{scope}[x=3cm,y=1.5cm,local bounding box=net]  
 \foreach \Y [count=\Z,remember=\Y as \LastY] in 
 {4,5,1} 
 {\foreach \YY in {1,...,\Y}
 {\node[neuron] (n-\Z-\YY) at (\Z,-\YY+\Y/2+1/2) {};
 \ifnum\Z=1
  \draw (n-\Z-\YY.west) -- node[above] {$s^t_{\YY}$}++ (-2em,0);
 \else
  \foreach \YYY in {1,...,\LastY}
  {\draw[-,semithick] (n-\the\numexpr\Z-1\relax-\YYY) -- (n-\Z-\YY) ;}
 \fi
 }}
 \end{scope} 
 \path foreach \X [count=\Y] in {Input,Hidden,Outpt}
 { (net.north-|n-\Y-1) node[above=1em,align=center] {\X\\ Layer}
 };
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

I put a nontrivial scale factor in to show that it works. However, you have other, arguably better handles to control the size of this diagram. By changing x and y in the scope you control the horizontal and vertical distances between the neurons, respectively. The sizes of the latter are controlled by the style. This code also avoids the need for adding |[clear]| all over, and if you want to change the number of neurons per layer, all you need to change is {4,5,1}. Of course, there are many other examples of drawing neuronal networks on this site (and I thought there is even a package but for some reason I can't find it now).

4
  • What's wrong with that? I copied that code from somewhere and adapted it to fit my needs. So, I don't really understand every part of it.
    – Schach21
    Commented May 16, 2020 at 1:06
  • 2
    @Schach21 First of all, as long as you are happy with the result, the code suits is purpose, and is fine by definition. However, to the best of my knowledge one should not use \parbox in a node, there are keys like text with and align=left for that. The consequences of such practice are usually not immediate nor dramatic, but can be annoying. Also, by using a matrix in the way you do you limit yourself in the number of nodes you use, and are forced to use a lot of repetition like adding the |[clear]| keys all over. I'll add an alternative that is free of at least these problems.
    – user194703
    Commented May 16, 2020 at 1:14
  • 2
    @Schach21 I added an alternative just so that this is not only a rant. ;-)
    – user194703
    Commented May 16, 2020 at 1:30
  • 1
    Thank you very much! This is very helpful. I hope you stay alive or a superposition of dead and alive, Mr Schröndnger's cat.
    – Schach21
    Commented May 16, 2020 at 4:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .