11

I have some code to generate random graphs:

\documentclass{article}
% code hijacked from: https://gist.github.com/fgrosse/5807324
\usepackage[utf8x]{inputenc}

\usepackage{amsmath} 
\usepackage{xcolor}
\usepackage{pagecolor}
\usepackage{fancyhdr}

\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

\usepackage{tikz}
\usetikzlibrary{backgrounds}

\makeatletter
\newcommand{\canvaswidth}{12}
\newcommand{\canvasheight}{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}
}
\makeatother

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

\pgfmathsetseed{\number\pdfrandomseed}

\begin{document}

\pagecolor{darkgray}
\color{blueish}

    \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}

            % draw node connections
            \ifnum\i>1
                \pgfmathsetmacro\last{\i -1}
                \foreach \j in {1,...,\last} {
                    \gettikzxy{(a\i)}{\pX}{\pY};
                    \gettikzxy{(a\j)}{\qX}{\qY};

                    % divide by 100 so the values are still small enough for tickz to handle while preserving adequate precision
                    \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}
\end{document}

It produces output like:

enter image description here

I also have some code to produce some sample text with math (it has the same "header content" as the graph code):

\documentclass{article}
% code hijacked from: https://gist.github.com/fgrosse/5807324
\usepackage[utf8x]{inputenc}
\usepackage{concmath}

\usepackage{amsmath} 
\usepackage{xcolor}
\usepackage{pagecolor}
\usepackage{fancyhdr}

\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

\usepackage{tikz}
\usetikzlibrary{backgrounds}

\makeatletter
\newcommand{\canvaswidth}{12}
\newcommand{\canvasheight}{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}
}
\makeatother

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

\pgfmathsetseed{\number\pdfrandomseed}

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

\pagestyle{scifi}

\begin{document}

\pagecolor{darkgray}
\color{blueish}

\title{A test sci-fi document}

\date{2958.16.32}

\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}

It produces output like this:

enter image description here

I tried to use the answer to this question Is it possible to use tikz to draw a background on the printed page? in order to put the random graph as the background of the text, but I ran into various compile errors, likely caused due to syntax errors, and so on.

So, rather than enumerate all those pointless errors, can I ask instead: how should I go about combining these two documents? In particular, should I skip using the backgrounds package?

2 Answers 2

3
+50

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}
6

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

3
  • Thanks! This is great. Couple of things though : 1) the node sizes are screwed up, but staring through the code didn't immediately reveal why -- see the original output -- the node sizes are such that the circles/boxes are easily distinguishable; 2) there can't be any empty lines in the TikZ code?
    – bzm3r
    Jan 18, 2016 at 21:38
  • Additional testing on my part confirms that no changes have been made to the code...so why are the node sizes in the background image screwed up?
    – bzm3r
    Jan 18, 2016 at 21:40
  • @user89: I can't answer why, but it has something to do with the default size settings for empty nodes with specified shapes. If you set the minimum size of the nodes (for example, adding minimum size=8pt to the circlenode and boxnode styles) the problem is fixed. +1 to Maarten for an excellent answer.
    – Guho
    Jan 21, 2016 at 2:11

You must log in to answer this question.

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