适合作为多张图片中节点的背景

适合作为多张图片中节点的背景

我使用 TikZ 图片来表示方程中的项,我想通过在它们后面绘制一个矩形来直观地对其中一些项进行分组。但每个项都是一个单独的图片,这意味着我不能只在背景层上绘制矩形。有没有办法让矩形出现在方程后面,尽管我事先不知道在哪里绘制矩形?

以下示例展示了我自己能做到的最好事情,即让矩形出现在等式前面。我编辑了该问题上一版本的示例,使其更加真实,因为它会影响一些建议解决方案的可用性。(抱歉!我的 MWE 一开始太小了。)

错误的方法

代码:

\documentclass[b3paper]{baposter}
\usepackage{tikz,amsmath}
\usetikzlibrary{fit}

\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}
  +
  \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}
 \end{align}

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

这有点棘手,因为

  1. 我想要突出显示的图片在源中并不连续,并且
  2. 有一个现有的背景,我必须在上面画,所以这实际上是粘贴一张图片的问题之间文档的两个现有“层”。

答案1

一种可能性是使用安德鲁·史黛西的改进版本\tikzmark(参见他的回答tikzmark 在首次运行时会有不同的行为(并且标记位置尚不可用)); 在以下示例中,该\placemark命令负责放置标记;其第一个强制参数可用于选择当前边界框的锚点,第二个参数为标记命名。然后可以将名称用作宏的参数\DrawRect

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

在此处输入图片描述

该代码需要运行三次。

答案2

可能有更优雅的“纯 TikZ”解决方案,但您可以使用该包将矩形推送到背景atbegshi

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

答案3

您可以随时使用hf-tikz(因此需要运行两次编译):

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

结果:

在此处输入图片描述

当然这需要baposter.cls编译。


第一个版本

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

我区分了圆圈与公式正确对齐的情况和不对齐的情况;结果是:

在此处输入图片描述

相关内容