TikZ: having trouble drawing a randomly generated graph on the background of a page

You can do this with the background package: \usepackage[pages=some]{background}.

It requires a setup:

\backgroundsetup{
scale=1,
color=black,
opacity=0.4,
angle=0,
contents={ your tikz picture }
}

Then, add \BgThispage to the page in which you want to use your background.

Full code:

\documentclass{article}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath} 
\usepackage{xcolor}
\usepackage{pagecolor}
\usepackage{fancyhdr}
\usepackage{tikz}
\usepackage[pages=some]{background}
\usetikzlibrary{backgrounds}

\definecolor{blueish}{rgb}{0.565,0.886,1}  % blue-ish
\definecolor{greenish}{rgb}{0.565,1,0.886}  % green-ish
\definecolor{darkgray}{rgb}{0.15,0.15,0.15}  % very dark gray
\definecolor{lightgray}{rgb}{0.6,0.6,0.6}  % light gray

\makeatletter
\newcommand{\canvaswidth}{12}
\newcommand{\canvasheight}{12}

\newcommand{\gettikzxy}[3]{ % I got this from http://tex.stackexchange.com/questions/33703/extract-x-y-coordinate-of-an-arbitrary-point-in-tikz
    \tikz@scan@one@point\pgfutil@firstofone#1\relax
    \edef#2{\the\pgf@x}
    \edef#3{\the\pgf@y}
}

\fancypagestyle{scifi}{%
    \fancyhf{}% Clear header/footer
    \fancyfoot[C]{\textcolor{blueish}{\thepage}}% Page # in middle/centre of footer
}
\makeatother

\tikzstyle{circlenode}=[circle, draw=blueish]
\tikzstyle{boxnode}=[rectangle, draw=greenish]

\pgfmathsetseed{\number\pdfrandomseed}

\pagestyle{scifi}

\backgroundsetup{
scale=1,
color=black,
opacity=0.4,
angle=0,
contents={%
  \begin{tikzpicture}
        \pgfmathparse{random(20, 40)}
        \pgfmathtruncatemacro\nrOfNodes{\pgfmathresult}
        \foreach \i in {1,...,\nrOfNodes} {
            \pgfmathsetmacro\posX{rnd*(\canvaswidth)}
            \pgfmathsetmacro\posY{rnd*(\canvasheight)}
            \pgfmathparse{random(1, 3)}
            \pgfmathtruncatemacro\nodetype{\pgfmathresult}
            \ifnum\nodetype>1
                \node[circlenode] (a\i) at (\posX, \posY) {};
            \else
                \node[boxnode] (a\i) at (\posX, \posY) {};
            \fi
            \pgfmathparse{random(60, 80)}
            \pgfmathtruncatemacro\diameter{\pgfmathresult}
            \begin{pgfonlayer}{background}
                \draw[color=black,fill=blue, opacity=0.05] (a\i) circle (\diameter pt);
            \end{pgfonlayer}
            \ifnum\i>1
                \pgfmathsetmacro\last{\i -1}
                \foreach \j in {1,...,\last} {
                    \gettikzxy{(a\i)}{\pX}{\pY};
                    \gettikzxy{(a\j)}{\qX}{\qY};
                    \pgfmathsetmacro\diffX{(\pX-\qX)/100}
                    \pgfmathsetmacro\diffY{(\pY-\qY)/100}
                    \pgfmathsetmacro\calculatedDistance{ sqrt( (\diffX)^2 + (\diffY)^2 ) * 100};
                    \ifdim\calculatedDistance pt <\diameter pt
                        \begin{pgfonlayer}{background}
                            \draw[lightgray] (a\i) -- (a\j) node [midway, above, sloped] {};
                        \end{pgfonlayer}
                    \fi
                }
            \fi
        }
    \end{tikzpicture}
  }%
}

\title{A test sci-fi document}
\date{2958.16.32}

\begin{document}

\pagecolor{darkgray}
\color{blueish}

\BgThispage

\maketitle
\reversemarginpar

% -----------------------------------------
\begin{enumerate}
    \item How many errors can each of the following codes detect/correct? (assuming NND decoder)
        \begin{enumerate}
            \item $\mathcal{C} = \{000000, 111111, 000111\}, q = 2$

            Computing the minimum distance between any two code words:
                \begin{center}
                    \begin{tabular}{c|c|c|c}
                            & $000000$ & $111111$ & $000111$ \\
                        \hline
                        $000000$ & $0$ & &  \\
                        \hline
                        $111111$ & $6$ & $0$ & \\
                        \hline
                        $000111$ & $3$ & $3$ & $0$ \\
                    \end{tabular}
                \end{center}

            Since the minimum distance between code words is $3$:
                \begin{align*}
                    &u \leq d - 1 \\
                    &u \leq 3 - 1 \\
                    &u \leq 2 \\
                    & \\
                    &v \leq \lfloor\frac{d - 1}{2}\rfloor \\
                    &v \leq \lfloor\frac{3 - 1}{2}\rfloor \\
                    &v \leq 1
                \end{align*}

            So, this code can correct $1$ or less errors, and can detect $2$ or less errors.

        \end{enumerate}

\end{enumerate}

\end{document}

enter image description here


I started from Maarten Dhondt's answer as I would recommend the same solution.

As you note, this solution shrinks the nodes in the picture, although other parts of the picture are rendered at their normal sizes.

Investigating, I tweaked a couple of things just to make sure the definitions etc. were unique and to rule out possible clashes. Guessing failed, so I looked at the source of background.sty and found the culprit.

