3

I have an equation in a tikz environment and I would like to be able to highlight part of it. For example the following:

\documentclass[12pt]{article}
\usepackage{amsmath,amssymb,latexsym}
\usepackage{tikz}


\begin{document}


\tikzstyle{block} = [draw,fill=blue!60,minimum width=1.1 em, minimum height= 1em, rounded corners= 4pt]

\begin{figure}[h]
\centering
  \begin{tikzpicture}
    \coordinate  (Eq1) at (1,2);
    \coordinate  (Eq2) at (1,1);

    \node[block] at (1,2) (block1) {};
    \node at (Eq1) {$A =  B + C$};
    \node at (Eq2) {$D = E + F$};

    \node[block] at (0, -0.5) (block2) {Blah};
    \draw[->] (block1) -- (block2);

\end{tikzpicture}
\end{figure}
\end{document}

However, it is a huge pain to by hand play around with the node placement of the blocks to highlight the corresponding part of the equation. I'd like a way to specify the node. Attempting to do such, I played around with \tikz{\node} within the equation but to no avail as follows

\documentclass[12pt]{article}

\usepackage{amsmath,amssymb,latexsym}
\usepackage{tikz}


\begin{document}


\tikzstyle{block} = [draw,fill=blue!60,minimum width=1.1 em, minimum height= 1em, rounded corners= 4pt]

\begin{figure}[h]
\centering
  \begin{tikzpicture}
    \coordinate  (Eq1) at (1,2);
    \coordinate  (Eq2) at (1,1);

%\node[block] at (1,2) (block1) {};
    \node at (Eq1) {$A =  \tikz{ \node[fill=blue!60, rounded corners = 4pt, minimum size = 1 em]
        (block1) {$B$};} + C$};
    \node at (Eq2) {$D = E + F$};

    \node[block] at (0, -0.5) (block2) {Blah};
    \draw[->] (block1) -- (block2);

\end{tikzpicture}
\end{figure}
\end{document}

The B is no longer aligned with the other parts of the equation and the arrow is stuck in the second block. Can you suggest a good way to go about doing this? Thanks!

EDIT Based on the first response (Thanks by the way!) I want to add that at this point I'd like to still do this all within a tikz picture. For my paired down example below I know it make senses to just do \begin\end{equation} but for my actual application I'm hoping to have a lot more going on and just this one equation arbitrarily placed in a bigger diagram. Maybe this can be done still using the usual latex equation commands but it seems more natural to approach having it within a tike picture

  • 1
    Looks like you could use the infamous \tikzmark, or at the very least, add remember picture to your tikzpicture environment. See tex.stackexchange.com/a/40333/18228 – Herr K. Feb 23 '15 at 22:47
  • 1
    Never load latexsym along with amssymb. – egreg Feb 23 '15 at 22:58
  • 1
    You can also use hf-tikz package to highlight parts of equations, arrays, ... It uses tikzmark and offers easily customizable boxes. – Ignasi Feb 24 '15 at 8:51
3

The following answer builds on your second attempt, and uses a \tikzmark-like command to achieve the annotation effect.

Code

\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage{tikz}

\newcommand\mytikzmark[3][]{%
  \tikz[remember picture,baseline=(#2.base)]{\node(#2)[outer sep=0pt,#1]{#3};}%
}

\begin{document}

\tikzset{block/.style={draw,fill=blue!60,minimum width=1.1 em, minimum height= 1em, rounded corners= 4pt}}

\begin{figure}[h]
\centering
  \begin{tikzpicture}[remember picture]
    \draw[help lines](-2,-1)grid(3,3); % shows background

    \coordinate  (Eq1) at (1,2);
    \coordinate  (Eq2) at (1,1);

    \node at (Eq1) {$A = \mytikzmark[block]{block1}{$B$}+ C$};
    \node at (Eq2) {$D = E + F$};

    \node[block] at (0, -0.5) (block2) {Blah};
    \draw[->] (block1) -- (block2);
\end{tikzpicture}
\end{figure}

I can also refer to \verb+(block1)+ outside of the figure like \mytikzmark[block]{block3}{this}.
\tikz[remember picture,overlay]{\draw[->](block3)to[bend right](block1);}
\end{document}

Output

enter image description here


By the way, it's recommended that you use \tikzset instead of the deprecated \tikzstyle.

  • (+1) But the tikzmark library is handy.... – cfr Feb 24 '15 at 2:30
  • 1
    @cfr: Thanks. I have to confess that I'm not very familiar with the tikzmark library, as I don't use it on a regular basis. In this case, however, the \subnode command from the library doesn't allow styling such as fill. So I decided to create my own variant :p – Herr K. Feb 24 '15 at 2:37
  • Ah. That makes sense. I've not used the \subnode facility - only the \tikzmark which I think uses coordinates, and wrappers thereof. – cfr Feb 24 '15 at 2:43
  • Thanks, I will play round with this today. I think this is what I am looking for! – Fractal20 Feb 24 '15 at 14:30
  • Awesome, I got to try it out and this is exactly what I was hoping for! For alignment purposes (i.e. that the two equations weren't left justified above) I found setting the same anchor helped, i.e. [anchor = west] on both. Is there any reason I shouldn't use that in combination with everything else? Thanks so much – Fractal20 Feb 24 '15 at 16:28
3
  1. Don't use nodes to manually align stuff that LaTeX already knows how to align. Put equations into align environments. It's ok to put tikzpictures into equations. You don't need them to be in a figure.
  2. As Herr K. points out, use overlay and remember picture. You can read about these options in pgfmanual.pdf, section 17.13.1.

Here is my solution:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{tikz}


\begin{document}


\tikzset{
    block/.style = {draw,fill=blue!60,rounded corners= 4pt}
}

\begin{align}
    A &= \tikz[remember picture,baseline=(B.base)] \node[block] (B) at (0,0) {B}; + C\\
    D &= E + F
\end{align}


\begin{tikzpicture}[overlay,remember picture]
    \node[block] (Blah) at (0,0) {Blah};
    \draw (B) -- (Blah);
\end{tikzpicture}
\end{document}

enter image description here

  • Is there a way to do this within a tikz picture? For example, say I wanted this original set of equations in an arbitrary place (not centered as equation will do). I'd still like to have the freedom to be able to specify the location of the equations using a node or something similar. Thanks, this is certainly already helpful! – Fractal20 Feb 23 '15 at 23:28
  • @Fractal20 I wouldn't know how to do that. It doesn't seem a good idea and try to put the align inside of a \node. The only thing that comes to my mind is using matrices. Look at the question linked in Herr K.'s answer, and at section 57 in the pgfmanual. It would help if you could say how arbitrary you need to place them. For example if it's just a matter of having 2 or 3 sets of equations next to each other, that's something align can still manage. – Turion Feb 23 '15 at 23:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.