TikZ:在页面背景上绘制随机生成的图形时遇到问题

TikZ:在页面背景上绘制随机生成的图形时遇到问题

我有一些生成随机图的代码:

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

它产生如下输出:

在此处输入图片描述

我还有一些代码可以用数学来生成一些示例文本(它具有与图形代码相同的“标题内容”):

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

它产生如下输出:

在此处输入图片描述

我尝试用这个问题的答案是否可以使用 tikz 在打印页面上绘制背景?为了把随机图作为文本的背景,但我遇到了各种编译错误,可能是由于语法错误等等引起的。

因此,与其列举所有这些毫无意义的错误,我是否可以问:我应该如何合并这两个文档?特别是,我是否应该跳过使用该backgrounds包?

答案1

我从Maarten Dhondt 的回答因为我会推荐相同的解决方案。

正如您所注意到的,该解决方案缩小了图片中的节点,尽管图片的其他部分仍以正常大小呈现。

调查后,我调整了几项内容,以确保定义等是唯一的,并排除可能的冲突。猜测失败了,所以我查看了源头background.sty并找到了罪魁祸首。

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

这里的关键变化是将inner sep设置为0pt。由于您的节点没有内容,因此这实际上将它们缩小为无。通常,这inner sep意味着即使是空节点也具有相当大的维度。

解决方案是恢复环境inner sep中的标准值tikzpicture。这可以在 中找到pgfmoduleshapes.code.tex

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

因此,如果我们设置inner sep=.3333em了图片,那么我们应该得到节点的预期渲染。

科幻节点恢复

完整代码(请注意,\tikzstyle已被弃用 - 下面进行了更新):

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

答案2

您可以使用以下background包来执行此操作:\usepackage[pages=some]{background}

它需要一个设置:

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

然后,添加\BgThispage到您想要使用背景的页面。

完整代码:

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

在此处输入图片描述

相关内容