\newcommand\bg@material{%
  \begin{tikzpicture}[remember picture,overlay,scale=\Background@Scale]
  \node[
    rotate=\Background@Angle,
    scale=\Background@Scale,
    opacity=\Background@Opacity,
    anchor=\Background@NodeAnchor,
    xshift=\Background@HShift,
    yshift=\Background@VShift,
    color=\Background@Color,
    inner sep=0pt
    ]
    at (\Background@Position) [\Background@Anchor]
      {\Background@Contents};
  \end{tikzpicture}}%
\newcommand\BgThispage{\AddThispageHook{\bg@material}}

The key change here is that inner sep is set to 0pt. Since your nodes have no content, this essentially shrinks them to nothing. Normally, the inner sep means that even an empty node has a significant dimension.

The solution is to restore the standard value of inner sep within the tikzpicture environment. This can be found in pgfmoduleshapes.code.tex.

\pgfset{
  inner xsep/.initial     =.3333em,
  inner ysep/.initial     =.3333em,
  inner sep/.style        ={/pgf/inner xsep=#1,/pgf/inner ysep=#1},
  ...

So, if we set inner sep=.3333em for the picture, then we should get the expected rendering of the nodes.

sci-fi nodes restored

Complete code (note that \tikzstyle is deprecated - it is updated below):

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{pagecolor}
\usepackage{fancyhdr}
\usepackage{tikz}
\usepackage[pages=some]{background}
\usetikzlibrary{backgrounds}

\definecolor{blueish}{rgb}{0.565,0.886,1}  % blue-ish
\definecolor{greenish}{rgb}{0.565,1,0.886}  % green-ish
\definecolor{darkgray}{rgb}{0.15,0.15,0.15}  % very dark gray
\definecolor{lightgray}{rgb}{0.6,0.6,0.6}  % light gray

\makeatletter
\newcommand{\mycanvaswidth}{12}
\newcommand{\mycanvasheight}{12}

\newcommand{\gettikzxy}[3]{ % I got this from https://tex.stackexchange.com/questions/33703/extract-x-y-coordinate-of-an-arbitrary-point-in-tikz
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \edef#2{\the\pgf@x}%
  \edef#3{\the\pgf@y}%
}

\fancypagestyle{scifi}{%
  \fancyhf{}% Clear header/footer
  \fancyfoot[C]{\textcolor{blueish}{\thepage}}% Page # in middle/centre of footer
}

\tikzset{
  circlenode/.style={circle, draw=blueish},
  boxnode/.style={rectangle, draw=greenish},
}

\pgfmathsetseed{\number\pdfrandomseed}

\pagestyle{scifi}

\makeatother

\backgroundsetup{
  scale=1,
  color=black,
  opacity=0.4,
  angle=0,
  contents={%
    \begin{tikzpicture}[inner sep=.3333em]
      \pgfmathparse{random(20, 40)}
      \pgfmathtruncatemacro\nrOfNodes{\pgfmathresult}
      \foreach \i in {1,...,\nrOfNodes}
      {
        \pgfmathsetmacro\posX{rnd*(\mycanvaswidth)}
        \pgfmathsetmacro\posY{rnd*(\mycanvasheight)}
        \pgfmathparse{random(1, 3)}
        \pgfmathtruncatemacro\nodetype{\pgfmathresult}
        \ifnum\nodetype>1
          \node[circlenode] (a\i) at (\posX, \posY) {};
        \else
          \node[boxnode] (a\i) at (\posX, \posY) {};
        \fi
        \pgfmathparse{random(60, 80)}
        \pgfmathtruncatemacro\diameter{\pgfmathresult}
        \begin{pgfonlayer}{background}
          \draw[color=black,fill=blue, opacity=0.05] (a\i) circle (\diameter pt);
        \end{pgfonlayer}
        \ifnum\i>1
        \pgfmathsetmacro\last{\i -1}
        \foreach \j in {1,...,\last} {
          \gettikzxy{(a\i)}{\pX}{\pY};
          \gettikzxy{(a\j)}{\qX}{\qY};
          \pgfmathsetmacro\diffX{(\pX-\qX)/100}
          \pgfmathsetmacro\diffY{(\pY-\qY)/100}
          \pgfmathsetmacro\calculatedDistance{ sqrt( (\diffX)^2 + (\diffY)^2 ) * 100};
          \ifdim\calculatedDistance pt <\diameter pt
          \begin{pgfonlayer}{background}
            \draw[lightgray] (a\i) -- (a\j) node [midway, above, sloped] {};
          \end{pgfonlayer}
          \fi
        }
        \fi
      }
    \end{tikzpicture}
  }%
}

\title{A test sci-fi document}
\author{a}
\date{2958.16.32}

\begin{document}
\pagecolor{darkgray}
\color{blueish}
\BgThispage
\maketitle
\reversemarginpar
\begin{enumerate}
  \item How many errors can each of the following codes detect/correct? (assuming NND decoder)
  \begin{enumerate}
    \item $\mathcal{C} = \{000000, 111111, 000111\}, q = 2$

    Computing the minimum distance between any two code words:
    \begin{center}
      \begin{tabular}{c|c|c|c}
        & $000000$ & $111111$ & $000111$ \\
        \hline
        $000000$ & $0$ & &  \\
        \hline
        $111111$ & $6$ & $0$ & \\
        \hline
        $000111$ & $3$ & $3$ & $0$ \\
      \end{tabular}
    \end{center}

    Since the minimum distance between code words is $3$:
    \begin{align*}
      &u \leq d - 1 \\
      &u \leq 3 - 1 \\
      &u \leq 2 \\
      & \\
      &v \leq \lfloor\frac{d - 1}{2}\rfloor \\
      &v \leq \lfloor\frac{3 - 1}{2}\rfloor \\
      &v \leq 1
    \end{align*}

    So, this code can correct $1$ or less errors, and can detect $2$ or less errors.
  \end{enumerate}
\end{enumerate}
\end{document}