Source

whizzy-tikz / whizzy-tikz.sty

Full commit

% TODO: resize the whole picture to scale it -- easyish
% TODO: use a name/label somehow
% TODO: make it work for lines, rectangles etc.


%{{{ old \advinode code

\tikzset{
% This is the workhorse style
  advi/.style={
% We do the advi stuff after the node has been placed so that we can
% get access to its width and height.  What this means is that our
% node ends up being equivalent to
%  \node[at=(x-value,y-value)] {node text} [advi/set advi={x=,y=}];
    append after command={[advi/set advi={#1}]},
% As the advi boxes are specified by lower-left corner, we anchor our
% node at the lower-left so that the given coordinate is the node
% coordinate
    anchor=south west,
% The parameter #1 is of the form "x=<value>,y=<value>".  We trick
% TikZ into taking that as defining some keys in the "/tikz/advi"
% directory
    advi/.cd,
    #1,
% We use the values that have just been set, namely `/tikz/advi/x`
% and `/tikz/advi/y` to specify the location of the node via the
% `at` key.  We need to give the full path as we're currently in at
% `/tikz/advi`
    /tikz/at={(\pgfkeysvalueof{/tikz/advi/x},\pgfkeysvalueof{/tikz/advi/y})}
  },
% This next bit ensures that the `/tikz/advi/x` and `y` keys can be
% used to store values.
  advi/.cd,
  x/.initial=0,
  y/.initial=0,
% This is the part that specifies the boxes in the dvi.  This is
% actually executed after the node has been processed
  set advi/.code={
    \pgfpointxy{1}{1}
    \advixunit \pgf@x
    \adviyunit \pgf@y
% As the node has been processed, we can get its width and height by
% looking at a couple of anchors.
    \tikz@scan@one@point\pgfutil@firstofone(\tikzlastnode.north east)
    \pgf@xa=\pgf@x
    \pgf@ya=\pgf@y
    \tikz@scan@one@point\pgfutil@firstofone(\tikzlastnode.south west)
% We adjust the values to be multiples of `em`s as that's the default
% for advi/whizzytex
    \pgfmathsetmacro{\advi@node@w}{(\pgf@xa - \pgf@x)/\the\advi@edit@xunit}%
    \pgfmathsetmacro{\advi@node@h}{(\pgf@ya - \pgf@y)/\the\advi@edit@yunit}%
    \pgfmathsetmacro{\advi@node@x}{(\pgf@x)/\the\advi@edit@xunit}%
    \pgfmathsetmacro{\advi@node@y}{(\pgf@y)/\the\advi@edit@yunit}%

    \tikz@scan@one@point\pgfutil@firstofone(\tikzlastnode)

    \pgfmathsetmacro{\advi@node@xm}{(\pgf@x)/\the\advi@edit@xunit}%
    \pgfmathsetmacro{\advi@node@ym}{(\pgf@y)/\the\advi@edit@yunit}%
% Finally, we call the `\adviedit` command
    % \advi@special
    %     {edit\space
    %      comm="\string\advinode"\space
    %      name="really long name:\advi@node@x:\advi@node@y"\space
    %      line=\the\inputlineno\space
    %      file=\jobname\space
    %      xunit=1em\space
    %      yunit=1em\space
    %      x=\advi@node@x\space
    %      y=\advi@node@y\space
    %      w=\advi@node@w\space
    %      h=\advi@node@h\space
    %      d=0.0000\space
    %      }
    % \adviedit{comm=\advinode,w=\advi@node@w,h=\advi@node@h,x=\advi@node@x,y=\advi@node@y}{}
    \adviedit{comm=\advinode,w=\advi@node@w,h=\advi@node@h,#1}{}
  }
}

% This is the command that whizzytex will look for.
\newcommand{\advinode}[1]{%
  \node[advi={#1}]
}

%}}}

% Monkey patch TikZ to keep track of the initial input, so that we can have an exact match.
% Original code is found in:
% /usr/local/texlive/2011/texmf-dist/tex/generic/pgf/frontendlayer/tikz/tikz.code.tex
\def\tikz@scan@no@calculator#1(#2){%
  \edef\tikz@temp{(#2)}%
  \global\def\my@node@def{#2}%
  \expandafter\tikz@@scan@@no@calculator\expandafter#1\tikz@temp%
}

\tikzset{
% This is the workhorse style
  ADVI/.style={
% We do the advi stuff after the node has been placed so that we can
% get access to its width and height.
    append after command={[ADVI/set advi={#1}]},
    ADVI/.cd,
% For some reason setting the values of type, precision, and duplicate
% here don't work.  But I think they should...
    #1,
  },
% This next bit ensures that the `/tikz/advi/type` and `precision` keys can be
% used to store values.
  ADVI/.cd,
  type/.initial=node,
  precision/.initial=4,
  duplicate/.initial=no,
% This is the part that specifies the boxes in the dvi.  This is
% actually executed after the node has been processed
  set advi/.code={
    \let\this@nodes@def\my@node@def
    \pgfpointxy{1}{1}
    \advixunit \pgf@x
    \adviyunit \pgf@y
% As the node has been processed, we can get its width and height by
% looking at a couple of anchors.
    \tikz@scan@one@point\pgfutil@firstofone(\tikzlastnode.north east)
    \pgf@xa=\pgf@x
    \pgf@ya=\pgf@y
    \tikz@scan@one@point\pgfutil@firstofone(\tikzlastnode.south west)
% We adjust the values to be multiples of `em`s as that's the default
% for advi/whizzytex
    \pgfmathsetmacro{\advi@node@w}{(\pgf@xa - \pgf@x)/\the\advi@edit@xunit}%
    \pgfmathsetmacro{\advi@node@h}{(\pgf@ya - \pgf@y)/\the\advi@edit@yunit}%
    \pgfmathsetmacro{\advi@node@x}{(\pgf@x)/\the\advi@edit@xunit}%
    \pgfmathsetmacro{\advi@node@y}{(\pgf@y)/\the\advi@edit@yunit}%
    %
    \tikz@scan@one@point\pgfutil@firstofone(\tikzlastnode)
    \pgfmathsetmacro{\advi@node@xm}{(\pgf@x)/\the\advi@edit@xunit}%
    \pgfmathsetmacro{\advi@node@ym}{(\pgf@y)/\the\advi@edit@yunit}%
% TODO: Why do I have to do this?  Why didn't it take before?
    \tikzset{/tikz/ADVI/.cd,#1}
% Finally, we call the `\adviedit` command
    \adviedit[tikz:\pgfkeysvalueof{/tikz/ADVI/type}:%
    \pgfkeysvalueof{/tikz/ADVI/precision}:\pgfkeysvalueof{/tikz/ADVI/duplicate}%
    @\this@nodes@def @\advi@node@x @\advi@node@y @\advi@node@xm @\advi@node@ym]%
    {comm=\advinode,w=\advi@node@w,h=\advi@node@h,x=\advi@node@x,y=\advi@node@y,line=\the\inputlineno}{}
  }
}

% Local Variables:
% TeX-master: "test"
% End: