4

I use hf-tikz quite often in my beamer presentations. One thing that I find quite annoying each time that I use it is that, if the rectangle providing the highlighting happens to be over some text preceding the one enclosed by the \tizmarkin and \tikzmarkend commands, such text is hidden.

See, for instance, this MWE

\documentclass{beamer}
\usepackage{tikz}
\usepackage[beamer]{hf-tikz}    
\begin{document}
    \begin{frame}
        \begin{align*}
            \mathbf{\hat{f}}(\tikzmarkin<2>[above left offset={-1.1,0.35},below right offset={1.0,-1.35}]{myhl}t+R\tikzmarkend{myhl}) &=  a + b + c
        \end{align*}
    \end{frame}
\end{document}

yielding this (the misalignment is due to bad screenshots)

enter image description here

enter image description here

Is there a way to avoid this? Or (as I fear) is it part of the way hf-tikz works and cannot, thus, be somehow bypassed?

JamesT
  • 3,169
gbernardi
  • 845
  • 1
    it would be easier to see two images than an animated one (perhaps it's just me but I find the cycle rate of this particularly painful) – David Carlisle Nov 03 '16 at 10:54
  • @DavidCarlisle fair enough. I edited the post – gbernardi Nov 03 '16 at 12:36
  • 1
    @AndreyTyukin What are you hoping to find with this bounty? Nowadays, hf-tikz is not my first choice to highlight parts of a equation. The tikzmark package/library is very advanced and can deal with this much better. What are you trying to highlight actually? There's probably no need to manually adjust the offsets anymore. – Qrrbrbirlbel May 08 '23 at 15:30
  • Actually, I had completely forgotten about this and can't even recall that I set a bounty on this question. As a matter of fact, after all these years the question is completely irrelevant to me (I'm not even using LaTeX anymore :D :D). Regardless. good luck to whoever will get the bounty.

    Edit: Wait, I just realised that it seems like it wasn't me starting the bounty. OK, I was very confused with all these answers popping up years later, and didn't realised that bounties can't be active for so long :|

    – gbernardi May 09 '23 at 16:26
  • @gbernardi Sorry for the confusion. I just was googling around a few days ago, ran into this question. It seemed to describe exactly the problem that I was facing, and it already included example code and illustrations. Since there were no answers, but I wanted to see how to make it work, I started a bounty. Obligatory XKCD precisely describing the situation: Wisdom of the Ancients. So, yeah, uhm... Even though it comes 7 years too late: thanks for the effort on illustrations. – Andrey Tyukin May 09 '23 at 21:33
  • @AndreyTyukin: No problem at all. I'm actually happy to see that something obscure I wrote 8 years ago (and that probably gave me enough a headache to motivate me to write a stackoverflow question) can be helpful for someone else. Good luck! – gbernardi May 10 '23 at 07:47

3 Answers3

3

The hf-tikz is almost ten years old and doesn't offer much what you couldn't just do with tikzmark directly which is basically what I've done here but with the original interface of hf-tikz.

This is: mark the begin and the end of the to highlighted part but \tikzmarkdraw it as early as possible so that it gets drawn before \mathbf{\hat{f}} gets put onto the page but it needs to be on the same page. With beamer this is very easy because you know where the pagebreaks are.

You could just put all \tikzmarkdraws at the start of the frame.

Code

\documentclass{beamer}
\usepackage{tikz}
\usepackage[beamer]{hf-tikz}
\NewDocumentCommand{\tikzmarkbegin}{m}{\tikzmarkend{begin-#1}}
\NewDocumentCommand{\tikzmarkdraw}{D<>{.-} O{} m}{%
  \only<#1>{%
    \tikz[remember picture,overlay]\draw[hf/rect,#2]
        ([shift={(\aboveleftoff)}]pic cs:begin-#3) rectangle
%        node [anchor=base] (node-#3) {}
        ([shift={(\belowrightoff)}]pic cs:#3);%
  }%
}
\tikzset{hf/rect/.style={line width=+1pt, fill=\fcol, draw=\bcol, rounded corners}}
\begin{document}
\begin{frame}
\begin{align*}
\tikzmarkdraw<2>[above left offset={-1.1,0.35},below right offset={1.0,-1.35}]{myhl}
\mathbf{\hat{f}}(\tikzmarkbegin{myhl} t + R\tikzmarkend{myhl}) &=  a + b + c
\end{align*}
\end{frame}
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
2

It is not necessary to move the hf-tikz annotation to the background. You can change the blend mode instead:

\documentclass{beamer}
\usepackage{tikz}
\usepackage[beamer]{hf-tikz}    
\begin{document}
    \begin{frame}
        \begin{align*}
            \mathbf{\hat{f}}(\tikzmarkin<2>[above left offset={-1.1,0.35},below right offset={1.0,-1.35},blend mode=multiply]{myhl}t+R\tikzmarkend{myhl}) &=  a + b + c
        \end{align*}
    \end{frame}
\end{document}

enter image description here

  • That's an interesting approach, thank you! Do I understand correctly that it assumes black text on a white background? Could it lead to unexpected results if the background is not just white, e.g. in presentations where there is some background image? – Andrey Tyukin May 04 '23 at 15:54
  • 1
    @AndreyTyukin As long as the text/background colour is sufficiently different from the hf-tikz colour, it should nevertheless produce a readable result. For extremely different colour themes, e.g. white text on dark background, maybe one can experiment with some of the other available blend modes. – samcarter_is_at_topanswers.xyz May 04 '23 at 16:08
2

As you are showing the box only on the second overlay, you could use the tikzmark library and draw the background before the equation:

\documentclass{beamer}

\setbeamertemplate{background canvas}{\includegraphics[scale=2]{example-image-duck}}

\usepackage{tikz} \usetikzlibrary{tikzmark,fit} \begin{document}

\begin{frame} \begin{tikzpicture}[overlay, remember picture] \node<2> [draw=pink!50!violet,thick,fill=orange!30!white, rounded corners, inner xsep=7pt, fit=(myhl)] {}; \end{tikzpicture} \begin{align} \mathbf{\hat{f}}(\tikzmarknode{myhl}{t+R}) &= a + b + c \end{align} \pause \end{frame}

\end{document}

If you would like to show it on all overlays, you could add it to the background layer:

\documentclass{beamer}

\setbeamertemplate{background canvas}{\includegraphics[scale=2]{example-image-duck}}

\usepackage{tikz} \usetikzlibrary{tikzmark,fit} \begin{document}

{ \addtobeamertemplate{background canvas}{}{ \begin{tikzpicture}[overlay, remember picture] \node [draw=pink!50!violet,thick,fill=orange!30!white, rounded corners, inner xsep=7pt, fit=(myhl)] {}; \end{tikzpicture} } \begin{frame} \begin{align} \mathbf{\hat{f}}(\tikzmarknode{myhl}{t+R}) &= a + b + c \end{align} \end{frame} }

\end{document}

enter image description here