Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tikz scopes with coordinates relative to a node
% This file adds two environments for TkiZ.
%
% The first, tikzonnode, simply changes the coordinate system so that (0,0) is
% at the south west anchor of the given node and (1,1) is at the north east
% anchor.
%
% The second environment, tikzonimage, displays the given image file (the first
% optional argument is passed to \includegraphics) and creates a tikzpicture on
% top of it with coordinates relative to the image corners.
%
% Both environments accept an optional argument with TikZ options.
\documentclass{article}
\usepackage{tikz}
\makeatletter
\def\tikzonnode#1{%
\pgfutil@ifnextchar[{\tikzonnode@opt#1}{\tikzonnode@opt#1[]}%
}
\def\endtikzonnode{%
\end{scope}%
}
\def\tikzonnode@opt#1[#2]{%
\pgfpointanchor{#1}{south west}%
\pgfgetlastxy\tse@tikz@shift@x\tse@tikz@shift@y
\begin{scope}[
shift={(\tse@tikz@shift@x,\tse@tikz@shift@y)},
x={(#1.south east)},
y={(#1.north west)},
#2]%
}
\def\tikzonimage{%
\pgfutil@ifnextchar[{\tikzonimage@opt}{\tikzonimage@opt[]}%
}
\def\tikzonimage@opt[#1]#2{%
\begin{tikzpicture}
\node[inner sep=0] (image) {\includegraphics[#1]{#2}};
\begin{tikzonnode}{image}%
}
\def\endtikzonimage{%
\end{tikzonnode}
\end{tikzpicture}%
}
\makeatother
\begin{document}
\begin{tikzonimage}[width=0.3\textwidth]{some_image.jpg}[color=red]
\node at (0,0) {test};
\end{tikzonimage}
\begin{tikzpicture}
\node (A) [text width=2cm] {abc abc abc abc abc abc abc abc abc};
\begin{tikzonnode}{A}[color=red]
\node at (0,0) {test};
\end{tikzonnode}
\end{tikzpicture}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.