% \iffalse meta-comment
%<*internal>
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
  \expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
Copyright (C) 2012 by Claudio Fiandrino
-------------------------------------------

This file may be distributed and/or modified under the
conditions of the LaTeX Project Public License, either version 1.3
of this license or (at
 your option) any later version.
The latest version of this license is in:

   http://www.latex-project.org/lppl.txt

and version 1.3 or later is part of all distributions of LaTeX
version 2005/12/01 or later.

This work is "maintained" (as per LPPL maintenance status) by
Claudio Fiandrino.

This work consists of the file  hf-tikz.dtx
and the derived files           hf-tikz.pdf and
                                hf-tikz.sty.
\endpreamble
\generate{\file{\jobname.sty}{\from{\jobname.dtx}{package}}}
%</install>
%<install>\endbatchfile
%<*internal>
\ifx\fmtname\nameofplainTeX
  \expandafter\endbatchfile
\else
  \expandafter\endgroup
\fi
%</internal>
%<*package>
%<hf-tikz>\NeedsTeXFormat{LaTeX2e}
%<hf-tikz>\ProvidesPackage{hf-tikz}
%<hf-tikz> [2012/08/17 v0.1 A simple way to highlight formulas and formula parts.]
%</package>
%<*driver>
\documentclass{ltxdoc}
\usepackage[T1]{fontenc}
\usepackage{csquotes}
\usepackage{lmodern}
\usepackage{amsmath,amssymb}
\usepackage[customcolors,shade]{\jobname}
\usetikzlibrary{calc}
\usepackage{hypdoc}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
% \GetFileInfo{hf-tikz.sty}
%
% \title{The \textsf{hf-tikz} package\thanks{This document
% corresponds to \textsf{hf-tikz}~\fileversion,
% dated \filedate.}}
% \author{Claudio Fiandrino\thanks{e-mail: \texttt{claudio dot fiandrino at gmail dot com}}}
% \maketitle
% \begin{abstract}
% This package provides a way to \emph{highlight} formulas in both documents and presentations thanks to Ti\emph{k}Z.
% The idea cames out from \href{http://tex.stackexchange.com/questions/52598/beamer-highlighting-aligned-math-with-overlay}{this question} on \href{http://tex.stackexchange.com}{TeX.StackExchange}
% and it is based on the \texttt{tikzmark} macro from \href{http://tex.stackexchange.com/users/86/andrew-stacey}{Andrew Stacey} and 
%\href{http://tex.stackexchange.com/users/4301/peter-grill}{Peter Grill}.
%
% \end{abstract}
% \tableofcontents
%
% \section{Introduction and requirements}
% The aim of the package is to provide a simple way to highlight formulas. This is not the first package that tries to accomplish this task, but, rather than \href{http://www.ctan.org/pkg/empheq}{empheq}, \textsf{hf-tikz} provides also a way to highlight formulas overlay-aware inside a presentation, not only in standard documents. Moreover, in contrast with \href{http://www.ctan.org/pkg/empheq}{empheq}, \textsf{hf-tikz} even allows to highlight just a part of an equation.
% 
% The package uses Ti\emph{k}Z and it is based on the \texttt{tikzmark} macro from \href{http://tex.stackexchange.com/users/86/andrew-stacey}{Andrew Stacey} and 
%\href{http://tex.stackexchange.com/users/4301/peter-grill}{Peter Grill} (see as reference \href{http://tex.stackexchange.com/questions/51582/background-coloring-with-overlay-specification-in-algorithm2e-beamer-package}{this answer} and \href{http://tex.stackexchange.com/questions/50015/tikzmark-to-have-different-behaviour-if-first-run-and-mark-locations-not-yet-av}{this question}): among the numerous versions present on \href{http://tex.stackexchange.com}{TeX.SX}, the reference one implemented is taken from \href{http://tex.stackexchange.com/questions/57537/issues-and-potentiality-of-the-tikzmark-macro-dynamic-box-adaptation#57655}{this answer}. Indeed, as explained later, the concept of \emph{extendible markers} helps in customizing the box dimension. 
%
% \textsf{hf-tikz} loads:
% \begin{itemize}
% \item Ti\emph{k}Z and the library \texttt{shadings};
% \item \textsf{xparse};
% \item \textsf{etoolbox}.
% \end{itemize}
%
%
% \section{The usage}
% \subsection{The basic commands}
% Formulas can be highlighted by means of the insertion of delimiters before and
% after the part to be highlighted. Two compilation runs are always necessary: the first one to compute the position of the markers (also called delimiters) and the second one to place the box.
% \DescribeMacro{\tikzmarkin}^^A
% The starting delimiter should be introduced with the \cs{tikzmarkin} macro: it has a different syntax upon 
% being in \texttt{beamer} mode or not as it will be pointed out in subsection \ref{subsec:beamer}.
%
% \DescribeMacro{\tikzmarkend}^^A
% The end delimiter should be introduced by means of the \cs{tikzmarkend} macro: despite \cs{tikzmarkin},
% this macro keeps the same syntax also in \texttt{beamer} mode.
%
% An example of the basic usage is:
% \begin{verbatim}
% \[x+\tikzmarkin{a}y\tikzmarkend{a}=400\]
% \end{verbatim}
% which produces:
% \[x+\tikzmarkin{a}y\tikzmarkend{a}=400\]
%
% Notice that delimiter labels, also called \texttt{marker-id}s, should characterize \emph{uniquely} the part highlighted therefore reuse the same label name twice will lead to undesired results. Along this documentation there are examples showing how it is possible to give label names.
%
% In presence of fractions, sums, integrals and other operators, the standard command is not suitable. Consider the following example:
% \begin{verbatim}
% \[\tikzmarkin{a-1}x+\dfrac{z}{y}=400\tikzmarkend{a-1}\]
% \end{verbatim}
% It leads to:
% \[\tikzmarkin{a-1}x+\dfrac{z}{y}=400\tikzmarkend{a-1}\]
% In this case, the user must specify manually which are the \emph{shift-offsets} that delimits the box:
% \begin{verbatim}
% \begin{equation}
% \tikzmarkin{right delim frac}(0.1,-0.4)(-0.1,0.5)
% x+\dfrac{z}{y}=400
% \tikzmarkend{right delim frac}
% \end{equation}
% \end{verbatim}
% and this fixes the problem:
% \begin{equation}
% \tikzmarkin{right delim frac}(0.1,-0.4)(-0.1,0.5)
% x+\dfrac{z}{y}=400
% \tikzmarkend{right delim frac}
% \end{equation}
%
% The \emph{shift-offsets} should be introduced following this syntax:
% \begin{verbatim}
% \tikzmarkin{marker-id}(below right offset)(above left offset)
% \end{verbatim}
% 
% Here is an image that explains the differences between the default setting and the \emph{shift-offsets} manually inserted for the previous example:
% \bigskip
% \begin{center}
% \tikzmarkin{r}(0.1,-0.4)(-0.1,0.5)\tikz[baseline=-0.5ex]\node[opacity=0]{\ensuremath{x+\dfrac{z}{y}=400}};\tikzmarkend{r}
% $ 
% \qquad
% x+\dfrac{z}{y}=400
% \qquad
% $
% \tikzmarkin{f}\tikz[baseline=-0.5ex]\node[opacity=0]{\ensuremath{x+\dfrac{z}{y}=400}};\tikzmarkend{f}
% \end{center}
% \begin{tikzpicture}[remember picture,overlay]
% \foreach \coord in {pic cs:r,pic cs:f}
% \draw plot[mark=x]  coordinates{(\coord)} node[left]{\scriptsize{\texttt{\coord}}};
%
% \draw plot[mark=x]  coordinates{($(pic cs:r)+(0.1,-0.4)$)} node[below]{\scriptsize{\texttt{(0.1,-0.4)}}};   
% \draw plot[mark=x]  coordinates{(r)} node[above]{\scriptsize{\texttt{(-0.1,0.5)}}};     
%
% \draw plot[mark=x]  coordinates{($(pic cs:f)+(0.1,-0.18)$)} node[below]{\scriptsize{\texttt{default setting}}};   
% \draw plot[mark=x]  coordinates{(f)} node[above]{\scriptsize{\texttt{default setting}}};   
% \end{tikzpicture}
%
% Manual shifts allow to customize dimensions on the base of user's needs: they should be introduced inside round braces as coordinate points. Coordinates, indeed, introduce more degree of freedom from the user's point of view while other solutions are more restrictive. Markers, therefore, are \emph{extensible}. Notice that is is not possible to use markers separately, but they should be declared in pair.
%
% \subsection{An advanced example}
% This example shows how to insert an annotation aligned with a sentence: it requires the \texttt{calc} library from Ti\emph{k}Z. The way in which colors are set is explained in subsection \ref{subsec:customcolors}.
% \hfsetfillcolor{red!10}
% \hfsetbordercolor{red}
% \begin{equation*}
% \left.\begin{array}{cc}
%  -2\cdot \tikzmarkin{col}(0.05,-0.3)(-0.05,0.4)2=& -4 \\
%  -2\cdot 1=& -2 \\
%  -2\cdot 0\tikzmarkend{col}=& 0
% \end{array}\right\} \text{\small Product increases by 2 each time.}
% \end{equation*}

% \begin{tikzpicture}[remember picture,overlay]
%  \coordinate (col-aa) at ($(col)+(1.825,-1.8)$); 
%  \node[align=left,right] at (col-aa) {\small{Annotation}};
%  \path[-latex,red,draw] (col-aa) -| ($(col)+(0.14,-1.55)$);
% \end{tikzpicture}
% \linebreak
%
% \subsubsection*{The code}
% \begin{verbatim}
% \begin{equation*}
% \left.\begin{array}{cc}
%  -2\cdot \tikzmarkin{col}(0.05,-0.3)(-0.05,0.4)2=& -4 \\
%  -2\cdot 1=& -2 \\
%  -2\cdot 0\tikzmarkend{col}=& 0
% \end{array}\right\} \text{\small Product increases by 2 each time.}
% \end{equation*}
%
% % To insert the annotation
% \begin{tikzpicture}[remember picture,overlay]
%  % adjust the shift from "col" to move the position of the annotation 
%  \coordinate (col-aa) at ($(col)+(1.825,-1.8)$); 
%  \node[align=left,right] at (col-aa) {\small{Annotation}};
%  \path[-latex,red,draw] (col-aa) -| ($(col)+(0.14,-1.55)$);
% \end{tikzpicture}
% \end{verbatim}
%
% The message here is that, when something is highlighted, the \texttt{marker-id } could be used to subsequently add elements on the image, i.e. the annotation. 
%
% \section{The options}
% \subsection{The \texttt{beamer} mode}\label{subsec:beamer}
% \DescribeMacro{beamer}^^A
%  The call:
% \begin{verbatim}
% \usepackage[beamer]{hf-tikz}
% \end{verbatim}
% let the package to enter in \texttt{beamer} mode and the \cs{tikzmarkin} macro is \emph{overlay-aware}, that is overlay specifications could be introduced as first argument. For example:
% \begin{verbatim}
% \begin{align}
% \tikzmarkin<1->{a1}a_i\tikzmarkend{a1} + b_j = 10 \\
% \tikzmarkin<3>{c}c_j + d_j + 
% \tikzmarkin<2>{b}a_i\tikzmarkend{b} 
% >= 30\tikzmarkend{c}
% \end{align}
% \end{verbatim}
%
% \subsection{Customize colors}\label{subsec:customcolors}
% \DescribeMacro{customcolors}^^A
% This option allows you to customize both the fill and the background color. When using this option, two commands become available:
% \begin{itemize}
% \item \cs{hfsetfillcolor}
% \item \cs{hfsetbordercolor}
% \end{itemize}
% They can be use in whatever part of the document allowing an high color customization. For example:
% \begin{verbatim}
% \hfsetfillcolor{red!10}
% \hfsetbordercolor{red}
% \[
% \tikzmarkin{a}(0.2,-0.4)(-0.2,0.6)
% \dfrac{100}{x}
% \tikzmarkend{a}
% \]
% \end{verbatim}
% produces:
% \hfsetfillcolor{red!10}
% \hfsetbordercolor{red}
% \[\tikzmarkin{z}(0.2,-0.4)(-0.2,0.6)\dfrac{100}{x}\tikzmarkend{z}\]
% Then:
% \begin{verbatim}
% \hfsetfillcolor{blue!10}
% \hfsetbordercolor{blue}
% \[\tikzmarkin{z1}x+y=400\tikzmarkend{z1}\]
% \end{verbatim}
% gives:
% \hfsetfillcolor{blue!10}
% \hfsetbordercolor{blue}
% \[\tikzmarkin{z1}x+y=400\tikzmarkend{z1}\]
%
% \subsection{Using shadings}
% \DescribeMacro{shade}^^A
% The option \verb!shade! activates the possibility of introducing shaded backgrounds besides the fill color currently set. Available shadings are:
% \begin{itemize}
% \item vertical shading;
% \item horizontal shading;
% \item radial shading.
% \end{itemize}
%
% \subsubsection*{Example with vertical shading}
% Code:
% \begin{verbatim}
% \[\tikzmarkin[top color=white, bottom color=blue!20]{vshade}x+y=400\tikzmarkend{vshade}\]
% \end{verbatim}
% Result:
% \[\tikzmarkin[top color=white, bottom color=blue!20]{vshade}x+y=400\tikzmarkend{vshade}\]
% 
% \subsubsection*{Example with horizontal shading}
% Code:
% \begin{verbatim}
% \[\tikzmarkin[left color=white, right color=blue!20]{hoshade}x+y=400\tikzmarkend{hoshade}\]
% \end{verbatim}
% Result:
% \[\tikzmarkin[left color=white, right color=blue!20]{hoshade}x+y=400\tikzmarkend{hoshade}\]
%
% % \subsubsection*{Example with radial shading}
% Code:
% \begin{verbatim}
% \[\tikzmarkin[outer color=white, inner color=blue!20]{rshade}x+y=400\tikzmarkend{rshade}\]
% \end{verbatim}
% Result:
% \[\tikzmarkin[outer color=white, inner color=blue!20]{rshade}x+y=400\tikzmarkend{rshade}\]
%
% \subsection{Avoid the background color}
% \DescribeMacro{nofill}^^A
% Using the \texttt{nofill} option allows to simply not introduce the background color. 
% When the option is active, you can not change this behaviour inside the document. Another option to remove the background color, is to set the fill color by means of \cs{hfsetfillcolor} with the same color of the page.
%
% \StopEventually{\PrintChanges}
% \section{Implementation}
% \iffalse
% <*hf-tikz>
% \fi
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{hf-tikz}[2012/08/17 v0.1 A simple way to highlight formulas and formula parts.]
\RequirePackage{tikz}
\usetikzlibrary{shadings}
\RequirePackage{xparse}
\RequirePackage{etoolbox}
%    \end{macrocode}
% \subsection{Options definition}
% In this subsection the definitions of pre-defined colors and options are shown.
%    \begin{macrocode}
%% Colors

% Pre-defined colors
\definecolor{fancybrown}{RGB}{255,216,197}
\definecolor{fancyviolet}{RGB}{197,122,197}

\newcommand{\fcol}{fancybrown}
\newcommand{\bcol}{fancyviolet}

%% Package option

\newbool{fill}
\booltrue{fill}
\DeclareOption{nofill}{\boolfalse{fill}}

\DeclareOption{customcolors}{
\def\hfsetfillcolor#1{\renewcommand{\fcol}{#1}}
\def\hfsetbordercolor#1{\renewcommand{\bcol}{#1}}
}

\newbool{shade}
\boolfalse{shade}
\DeclareOption{shade}{\booltrue{shade}}

\newbool{beamer}
\boolfalse{beamer}
\DeclareOption{beamer}{\booltrue{beamer}}

\ProcessOptions
%    \end{macrocode}
% \subsection{General settings}
% In this subsection the general settings that allow the highlighing are shown. 
%    \begin{macrocode}
%% Settings

\ifbool{beamer}{%true
   \newcounter{jumping}
   \resetcounteronoverlays{jumping}

   \def\jump@setbb#1#2#3{%
     \@ifundefined{jump@#1@maxbb}{%
       \expandafter\gdef\csname jump@#1@maxbb\endcsname{#3}%
     }{%
       \csname jump@#1@maxbb\endcsname
       \pgf@xa=\pgf@x
       \pgf@ya=\pgf@y
       #3
       \pgfmathsetlength\pgf@x{max(\pgf@x,\pgf@xa)}%
       \pgfmathsetlength\pgf@y{max(\pgf@y,\pgf@ya)}%
       \expandafter\xdef\csname jump@#1@maxbb\endcsname{\noexpand\pgfpoint{\the\pgf@x}{\the\pgf@y}}%
     }
     \@ifundefined{jump@#1@minbb}{%
       \expandafter\gdef\csname jump@#1@minbb\endcsname{#2}%
     }{%
       \csname jump@#1@minbb\endcsname
       \pgf@xa=\pgf@x
       \pgf@ya=\pgf@y
       #2
       \pgfmathsetlength\pgf@x{min(\pgf@x,\pgf@xa)}%
       \pgfmathsetlength\pgf@y{min(\pgf@y,\pgf@ya)}%
       \expandafter\xdef\csname jump@#1@minbb\endcsname{\noexpand\pgfpoint{\the\pgf@x}{\the\pgf@y}}%
     }
   }

   \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}%
       }
     },
     onslide/.code args={<#1>#2}{%
       \only<#1>{\pgfkeysalso{#2}}%
     },
     alt/.code args={<#1>#2#3}{%
       \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}%
     },
     stop jumping/.style={
       execute at end picture={%
         \stepcounter{jumping}%
         \immediate\write\pgfutil@auxout{%
           \noexpand\jump@setbb{\the\value{jumping}}{\noexpand\pgfpoint{\the\pgf@picminx}{\the\pgf@picminy}}{\noexpand\pgfpoint{\the\pgf@picmaxx}{\the\pgf@picmaxy}}
         },
         \csname jump@\the\value{jumping}@maxbb\endcsname
         \path (\the\pgf@x,\the\pgf@y);
         \csname jump@\the\value{jumping}@minbb\endcsname
         \path (\the\pgf@x,\the\pgf@y);
       },
     }
   }
}{% false
   \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,\pgfpointorigin\@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
  }%
}
%    \end{macrocode}
% \subsection{The highlighting commands}
% In this subsection the definition of the highlighing commands in \texttt{beamer} mode and not are shown.
%    \begin{macrocode}
\ifbool{beamer}{%true
   \ifbool{fill}{%true-fill
      \ifbool{shade}{%true-shade
         \NewDocumentCommand{\tikzmarkin}{r<> d[] m D(){0.1,-0.18} D(){-0.1,0.35}}{%
         \IfNoValueTF{#2}{%true-val
            \only<#1>{\tikz[remember picture,overlay]
            \draw[line width=1pt,rectangle,rounded corners,fill=\fcol,draw=\bcol]
            (pic cs:#3) ++(#4) rectangle (#5) node [anchor=base] (#3){}
            ;}
         }{%false-val
            \only<#1>{\tikz[remember picture,overlay]
            \draw[line width=1pt,rectangle,rounded corners,fill=\fcol,#2,draw=\bcol]
            (pic cs:#3) ++(#4) rectangle (#5) node [anchor=base] (#3){}
            ;}}         
         }
      }{%false-shade
         \NewDocumentCommand{\tikzmarkin}{r<> m D(){0.1,-0.18} D(){-0.1,0.35}}{%
         \only<#1>{\tikz[remember picture,overlay]
         \draw[line width=1pt,rectangle,rounded corners,fill=\fcol,draw=\bcol]
         (pic cs:#2) ++(#3) rectangle (#4) node [anchor=base] (#2){}
         ;}}
      }
   }{%false-fill
      \NewDocumentCommand{\tikzmarkin}{r<> m D(){0.075,-0.18} D(){-0.075,0.35}}{%
      \only<#1>{\tikz[remember picture,overlay]
      \draw[line width=1pt,rectangle,rounded corners,draw=\bcol]
      (pic cs:#2) ++(#3) rectangle (#4) node [anchor=base] (#2){}
      ;}}
   }
}{%false-beamer
   \ifbool{fill}{%true-fill
      \ifbool{shade}{%true-shade
         \NewDocumentCommand{\tikzmarkin}{d[] m D(){0.1,-0.18} D(){-0.1,0.35}}{%
         \IfNoValueTF{#1}{%true-val
            \tikz[remember picture,overlay]
            \draw[line width=1pt,rectangle,rounded corners,fill=\fcol,draw=\bcol]
            (pic cs:#2) ++(#3) rectangle (#4) node [anchor=base] (#2){}
            ;
         }{%false-val
         \tikz[remember picture,overlay]
         \draw[line width=1pt,rectangle,rounded corners,fill=\fcol,#1,draw=\bcol]
         (pic cs:#2) ++(#3) rectangle (#4) node [anchor=base] (#2){}
         ;}}
      }{%false-shade
         \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=\fcol,draw=\bcol]
         (pic cs:#1) ++(#2) rectangle (#3) node [anchor=base] (#1){}
         ;}
      }
   }{%false-fill
      \NewDocumentCommand{\tikzmarkin}{m D(){0.075,-0.18} D(){-0.075,0.35}}{%
      \tikz[remember picture,overlay]
      \draw[line width=1pt,rectangle,rounded corners,draw=\bcol]
      (pic cs:#1) ++(#2) rectangle (#3) node [anchor=base] (#1){}
      ;}
   }
}

\newcommand\tikzmarkend[2][]{%
\tikz[remember picture with id=#2] #1;}
%    \end{macrocode}
%
% \iffalse
% </hf-tikz>
% \fi
%\Finale
\endinput
