# tcolorbox frame + TikZ picture + marginpar

In a reply to another question user leandriis gave code that surrounds a theorem environment with a rectangle that has a TikZ picture in a corner. The following is copied almost verbatim from leandriis' answer with the exception of an additional comment that has been added near the end.

\documentclass{scrartcl}

\newtheorem{theorem}{Theorem}
\usepackage{lipsum}

\usepackage[most]{tcolorbox}
\usepackage{tikz}

% Definition of \mydangersymbol taken from: https://tex.stackexchange.com/a/604048/134144
\newcommand{\mydangersymbol}[1]{%
\begin{tikzpicture}[baseline=(x.base)]
\draw[rounded corners=.01em] (-.05em,-1.3em)rectangle(.05em,.9em);
\draw[fill=white,rounded corners=1] (0,.8em)--(.8em,0)--(0,-.8em)--(-.8em,0)--cycle;
\draw[very thick,line cap=round](-.3em,-1.3em)--(.3em,-1.3em);
\node(x) at (0,0em) {\normalfont\sffamily\small#1};
\end{tikzpicture}%
}

\NewTColorBox{mydangerenv}{+O{}}{%
enhanced,
sharp corners,
colback=white,
coltitle=black,
title={\mydangersymbol{#1}},
attach boxed title to top left ={yshift=-\tcboxedtitleheight/2-4pt,
yshifttext=-\tcboxedtitleheight/2-4pt,
xshift=-\tcboxedtitlewidth/2+0.25mm},
boxed title style={colback=white,
colframe=white,
sharp corners,
boxsep=0pt,
boxrule=0pt,
bottom=3pt,
halign title=flush center},
boxrule=0.5mm,
top=-\tcboxedtitleheight/2+5pt,
boxsep=5pt,
}

\begin{document}

\begin{mydangerenv}[A]
\begin{theorem}
%\marginpar{Hello, world!}
\lipsum[1]
\end{theorem}
\end{mydangerenv}

\end{document}


This code compiles successfully (I use LuaLaTeX), and typesets as follows:

If I now delete the % character from the last comment, so that the \marginpar command is visible to the TeX engine, and recompile the code, the compilation fails, and the following error message appears in the log file:

! LaTeX Error: Not in outer par mode.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
...

l.47 \marginpar
{Hello, world!}
?
! Emergency stop.
...

l.47 \marginpar
{Hello, world!}
You've lost some text.  Try typing  <return>  to proceed.
If that doesn't work, type  X <return>  to quit.


How can this problem be corrected, so that the code typesets as follows:

NB

I used \marginpar for simplicity, but I'm actually interested in campa's \marginstuff command. Hopefully the answer to the question as posed will help me solve the problem with \marginstuff, which gives a similar Not in outer par mode error message.

• I do not think that \marginpar can work inside more or less anything. You can move it before or after the colorbox, though. You can create "callouts" by hand like in tex.stackexchange.com/questions/295903/… if you want something more controllable. Jul 12 at 13:08

Alternative I: load the package marginnote and use \marginnote{Hello, world!}

Alternative II: Load \tcbuselibrary{documentation} and use \tcbdocmarginnote{Hello, world!}. (It loads marginote)

\documentclass{scrartcl}

\newtheorem{theorem}{Theorem}
\usepackage{lipsum}

\usepackage[most]{tcolorbox}

\tcbuselibrary{documentation} % to use \tcbdocmarginnote{..}, loads marginnote <<<<
%\usepackage{marginnote} %to use \marginnote{...} <<<<

\usepackage{tikz}

% Definition of \mydangersymbol taken from: https://tex.stackexchange.com/a/604048/134144
\newcommand{\mydangersymbol}[1]{%
\begin{tikzpicture}[baseline=(x.base)]
\draw[rounded corners=.01em] (-.05em,-1.3em)rectangle(.05em,.9em);
\draw[fill=white,rounded corners=1] (0,.8em)--(.8em,0)--(0,-.8em)--(-.8em,0)--cycle;
\draw[very thick,line cap=round](-.3em,-1.3em)--(.3em,-1.3em);
\node(x) at (0,0em) {\normalfont\sffamily\small#1};
\end{tikzpicture}%
}

\NewTColorBox{mydangerenv}{+O{}}{%
enhanced,
sharp corners,
colback=white,
coltitle=black,
title={\mydangersymbol{#1}},
attach boxed title to top left ={yshift=-\tcboxedtitleheight/2-4pt,
yshifttext=-\tcboxedtitleheight/2-4pt,
xshift=-\tcboxedtitlewidth/2+0.25mm},
boxed title style={colback=white,
colframe=white,
sharp corners,
boxsep=0pt,
boxrule=0pt,
bottom=3pt,
halign title=flush center},
boxrule=0.5mm,
top=-\tcboxedtitleheight/2+5pt,
boxsep=5pt,
}

\begin{document}

\begin{mydangerenv}[A]
\begin{theorem}
\tcbdocmarginnote{Hello, world!}    % <<<<
%       \marginnote{Hello, world!}% <<<<
\lipsum[1]
\end{theorem}
\end{mydangerenv}

\end{document}

• Thanks. I tested the marginnote solution you suggested in Alternative I. The code compiled successfully, but no margin note was produced in the typeset output. Jul 13 at 7:11
• Never mind, I've figured it out: the code needs to be compiled twice. Jul 13 at 7:18