Home > tikz pgf > Issues and potentiality of the tikzmark macro: dynamic box adaptation

Issues and potentiality of the tikzmark macro: dynamic box adaptation

May 27Hits:1
Advertisement

Since I discovered the tikzmark macro I've been enthusiastic about it.

I think that it is possible to distinguish at least two versions of the command:

  • a basic one (two examples: http://tex.stackexchange.com/a/35719/13304 and tikzmark to have different behaviour if first run (and mark locations not yet available))
  • an improved version (two examples: http://tex.stackexchange.com/a/50054/13304 and Highlight a column in equation or math environment)

One answer in which both versions are used is http://tex.stackexchange.com/a/51590/13304.

Assume to use now the improved version. For what concern Highlight a column in equation or math environment the basic definition is:

\newcommand{\tikzmarkin}[1]{%       \tikz[remember picture with id=#1]       \draw[line width=1pt,rectangle,rounded corners,fill=\fillcol]       (pic cs:#1) ++(0.065,-0.15) rectangle (-0.05,0.32)       ;} 

while in http://tex.stackexchange.com/a/51590/13304:

\newcommand<>{\boxto}[1]{% \only#2{\tikz[remember picture,overlay] \draw[myframe,line width=1pt,fill=mybrown,,rectangle,rounded corners] (pic cs:#1) ++(1.4,-.25) rectangle (-.2,0.4) ;}% } 

They have in common one thing, the fact that the rectangle is defined a priori. In the following example I will point out that this could be not convenient when used multiple times:

\documentclass{article} \usepackage{amsmath} \usepackage{tikz}  % to change colors \newcommand{\fillcol}{white} \newcommand{\bordercol}{red}  %% code by Andrew Stacey  % http://tex.stackexchange.com/questions/51582/background-coloring-with-overlay-specification-in-algorithm2e-beamer-package#51582  \makeatletter \tikzset{%      remember picture with id/.style={%        remember picture,        overlay,        draw=\bordercol,        save picture id=#1,      },      save picture id/.code={%        \edef\[email protected]{#1}%        \immediate\write\[email protected]{%          \noexpand\savepointas{\[email protected]}{\pgfpictureid}}%      },      if picture id/.code args={#1#2#3}{%        \@[email protected]@#1}{%          \pgfkeysalso{#3}%        }{          \pgfkeysalso{#2}%        }      }    }     \def\savepointas#1#2{%   \expandafter\gdef\csname [email protected]@#1\endcsname{#2}% }  \def\[email protected]#1,#2\@nil{%   \def\[email protected]{#1}%   \def\[email protected]{#2}% }  \tikzdeclarecoordinatesystem{pic}{%   \[email protected]@,{#1}%   \[email protected]@%     \[email protected]#1\@nil   \else     \[email protected]#1,(0pt,0pt)\@nil   \fi   \@[email protected]@\[email protected]}{%     \[email protected]@[email protected]\[email protected]\[email protected]   }{%   \[email protected]{\csname [email protected]@\[email protected]\endcsname}\[email protected]@pic%   \[email protected]{\pgfpictureid}\[email protected]@pic%   \[email protected]{\pgfpointorigin\[email protected]@pic}%   \[email protected]=\[email protected]   \[email protected]=\[email protected]   \[email protected]{\pgfpointorigin\[email protected]@pic}%   \advance\[email protected] by -\[email protected]   \advance\[email protected] by -\[email protected]   }% } \makeatother  \newcommand{\tikzmarkin}[1]{%       \tikz[remember picture,overlay]       \draw[line width=1pt,rectangle,rounded corners,fill=\fillcol, draw=\bordercol]       (pic cs:#1) ++(0.065,-0.42) rectangle (-0.05,0.52) node [anchor=base] (#1){}       ;}   \newcommand\tikzmarkend[2][]{% \tikz[remember picture with id=#2] #1;}   \begin{document} \begin{align*} &x+\tikzmarkin{a}\dfrac{z}{y}\tikzmarkend{a}=100 \hspace{1cm} \text{perfect alignment}\\ &x+\tikzmarkin{z}y\tikzmarkend{z}=100 \hspace{1cm} \text{bad behaviour} \end{align*}  \end{document} 

This leads to:

Issues and potentiality of the tikzmark macro: dynamic box adaptation

This is due to the fact that the rectangle has been defined to be suitable for the fraction and not for a single term.

To improve the method proposed based on the \tikzmark, my question is: it is possible to make those dimensions dynamic?

I mean not only for a math case, but also for simple text. For example:

\begin{document}  \begin{itemize} \item this is a text to be \tikzmarkin{a}highlighted\tikzmarkend{a} \item \tiny{this is a text to be \tikzmarkin{b}highlighted\tikzmarkend{b}} \end{itemize}  \end{document} 

where the tikzmark has been defined as:

\newcommand{\tikzmarkin}[1]{%       \tikz[remember picture,overlay]       \draw[line width=1pt,rectangle,rounded corners,fill=\fillcol, draw=\bordercol]       (pic cs:#1) ++(0.065,-0.16) rectangle (-0.05,0.32) node [anchor=base] (#1){}       ;} 

leads to:

Issues and potentiality of the tikzmark macro: dynamic box adaptation

but when the text is \tiny, the rectangle dimensions could have been reduced a bit.

Answers

While I adore the power of the \tikzmark concept, too, it seems (with the necessity to compile twice) to be overkill for this situation. Why not just box the content and measure its size?

The following implements this idea based on some code I originally developed for this answer to a question about highlighting elements in a lstlisting environment while also keeping the syntax highlighting. The result is the \btHL command, which works like a font-changing command (such as \color or \bfseries) in that it affects everything until the end of the group (not across line breaks); this was a requirement for playing together with listings. The basic idea is to box the content and then typeset it inside a TikZ node. The bounding box of the tikzpicture, however, is adjusted to the size of the content, so that the highlighting does not take extra space (to prevent "jumping content" if used with beamer overlays).

As a quick solution, I have implemented your \tikzhighlight macro on this base; the code, however, could be simplified quite a bit if the content to highlight is always given as a macro parameter.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usepackage{amsmath}

\makeatletter
\newenvironment{btHighlight}[1][]
{\begingroup\[email protected]@par/.style={#1}}\begin{lrbox}{\@tempboxa}}
{\end{lrbox}\[email protected]@box[[email protected]@par]{\@tempboxa}\endgroup}

\newcommand\btHL[1][]{%
  \begin{btHighlight}[#1]\bgroup\aftergroup\[email protected]@endenv%
}
\def\[email protected]@endenv{%
  \end{btHighlight}%
  \egroup
}
\newcommand{\[email protected]@box}[2][]{%
  \tikz[#1]{%
    \pgfpathrectangle{\pgfpoint{0pt}{0pt}}{\pgfpoint{\wd #2}{\ht #2}}%
    \pgfusepath{use as bounding box}%
    \node[anchor=base west, fill=orange!30,outer sep=0pt,inner xsep=0.2em, inner ysep=0.1em,  #1]{\usebox{#2}};
  }%
}
\makeatother

\newcommand{\tikzhighlight}[2][red]{%
  {\btHL[fill=#1!10,draw=#1,rounded corners]#2}%
}

\begin{document}

\begin{itemize}
\item this is \tikzhighlight[yellow]{a text to be highlighted}
\item {\tiny{this is a text to be \tikzhighlight{highlighted}}}
\item \huge{this is a text to be \tikzhighlight{highlighted}}
\end{itemize}

\begin{align*}
&\tikzhighlight[green]{\ensuremath{x+\dfrac{z}{y}}}=100\\
&x+\tikzhighlight[blue]{\ensuremath{y}}=100
\end{align*}

\end{document}

Some additional fine tuning could be applied to the dimensioning of the boxes and the bounding box.

Issues and potentiality of the tikzmark macro: dynamic box adaptation

Thanks to Peter Grill suggestion I developed a reasonable solution based on:

  • Andrew's code to save the position of the picture;
  • Peter's code to compute the height and the width of the box.

Here is my MWE:

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

%% code by Andrew Stacey
% http://tex.stackexchange.com/questions/51582/background-coloring-with-overlay-specification-in-algorithm2e-beamer-package#51582

\makeatletter
\tikzset{%
     remember picture with id/.style={%
       remember picture,
       overlay,
       save picture id=#1,
     },
     save picture id/.code={%
       \edef\[email protected]{#1}%
       \immediate\write\[email protected]{%
         \noexpand\savepointas{\[email protected]}{\pgfpictureid}}%
     },
     if picture id/.code args={#1#2#3}{%
       \@[email protected]@#1}{%
         \pgfkeysalso{#3}%
       }{
         \pgfkeysalso{#2}%
       }
     }
   }

   \def\savepointas#1#2{%
  \expandafter\gdef\csname [email protected]@#1\endcsname{#2}%
}

\def\[email protected]#1,#2\@nil{%
  \def\[email protected]{#1}%
  \def\[email protected]{#2}%
}

\tikzdeclarecoordinatesystem{pic}{%
  \[email protected]@,{#1}%
  \[email protected]@%
    \[email protected]#1\@nil
  \else
    \[email protected]#1,(0pt,0pt)\@nil
  \fi
  \@[email protected]@\[email protected]}{%
    \[email protected]@[email protected]\[email protected]\[email protected]
  }{%
  \[email protected]{\csname [email protected]@\[email protected]\endcsname}\[email protected]@pic%
  \[email protected]{\pgfpictureid}\[email protected]@pic%
  \[email protected]{\pgfpointorigin\[email protected]@pic}%
  \[email protected]=\[email protected]
  \[email protected]=\[email protected]
  \[email protected]{\pgfpointorigin\[email protected]@pic}%
  \advance\[email protected] by -\[email protected]
  \advance\[email protected] by -\[email protected]
  }%
}

% code by Peter Grill <modified>
%http://tex.stackexchange.com/questions/35319/a-boxed-alternative-with-minimal-spacing/35357#35357

\newcommand*{\@DrawBoxHeightSep}{0.1em}%
\newcommand*{\@DrawBoxDepthSep}{0.08em}%
\newcommand{\@DrawBox}[6][red]{%#1= style, #2=height, #3=depth, #4 left marker, #5 right marker, #6 text
    \tikz[overlay,remember picture,baseline]
    \draw[fill=#1!10,draw=#1,rounded corners]
      ($(pic cs:#4)+(-0.2em,#2+\@DrawBoxHeightSep)$) rectangle
      ($(pic cs:#5)+(0.2em,-#3-+\@DrawBoxDepthSep)$);
      \tikz[overlay,remember picture,baseline]\node[anchor=base] at ($(pic cs:#4)!0.5!(pic cs:#5)$) {#6};
}

\newcommand\tikzmark[2][]{%
\tikz[remember picture with id=#2] #1;}

\newcounter{image}
\setcounter{image}{1}
\newdimen\@myBoxHeight%
\newdimen\@myBoxDepth%
\newcommand{\tikzhighlight}[2][red]{%
    \settoheight{\@myBoxHeight}{#2}% Record height of box
    \settodepth{\@myBoxDepth}{#2}% Record height of box
    \tikzmark{l\theimage}#2\tikzmark{r\theimage}\@DrawBox[#1]{\@myBoxHeight}{\@myBoxDepth}{l\theimage}{r\theimage}{#2}% Draw the box
    \stepcounter{image}
}
\makeatother

\begin{document}

\begin{itemize}
\item this is \tikzhighlight[yellow]{a text to be highlighted}
\item {\tiny{this is a text to be \tikzhighlight{highlighted}}}
\item \huge{this is a text to be \tikzhighlight{highlighted}}
\end{itemize}

\begin{align*}
&\tikzhighlight[green]{\ensuremath{x+\dfrac{z}{y}}}=100\\
&x+\tikzhighlight[blue]{\ensuremath{y}}=100
\end{align*}

\end{document}

which gives:

Issues and potentiality of the tikzmark macro: dynamic box adaptation

One remark: in math mode \ensuremath inside \tikzhighlight is fundamental to avoid errors.

Solution with extensible markers

The previous solution had, as a drawback, the necessity of inserting the content to be highlighted inside the command.

Another approach is to make the markers extendible in some sense, that is allowing the user to shift them to better fit the area to be highlighted.

This could be achieved declaring something like

\NewDocumentCommand{\tikzmarkin}{m D(){0.1,-0.18} D(){-0.1,0.35}}{%
          \tikz[remember picture,overlay]
          \draw[line width=1pt,rectangle,rounded corners,fill=\fillcol,draw=\bordercol]
          (pic cs:#1) ++(#2) rectangle (#3)
          ;}

What are #2 and #3? If we consider:

\tikzmarkin{a}x+\dfrac{z}{y}=400\tikzmarkend{a}

#2 is the end marker while #3 is the in marker. Now the user can change the dimension of the box as he wants since it's not computed automatically: that's the cost of not having things inside the command.

Let's make a complete example:

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{calc}

% to change colors
\newcommand{\fillcol}{blue!20}
\newcommand{\bordercol}{blue}

\newcommand{\setfillcolor}[1]{\renewcommand{\fillcol}{#1}}
\newcommand{\setbordercolor}[1]{\renewcommand{\bordercol}{#1}}

%% code by Andrew Stacey
% http://tex.stackexchange.com/questions/51582/background-coloring-with-overlay-specification-in-algorithm2e-beamer-package#51582

\makeatletter
\tikzset{%
     remember picture with id/.style={%
       remember picture,
       overlay,
       save picture id=#1,
     },
     save picture id/.code={%
       \edef\[email protected]{#1}%
       \immediate\write\[email protected]{%
         \noexpand\savepointas{\[email protected]}{\pgfpictureid}}%
     },
     if picture id/.code args={#1#2#3}{%
       \@[email protected]@#1}{%
         \pgfkeysalso{#3}%
       }{
         \pgfkeysalso{#2}%
       }
     }
   }

   \def\savepointas#1#2{%
  \expandafter\gdef\csname [email protected]@#1\endcsname{#2}%
}

\def\[email protected]#1,#2\@nil{%
  \def\[email protected]{#1}%
  \def\[email protected]{#2}%
}

\tikzdeclarecoordinatesystem{pic}{%
  \[email protected]@,{#1}%
  \[email protected]@%
    \[email protected]#1\@nil
  \else
    \[email protected]#1,(0pt,0pt)\@nil
  \fi
  \@[email protected]@\[email protected]}{%
    \[email protected]@[email protected]\[email protected]\[email protected]
  }{%
  \[email protected]{\csname [email protected]@\[email protected]\endcsname}\[email protected]@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
  }%
}

\NewDocumentCommand{\tikzmarkin}{m D(){0.1,-0.18} D(){-0.1,0.35}}{%
      \tikz[remember picture,overlay]
      \draw[line width=1pt,rectangle,rounded corners,fill=\fillcol,draw=\bordercol]
      (pic cs:#1) ++(#2) rectangle (#3)
      ;}

\newcommand\tikzmarkend[2][]{%
\tikz[remember picture with id=#2] #1;}

\begin{document}
 \begin{align}
      \tikzmarkin{a1}(0.05,-0.1)(-0.05,0.23)a_i\tikzmarkend{a1} + b_j = 10 \\
     \tikzmarkin{c}c_j + d_j + a_i >= 30 \tikzmarkend{c}
 \end{align}

\[
    \tikzmarkin{a}x+\dfrac{z}{y}=400\tikzmarkend{a} \qquad\qquad
    \tikzmarkin{a2}(0.1,-0.4)(-0.1,0.5)x+\dfrac{z}{y}=400\tikzmarkend{a2}
\]
\vspace{1cm}
A integral:
\setfillcolor{red!10}
\setbordercolor{red}
\begin{equation}V(z)=\sum_{\kappa=0}^{\infty}{\; \tikzmarkin{k}(0.1,-0.7)(-0.1,0.8) \int_{0}^{\infty}{\dfrac{\mathrm{e}^{-\lambda x}\cdot (\lambda x)^\kappa}{\kappa!} \, b(x)\,\mathrm{d} x}\cdot z^\kappa \tikzmarkend{k}}\end{equation}

\vspace{1cm}
\setfillcolor{green!10}
\setbordercolor{green!50!black}
 \begin{equation*}
    \left.\begin{array}{cc}
      -2\cdot \tikzmarkin{r}2=& -4 \\
      -2\cdot 1=& -2 \\
      -2\cdot 0\tikzmarkend{r}=& 0
    \end{array}\right\}
    \qquad\qquad
    \left.\begin{array}{cc}
      -2\cdot \tikzmarkin{r1}(0.035,-0.2)(-0.035,0.35)2=& -4 \\
      -2\cdot 1=& -2 \\
      -2\cdot 0\tikzmarkend{r1}=& 0
    \end{array}\right\}
\end{equation*}
\end{document}

The result:

Issues and potentiality of the tikzmark macro: dynamic box adaptation

Related Articles

  • Issues and potentiality of the tikzmark macro: dynamic box adaptation

    Issues and potentiality of the tikzmark macro: dynamic box adaptationMay 27

    Since I discovered the tikzmark macro I've been enthusiastic about it. I think that it is possible to distinguish at least two versions of the command: a basic one (two examples: http://tex.stackexchange.com/a/35719/13304 and tikzmark to have differe

  • Alignments by means of the tikzmark macro

    Alignments by means of the tikzmark macroMay 27

    This is a follow up of my previous question Issues and potentiality of the tikzmark macro: dynamic box adaptation. I thought should have been better to ask two questions although they are related to the same argument because problems are different. I

  • My \Render that attempts to iterate a list and invoke macros dynamically produces errors

    My \Render that attempts to iterate a list and invoke macros dynamically produces errorsDecember 7

    My \Render that attempts to iterate a list and invoke macros dynamically produces errors. The given MWE with detailed comments tells what my objective is. How to fix it? I have no more idea how to fix the following errors: MWE \documentclass{book} \u

  • Facing issue while invoking Document service in Microsoft Dynamic AX: mule esbJanuary 16

    I am facing issue while invoking document service in microsoft dynamic ax. I tried to invoke find method..But it gives me error NullPayload. find method has one parameter AifQueryCriteria type in Microsoft dynamic AX How can I pass this type of param

  • Potential problems from passing macro arguments to \verbOctober 15

    I've been working on a command that will typeset snippets of code within a paragraph. I'm aware of listings and company, but I want something that will just typeset the code verbatim and also allow me to do other things with it (like save to an exter

  • Expansion Issues When Passing TikZ Styles to Macro Created by Macro

    Expansion Issues When Passing TikZ Styles to Macro Created by MacroOctober 22

    I'm struggling (again) with expansions. This time I have a generic function \misdirection that creates a TikZ environment subject to a few options. Then, I create a macro \freezeStyle that creates a new macro that has frozen some of the options to \m

  • VBA macro dynamically created Scrollbar updates dynamically created TextboxJanuary 26

    How can a dynamically created Textbox be updated by a dynamically created Scrollbar using WithEvents handler? I have this userform code below and the Class Module thereafter. Option Explicit Dim TextArray() As New Class1 Dim ScrollArray() As New Clas

  • Word Macro - dynamically display textbox or text in certain letters according to a field valueFebruary 5

    I am using Word 2007. I have an Excel worksheet with field called "Branch". Using this worksheet to perform mail merge, the letter in MS Word will display a textbox with certain text if the branch="ABC", else it will not display the te

  • Expansion of macros and box contentsOctober 13

    I don't understand the following behavior. Intro First, here is a simplified version of a macro I am using (which is itself a hacked version of a macro found in gloss.tex). If you have a string MY|STRING with a delimiter in it (in this case, a pipe),

  • Issue with "Custom web view in File Dialog Box" in Office 2013August 16

    I have a custom "web view for file dialog" that used to work in Office 2010 and no longer works in the 2013 version. By "web view for file dialog box" I mean the feature described here: http://msdn.microsoft.com/en-us/library/ms947923.

  • Issue processing a servlet call after a confirm box call in JSPJanuary 21

    Still working on my mainEFT.jsp and I was trying to add a confirmation box that checks whether the user wants to not enter values in the command line and select all records. If they click "OK" then it should process all records. If they click &q

  • Networking issue in Windows 8.1 / 7 and Virtual Box

    Networking issue in Windows 8.1 / 7 and Virtual BoxJune 14

    I have a Windows 8.1 (x64) installed on my notebook PC, I've also installed a Windows 7 (x86) in Virtual Box which is installed in my Windows 8.1. in Windows 8.1 I can access a network that is not internet accessible but I can access Internet through

  • tikzmark shadow around box

    tikzmark shadow around boxNovember 29

    I have drawn a rounded box around my equations using this code : \usepackage[customcolors]{hf-tikz} \usepackage[framemethod=tikz]{mdframed} \hfsetbordercolor{green} \begin{equation} \tikzmarkin{g}(0.4,-0.5)(-0.2,0.75)r = \frac{n \sum xy - \sum x \sum

  • Beamer - Source code highlighting/annotation tips

    Beamer - Source code highlighting/annotation tipsJune 1

    How can I highlight and annotate source code in a beamer presentation like this one? For example, consider the following image from that .pdf file: It looks to me that above presentation is made with beamer... --------------Solutions------------- As

  • Highlight table cells using thick, colored border

    Highlight table cells using thick, colored borderAugust 15

    I'd like to highlight certain cells of my table. In particular, I'd like to set the border of these sells colored and thick. These cells should also have a background color. Here's a minimal example of my table: \documentclass{article} \usepackage{ar

  • Array cell highlighting tikz

    Array cell highlighting tikzNovember 19

    I'm beginning to write up a PhD thesis and am pretty new to LaTeX. I'm trying to explain a numerical method I'm using and in doing this I wish to highlight sections of arrays to aid explanation. The entries in the arrays I am highlighting vary in siz

  • multicols within colorbox

    multicols within colorboxNovember 22

    i'm trying to get a gray background for an entire multicols environment and get errors with this "minimal" working example: \documentclass[11pt,a4paper]{article} \usepackage[utf8]{inputenc} \usepackage{graphicx} \usepackage[table,dvipsnames]{xco

  • Multiple radiobutton selecion issue- dynamic radiobutton creationJanuary 23

    I am getting issue with radiobutton. I have created dynamic radiobuttons but i am able to multiple select.I need to check only one as per radiobutton functionality. Here is my code: List<String> answers = Constants_Variables.missionMainResponse.getR

  • Macro photography with an ultra-wide lens and extension tubes?

    Macro photography with an ultra-wide lens and extension tubes?June 28

    While reading some reviews of Canon EF-S 10-22mm f/3.5-4.5 USM, I was a bit surprised to notice that the lens is supposed to be compatible with 12mm and 25mm extension tubes, and with those one should be able to get maximum magnifications better than

  • How would I create Dynamic LOD with Quad TreesApril 21

    I would like to be able to create something like the following http://www.youtube.com/watch?v=LxZhWrSmrOY&feature=player_embedded how would I create something like it? I understand I will have to use quad trees and cube to sphere projection, but I do

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.942 s.