Fitting as a background to nodes in multiple pictures

One possibility is to use Andrew Stacey's improved version of \tikzmark (See his answer to tikzmark to have different behaviour if first run (and mark locations not yet available)); in the following example, the \placemark command is in charge of, well... placing the marks; its first mandatory argument can be used to select an anchor of the current bounding box, and the second argument gives the mark a name. The names can then be used as arguments for the \DrawRect macro:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{fit}

\makeatletter
\tikzset{%
  remember picture with id/.style={%
    remember picture,
    overlay,
    save picture id=#1,
  },
  save picture id/.code={%
    \edef\pgf@temp{#1}%
    \immediate\write\pgfutil@auxout{%
      \noexpand\savepointas{\pgf@temp}{\pgfpictureid}}%
  },
  if picture id/.code args={#1#2#3}{%
    \@ifundefined{save@pt@#1}{%
      \pgfkeysalso{#3}%
    }{
      \pgfkeysalso{#2}%
    }
  }
}

\def\savepointas#1#2{%
  \expandafter\gdef\csname save@pt@#1\endcsname{#2}%
}

\def\tmk@labeldef#1,#2\@nil{%
  \def\tmk@label{#1}%
  \def\tmk@def{#2}%
}

\tikzdeclarecoordinatesystem{pic}{%
  \pgfutil@in@,{#1}%
  \ifpgfutil@in@%
    \tmk@labeldef#1\@nil
  \else
    \tmk@labeldef#1,(0pt,0pt)\@nil
  \fi
  \@ifundefined{save@pt@\tmk@label}{%
    \tikz@scan@one@point\pgfutil@firstofone\tmk@def
  }{%
  \pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic%
  \pgfsys@getposition{\pgfpictureid}\save@this@pic%
  \pgf@process{\pgfpointorigin\save@this@pic}%
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  \pgf@process{\pgfpointorigin\save@orig@pic}%
  \advance\pgf@x by -\pgf@xa
  \advance\pgf@y by -\pgf@ya
  }%
}
\newcommand\tikzmark[2][]{%
\tikz[remember picture with id=#2] #1;}
\makeatother

\newcommand\placemark[2]{%
  \node[inner sep=0pt,outer sep=0pt] at #1 {\tikzmark{#2}}; 
}

\newcommand\DrawRect[2]{%
\begin{tikzpicture}[overlay,remember picture]%
  \node[draw=red,fill=yellow,fill opacity=0.8,fit=(pic cs:#1) (pic cs:#2),rectangle] {};
\end{tikzpicture}%
}

\begin{document}

\DrawRect{fnode}{snode}
\DrawRect{finode}{sinode}
\DrawRect{fiinode}{siinode}

 \begin{equation}
  x =
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=red,minimum width=1cm] (first node) {};
  \end{tikzpicture}
  +
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=cyan,minimum width=1cm] (first node) {};
   \placemark{(current bounding box.north west)}{fnode} 
  \end{tikzpicture}
  +
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=green,minimum width=1cm] (second node) {}; 
   \placemark{(current bounding box.south east)}{snode} 
  \end{tikzpicture}
 \end{equation}

 \begin{equation}
  x =
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=red,minimum width=1cm] (first node) {};
   \placemark{(current bounding box.north west)}{finode} 
  \end{tikzpicture}
  +
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=cyan,minimum width=1cm] (first node) {};
   \placemark{(current bounding box.south east)}{sinode} 
  \end{tikzpicture}
  +
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=green,minimum width=1cm] (second node) {}; 
  \end{tikzpicture}
 \end{equation}

\begin{align}
  x &=
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=red,minimum width=1cm] {}; 
  \end{tikzpicture}
  +
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=cyan,minimum width=1cm] (first node) {};
   \placemark{(current bounding box.north west)}{fiinode} 
  \end{tikzpicture}
  \\
  y &=
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=red,minimum width=1cm] {}; 
  \end{tikzpicture}
  +
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=cyan,minimum width=1cm] (second node) {}; 
  \end{tikzpicture}
  +
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=green,minimum width=1cm] (third node) {}; 
   \placemark{(current bounding box.south east)}{siinode} 
  \end{tikzpicture}
 \end{align}

\end{document}

enter image description here

The code needs to be run three times.


There may be a more elegant 'pure TikZ' solution, but you could push your rectangle to the background using the atbegshi package:

