4

Let's consider the example I used in

Tikz: node in margin right to other node interacts with tcbtheorem

that is a command that uses TikZ to draw a colored box around a marginal note.

As this note is drawn by TikZ it is not recognized as marginal content, and can overlap for example with sidenotes.

For instance:

\documentclass[oneside]{book}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{tikzpagenodes}
\usepackage[most]{tcolorbox}
\tcbuselibrary{theorems}
\usepackage{lipsum}
\usepackage{marginfix}
\usepackage{sidenotes}


\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, even a longer text}};
\end{tikzpicture}
}


\begin{document}


Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum
ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero,
nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla
ultrices. Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in,
pretium quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean
faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor
semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu, accumsan eleifend,
sagittis quis, diam. Duis eget orci sit amet orci dignissim rutrum.
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi auctor lorem
non justo. Nam\sidenote{Random sidenote with a bit long text.} lacus libero, pretium at, lobortis vitae, ultricies et, tellus. Donec aliquet,
tortor sed accumsan bibendum, erat ligula aliquet magna, vitae ornare odio metus a mi.\caution{}
Morbi ac orci et nisl hendrerit mollis. Suspendisse ut massa. Cras nec ante. Pellentesque a
nulla. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus
mus. Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursus
luctus mauris.



\end{document}

yields:

enter image description here

To avoid such behaviour I would like to "block" the height of the caution box in the margin so that nothing else can go there. The package marginfix package provides the \marginphantom command that seems to be meant for what I want. However, adding \marginphantom{2cm} in the caution definition just before the tikzpicture (or inside the \tikzmark definition) does not work.

Do you think of another solution for blocking in the margin the necessary space for my cautionbox, so that it does not overlap with other margin material (I am thinking to \marginpar or \sidenote). (Furthermore, I would be very grateful if anyone can explain what's wrong with my proposed use of \marginphantom).

Edit

A clarification: the command \tikzmark is defined and used here because I want the box to be at the exact place where caution is called.

Furthermore, the box I need is more complicated than just a red box, so I need TikZ to plot it, I cannot use something like colorbox.

  • So, you want the \sidenote to be moved in the case of overlapings with your caution-box, and not the other way around? – Ruben Aug 3 '15 at 12:37
  • Yes, that's exactly it. – mvienney Aug 3 '15 at 12:38
  • The main problem is that the \sidenote is created first. – John Kormylo Aug 3 '15 at 15:53
5

The easiest way to avoid overlapings is to wrap the tikzpicture itself into a \sidenote inside the definition of \caution as

\sidenote{%
  \tikzpicture[remember picture]
    \node[draw=red,anchor=west,xshift=\marginparsep,yshift=0pt]   
    (mybox\themycaution)
    at ([yshift=3pt]current page text area.east|-\themycaution) 
    {\parbox{\marginparwidth}{Some text, even a longer text}};
  \endtikzpicture
}

Note that the 'overlay' option has to be canceled out in this case.

Now, you have to disable the numbering of those sidenotes and inhibit the increment of the sidenote counter:

\newcommand{\caution}{%
  ...
  \begingroup
  \let\thesidenote\relax
  \let\refstepcounter\@gobble
  \sidenote{%
    ...
  }
  \endgroup
}

output

Complete code

\documentclass[oneside]{book}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{tikzpagenodes}
\usepackage[most]{tcolorbox}
\tcbuselibrary{theorems}
\usepackage{lipsum}
\usepackage{sidenotes}
\usepackage{marginfix}

\newcounter{mycaution}
\newcommand\tikzmark[1]{%
  \tikz[remember picture,overlay]
  \node[inner xsep=0pt,outer sep=0pt] (#1) {};
}

\makeatletter
\newcommand{\caution}{%
  \stepcounter{mycaution}%
  \tikzmark{\themycaution}%
  \begingroup
  \let\thesidenote\relax
  \let\refstepcounter\@gobble
  \sidenote{%
    \tikzpicture[remember picture]
      \node[draw=red,anchor=west,xshift=\marginparsep,yshift=0pt]   
      (mybox\themycaution)
      at ([yshift=3pt]current page text area.east|-\themycaution) 
      {\parbox{\marginparwidth}{Some text, even a longer text}};
    \endtikzpicture
  }%
  \endgroup
}
\makeatother

\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum
ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero,
nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla
ultrices. Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in,
pretium quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean
faucibus.\caution Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor
semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu, accumsan eleifend,
sagittis quis, diam. Duis eget orci sit amet orci dignissim rutrum.
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi auctor lorem
non justo. Nam\sidenote{Random sidenote with a bit long long long long long long long long long long long long text.} lacus libero, pretium at, lobortis vitae, ultricies et, tellus. Donec aliquet,
tortor sed accumsan bibendum, erat ligula aliquet magna, vitae ornare odio metus a mi.\caution
Morbi ac orci et nisl hendrerit mollis. Suspendisse ut massa. Cras nec ante. Pellentesque a
nulla. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus
mus. Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursus
luctus mauris.
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.