I am currently trying to use @GonzaloMedina's answer to create a framed environment for a margin note
Consider the following simplified code:
\documentclass[oneside]{book}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{tikzpagenodes}
\usepackage{lipsum}
\newcounter{mycaution}
\newcommand\tikzmark[1]{%
\tikz[remember picture,overlay]
\node[inner xsep=0pt,outer sep=0pt] (#1) {};
}
\newcommand{\caution}{
\stepcounter{mycaution}
\tikzmark{\themycaution}%
\begin{tikzpicture}[remember picture,overlay]
\node[draw=red,anchor=west,xshift=\marginparsep,yshift=0pt]
(mybox\themycaution)
at ([yshift=3pt]current page text area.east|-\themycaution)
{\parbox{\marginparwidth}{Some text}};
\end{tikzpicture}
}
\begin{document}
\lipsum[1]
Some random text\caution{}
\lipsum[2-5]
\end{document}
The command \caution does what I expect: it produces a red box in the right margin, right to the text before the call of \caution.
Now, assume I want to do the same thing, but that \caution is called inside a tcbtheorem environnment. For example:
\documentclass[oneside]{book}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{tikzpagenodes}
\usepackage[most]{tcolorbox}
\tcbuselibrary{theorems}
\usepackage{lipsum}
\newcounter{mycaution}
\newcommand\tikzmark[1]{%
\tikz[remember picture,overlay]
\node[inner xsep=0pt,outer sep=0pt] (#1) {};
}
\newcommand{\caution}{
\stepcounter{mycaution}
\tikzmark{\themycaution}%
\begin{tikzpicture}[remember picture,overlay]
\node[draw=red,anchor=west,xshift=\marginparsep,yshift=0pt]
(mybox\themycaution)
at ([yshift=3pt]current page text area.east|-\themycaution)
{\parbox{\marginparwidth}{Some text}};
\end{tikzpicture}
}
\newtcbtheorem{theo}{Theorem}{theorem style=plain}{th}
\begin{document}
\begin{theo}{}{}
Some random text\caution{}
\lipsum[1]
\end{theo}
\lipsum[1]
Some random text\caution{}
\end{document}
Then the output is the following:
I am not used to TikZ, but what I understand is that:
- the effect of
tikzmarkis to place a node a the exact place where it is called, and the coordinates of this node can be used later, - the
remember picture, overlayoption oftikzpictureallow to use these coordinates.
However, a tcolorbox (and hence a tcbtheorem) is drawn using TikZ, so that these coordinates are kind of lost (except that the problem only comes from the x-coordinate, the y-coordinate seems to be good).
It seems that tikzmark gives a name (the value of mycaution) to the node it creates, but that we do not really use this name later, assuming that TikZ has not been used between the call of \tikzmark and the drawing of the box. However, I have no idea about how I can use this name to specify that my box should be in the margin right to this node...
Can anyone provide a solution (or better: an explanation of what really happens here)?



corrected page text areaevery time I am about to usecautioninside ofthm, or do you think of a way to do this automatically ? – mvienney Jul 29 '15 at 18:14correct current .... Seems quite obvious, but still...). However, I still think that you're some kind of TikZ sorcerer :-) – mvienney Jul 29 '15 at 21:58