tcolorbox 框架 + TikZ 图片 + marginpar

tcolorbox 框架 + TikZ 图片 + marginpar

对另一个问题的答复用户 leandriis 给出了一个代码,该代码用一个矩形包围定理环境,矩形的一角有一张 TikZ 图片。以下内容几乎逐字逐句地复制自 leandriis 的回答,但在结尾处添加了一条额外的注释。

\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}

该代码编译成功(我使用LuaLaTeX),并且排版如下:

定理环境被一个矩形包围,矩形的左上角有一个 TikZ 图片

如果我现在从最后一条注释中删除该%字符,以便该\marginpar命令对 TeX 引擎可见,并重新编译代码,则编译会失败,并且日志文件中会出现以下错误消息:

! 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.

怎样才能修正这个问题,使代码排版如下:

矩形和边注


注意:

我使用这个是\marginpar为了简单,但我实际上感兴趣的是坎帕的\marginstuff命令。希望对所提问题的回答能够帮助我解决\marginstuff出现类似Not in outer par mode错误消息的问题。

答案1

方案一:加载包marginnote并使用\marginnote{Hello, world!}

米

方案二:加载\tcbuselibrary{documentation} 并使用 \tcbdocmarginnote{Hello, world!}。(它加载marginote

d

\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}

相关内容