Alternative to using nested tikz in reference-able markers
Revised version after your feedback. This is an attempt of an answer but no guarantee that it works. Why should one not nest tikzpicture
s? The main reason is that the pgf keys of the ambient tikzpicture
will affect the shapes of the inner ones. One way to evade this is to create "new" shapes that merely differ from the standard ones by the pgf
keys they use. So I copied the ellipse
shape from pgflibraryshapes.geometric.code.tex
, dropped some pgf key and the inner sep sit now in the directory Rmano
such that you can still change it from outside if needed. This shape is used at the pgf level. There is no mediator like the TikZ \node
command which could obscure things. Unfortunately one has to work a bit harder to implement baseline
.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\makeatletter
\pgfdeclareshape{stubborn ellipse}
%
% Draws a circle around the text
%
{%
\savedanchor\centerpoint{%
\pgf@x=.5\wd\pgfnodeparttextbox%
\pgf@y=.5\ht\pgfnodeparttextbox%
\advance\pgf@y by-.5\dp\pgfnodeparttextbox%
}%
\savedanchor\radius{%
%
% Calculate ``height radius''
%
\pgfmathsetlength\pgf@yb{\pgfkeysvalueof{/Rmano/inner ysep}}%
\advance\pgf@y by\pgf@yb%
%
% Calculate ``width radius''
%
\pgf@x=.5\wd\pgfnodeparttextbox%
\pgfmathsetlength\pgf@xb{\pgfkeysvalueof{/Rmano/inner xsep}}%
\advance\pgf@x by\pgf@xb%
%
% Adjust
%
\pgf@x=1.4142136\pgf@x%
\pgf@y=1.4142136\pgf@y%
}%
%
% Anchors
%
\anchor{center}{\centerpoint}%
\anchor{mid}{\centerpoint\pgfmathsetlength\pgf@y{.5ex}}%
\anchor{base}{\centerpoint\pgf@y=0pt}%
\anchor{north}
{
\pgf@process{\radius}
\pgf@ya=\pgf@y%
\pgf@process{\centerpoint}
\advance\pgf@y by\pgf@ya
}%
\anchor{south}
{
\pgf@process{\radius}
\pgf@ya=\pgf@y%
\pgf@process{\centerpoint}
\advance\pgf@y by-\pgf@ya
}%
\anchor{west}
{
\pgf@process{\radius}
\pgf@xa=\pgf@x%
\pgf@process{\centerpoint}
\advance\pgf@x by-\pgf@xa
}%
\anchor{mid west}
{%
\pgf@process{\radius}
\pgf@xa=\pgf@x%
\pgf@process{\centerpoint}
\advance\pgf@x by-\pgf@xa%
\pgfmathsetlength\pgf@y{.5ex}
}%
\anchor{base west}
{%
\pgf@process{\radius}
\pgf@xa=\pgf@x%
\pgf@process{\centerpoint}
\advance\pgf@x by-\pgf@xa%
\pgf@y=0pt
}%
\anchor{north west}
{
\pgf@process{\radius}
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\pgf@process{\centerpoint}
\advance\pgf@x by-0.707107\pgf@xa
\advance\pgf@y by0.707107\pgf@ya
}%
\anchor{south west}
{
\pgf@process{\radius}
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\pgf@process{\centerpoint}
\advance\pgf@x by-0.707107\pgf@xa
\advance\pgf@y by-0.707107\pgf@ya
}%
\anchor{east}
{%
\pgf@process{\radius}
\pgf@xa=\pgf@x%
\pgf@process{\centerpoint}
\advance\pgf@x by\pgf@xa
}%
\anchor{mid east}
{%
\pgf@process{\radius}
\pgf@xa=\pgf@x%
\pgf@process{\centerpoint}
\advance\pgf@x by\pgf@xa%
\pgfmathsetlength\pgf@y{.5ex}
}%
\anchor{base east}
{%
\pgf@process{\radius}
\pgf@xa=\pgf@x%
\pgf@process{\centerpoint}
\advance\pgf@x by\pgf@xa%
\pgf@y=0pt
}%
\anchor{north east}
{
\pgf@process{\radius}
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\pgf@process{\centerpoint}
\advance\pgf@x by0.707107\pgf@xa
\advance\pgf@y by0.707107\pgf@ya
}%
\anchor{south east}
{
\pgf@process{\radius}
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\pgf@process{\centerpoint}
\advance\pgf@x by0.707107\pgf@xa
\advance\pgf@y by-0.707107\pgf@ya
}%
\anchorborder{
\edef\pgf@marshal{%
\noexpand\pgfpointborderellipse
{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}
{\noexpand\radius}%
}%
\pgf@marshal%
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
\centerpoint%
\advance\pgf@x by\pgf@xa%
\advance\pgf@y by\pgf@ya%
}%
%
% Background path
%
\backgroundpath
{
\pgf@process{\radius}%
\pgfutil@tempdima=\pgf@x%
\pgfutil@tempdimb=\pgf@y%
\advance\pgfutil@tempdima by-\pgf@xb%
\advance\pgfutil@tempdimb by-\pgf@yb%
\pgfpathellipse{\centerpoint}{\pgfqpoint{\pgfutil@tempdima}{0pt}}{\pgfqpoint{0pt}{\pgfutil@tempdimb}}%
}%
}%
\pgfkeys{/Rmano/inner ysep/.initial=2pt,/Rmano/inner xsep/.initial=2pt}
%
\newcommand*\circled[2][]{\setbox0\hbox{#1}%
\pgf@xa=\ht0%
\advance\pgf@xa by \pgfkeysvalueof{/Rmano/inner ysep}%
\pgf@ya=0.4142136\pgf@xa%
\raisebox{-\pgf@ya}{\textcolor{red}{\begin{pgfpicture}%
\pgfnode{stubborn ellipse}{center}{#2}{#1}{\pgfusepath{stroke}}%
\end{pgfpicture}}}}
\makeatother
\newcounter{cstepcnt}
\newcommand{\startcstep}{\setcounter{cstepcnt}{0}}
\newcommand{\cstep}{%
\refstepcounter{cstepcnt}%
\typeout{\number\value{cstepcnt}}%
\circled[cnode-\number\value{cstepcnt}]{\number\value{cstepcnt}}% this is pretty dangerous
}
\renewcommand{\thecstepcnt}{\textbf{\arabic{cstepcnt}:}}
\begin{document}
one text here \cstep{} some text here \cstep\label{c:one}.
\begin{tikzpicture}
\draw (0,0) node[draw](A){text\cstep\label{c:inner}};
\end{tikzpicture}
And then ref~\ref{c:one} and \ref{c:inner}.
\foreach \X in {1,...,12} {\cstep{}}.
\end{document}
(Looking at this \raisebox
hackery one can be really glad that TikZ comes with simple keys like baseline
which allow one to avoid all this mess. ;-)