\documentclass{article}
\usepackage{atbegshi}
\usepackage{tikz}
\usetikzlibrary{fit}

\begin{document}
 \begin{equation}
  x =
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=cyan,minimum width=1cm] (first node) {}; 
  \end{tikzpicture}
  +
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=green,minimum width=1cm] (second node) {}; 
  \end{tikzpicture}
 \end{equation}

 \AtBeginShipoutNext{
   \AtBeginShipoutUpperLeft{
     \begin{tikzpicture}[remember picture,overlay]
      \node[draw=red,fill=yellow,fill opacity=0.8,fit=(first node) (second node),rectangle] {};
     \end{tikzpicture}
   }
 }
\end{document}

You can always use hf-tikz (thus two compilation runs are necessary):

\documentclass[b3paper]{baposter}
\usepackage{amsmath}
\usepackage[customcolors,norndcorners]{hf-tikz}
\tikzset{hf color setting/.style={
    set fill color=yellow,
    set border color=red,
  },
  hf specifications no vert align/.style={
    above left offset={-0.05cm,1.05cm},
    below right offset={0.05cm,-0.1cm},
    hf color setting,
  },
}

\begin{document}
\begin{poster}{bgColorOne=blue!20!white,background=plain}{}{}{}{}
 \begin{posterbox}[textborder=none,headerborder=none,span=3,boxColorOne=white]{title}
 \begin{align}
  x &=  
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=red,minimum width=1cm] {}; 
  \end{tikzpicture}
  +
  \tikzmarkin[hf specifications no vert align]{a}
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=cyan,minimum width=1cm] (first node) {}; 
  \end{tikzpicture}
  \\
  y &=
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=red,minimum width=1cm] {}; 
  \end{tikzpicture}
  +
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=cyan,minimum width=1cm] (second node) {}; 
  \end{tikzpicture}
  +
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=green,minimum width=1cm] (third node) {}; 
  \end{tikzpicture}
  \tikzmarkend{a}
 \end{align}
 \end{posterbox}
\end{poster}
\end{document}

The result:

enter image description here

Of course this needs baposter.cls to be compiled.


First version

\documentclass{article}
\usepackage[customcolors,norndcorners]{hf-tikz}
\tikzset{hf color setting/.style={
    set fill color=yellow,
    set border color=red,
  },
  hf specifications vert align/.style={
    above left offset={-0.05cm,0.75cm},
    below right offset={0.05cm,-0.55cm},
    hf color setting,
  },
  hf specifications no vert align/.style={
    above left offset={-0.05cm,1.05cm},
    below right offset={0.05cm,-0.1cm},
    hf color setting,
  },
}

% vertical alignment within math environments see:
% http://tex.stackexchange.com/questions/59658/use-of-tikzpicture-matrix-in-align-or-gather-environment/59660#comment126261_59660
\def\vertmathalign{-\the\dimexpr\fontdimen22\textfont2\relax}
\begin{document}
With circle vertical alignment:
 \begin{equation}
  x =
  \begin{tikzpicture}[baseline=\vertmathalign,remember picture]
   \node[circle,fill=red,minimum width=1cm] (first node) {}; 
  \end{tikzpicture}
  +
  \tikzmarkin[hf specifications vert align]{a}
  \begin{tikzpicture}[baseline=\vertmathalign,remember picture]
   \node[circle,fill=cyan,minimum width=1cm] (first node) {}; 
  \end{tikzpicture}
  +
  \begin{tikzpicture}[baseline=\vertmathalign,remember picture]
   \node[circle,fill=green,minimum width=1cm] (second node) {}; 
  \end{tikzpicture}
  \tikzmarkend{a}
 \end{equation}
 Without
 \begin{equation}
  x =
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=red,minimum width=1cm] (first node) {}; 
  \end{tikzpicture}
  +
  \tikzmarkin[hf specifications no vert align]{b}
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=cyan,minimum width=1cm] (first node) {}; 
  \end{tikzpicture}
  +
  \begin{tikzpicture}[remember picture]
   \node[circle,fill=green,minimum width=1cm] (second node) {}; 
  \end{tikzpicture}
  \tikzmarkend{b}
 \end{equation}
\end{document}

I've distinguished between the case in which circles are correctly aligned with the formula and not; the result is:

enter image description here