tcolorbox:如何引用盒子?

tcolorbox:如何引用盒子?

通过使用tcolorbox包,我定义了以下框,它们知道它们是在偶数页还是奇数页:

enter image description here

enter image description here

上述框的 mwe 为:

\documentclass{article}
\usepackage[many]{tcolorbox}
\usetikzlibrary{positioning}

\newcounter{task}
\newtcolorbox[use counter=task,
              number within=section,
              number freestyle={\thesection-\noexpand\arabic{\tcbcounter}},
              ]{vaja}%
   {enhanced jigsaw,
    breakable,
    toggle left and right,
    boxrule=0mm,
    colback=white, colframe=red,
    top=0mm,bottom=1mm,left=1mm,right=1mm,
    arc=0mm,
    borderline={0.5pt}{0pt}{red, sharp corners},
    fontupper=\normalsize\sffamily, fontlower=\normalsize\sffamily,
    rightrule=1mm,
        overlay={%
    \tcbifoddpage{\node[font=\large\sffamily, text=red, inner sep=0mm,
                        below right=0mm and \marginparsep] at (frame.north east) {Vaja
                        \thetcbcounter};}
                 {\node[font=\large\sffamily, text=red, inner sep=0mm,
                        below left=0mm and \marginparsep]  at (frame.north west) {Vaja
                    \thetcbcounter};}
        }%end of overlay
    }% end of task

\usepackage{lipsum}

\begin{document}
    \begin{vaja}\label{task:ce-1}% label hasn't any effect
\lipsum*[11]
    \end{vaja}
\newpage
    \begin{vaja}\label{task:ce-2}% label hasn't any effect
\lipsum[11]
see \ref{task:ce-1}
    \end{vaja}
see \ref{task:ce-1} and \ref{task:ce-2}% doesn't work
\end{document}

我最初并没有预料到参考文献,但现在我意识到这将是一个非常有用的选择。上面引用的简单方法不起作用......

  • 我应该如何改变“vaja”的定义,以便可以引用?

  • 我怀疑在盒子定义中我应该使用title,但如何插入它的定义以保留盒子设计?

答案1

您需要使用label=...选项来进行自定义tcolorbox,因此我在框中添加了一个可选参数vaja

如果cleveref使用,请crefname=...在 init 选项中说明以提供正确的cleveref参考名称。

解释:

tcolorbox有一个phantom=键 -- 的每个参数都phantom将添加到列表中并存储在\sbox宏中 -- 这\refstepcounter对于 的内部计数器的宏tcolorbox也是如此,这样重新定义的\@currentlabel就会丢失。任何\label相同键之外的phantom=...命令都不会获取正确的\@currentlabel

然而,该label=...选项\label直接放置\refstepcounter在幻影“组”之后→\@currentlabel被正确存储并且“幸存”。

\documentclass{article}
\usepackage[many]{tcolorbox}
\usetikzlibrary{positioning}

\newcounter{task}
\newtcolorbox[use counter=task,
              number within=section,
              number freestyle={\thesection-\noexpand\arabic{\tcbcounter}},
              ]{vaja}[1][]%
   {enhanced jigsaw,
    breakable,
    toggle left and right,
    boxrule=0mm,
    colback=white, colframe=red,
    top=0mm,bottom=1mm,left=1mm,right=1mm,
    arc=0mm,
    borderline={0.5pt}{0pt}{red, sharp corners},
    fontupper=\normalsize\sffamily, fontlower=\normalsize\sffamily,
    rightrule=1mm,
        overlay={%
    \tcbifoddpage{\node[font=\large\sffamily, text=red, inner sep=0mm,
                        below right=0mm and \marginparsep] at (frame.north east) {Vaja
                        \thetcbcounter};}
                 {\node[font=\large\sffamily, text=red, inner sep=0mm,
                        below left=0mm and \marginparsep]  at (frame.north west) {Vaja
                    \thetcbcounter};}
        },%end of overlay
        #1
    }% end of task

\usepackage{lipsum}

\begin{document}
    \begin{vaja}[label=task:ce-1]% label hasn't any effect
\lipsum*[11]
    \end{vaja}
\newpage
    \begin{vaja}[label={task:ce-2}]% label hasn't any effect
\lipsum[11]
see \ref{task:ce-1}
    \end{vaja}
see \ref{task:ce-1} and \ref{task:ce-2}% doesn't work
\end{document}

enter image description here

相关内容