如何在此示例 tcolorbox 中添加标签以供引用

如何在此示例 tcolorbox 中添加标签以供引用

我是 LaTeX 的新手,想添加标签来引用下面在 中完成的代码tcolorbox。此程序示例由 @ 提供贡萨洛·梅迪纳在他改进的版本中,tcbcounter包含了可选标题。

我需要一个用于示例和假设的标签选项。

实际上,在每个部分之后,我都会使用块示例和假设,因此在可选标题文本中添加当前部分标题的选项也会有很大帮助。

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

\definecolor{myblue}{RGB}{0,163,243}

\tcbset{mystyle/.style={
  breakable,
  enhanced,
  outer arc=0pt,
  arc=0pt,
  colframe=myblue,
  colback=myblue!20,
  attach boxed title to top left,
  boxed title style={
    colback=myblue,
    outer arc=0pt,
    arc=0pt,
    top=3pt,
    bottom=3pt,
    },
  fonttitle=\sffamily
  }
}

\newtcolorbox[auto counter,number within=section]{example}[1][]{
  mystyle,
  title=Example~\thetcbcounter,
  overlay unbroken and first={
      \path
        let
        \p1=(title.north east),
        \p2=(frame.north east)
        in
        node[anchor=west,font=\sffamily,color=myblue,text width=\x2-\x1] 
        at (title.east) {#1};
  }
}
\newtcolorbox[auto counter]{assumption}[1][]{
  mystyle,
  colback=white,
  rightrule=0pt,
  toprule=0pt,
  title=Assumption SLR.\thetcbcounter,
  overlay unbroken and first={
      \path
        let
        \p1=(title.north east),
        \p2=(frame.north east)
        in
        node[anchor=west,font=\sffamily,color=myblue,text width=\x2-\x1] 
        at (title.east) {#1};
  }
}

\begin{document}

\section{Test section}
\begin{example}
test
\end{example}
\begin{assumption}
test
\end{assumption}
\begin{example}[Optional title]
test
\end{example}
\begin{assumption}[Optional title with some more words for the example so it spans two lines]
test
\end{assumption}

\end{document}

答案1

您可以为标签添加参数

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

\definecolor{myblue}{RGB}{0,163,243}

\tcbset{mystyle/.style={
  breakable,
  enhanced,
  outer arc=0pt,
  arc=0pt,
  colframe=myblue,
  colback=myblue!20,
  attach boxed title to top left,
  boxed title style={
    colback=myblue,
    outer arc=0pt,
    arc=0pt,
    top=3pt,
    bottom=3pt,
    },
  fonttitle=\sffamily
  }
}

\newtcolorbox[auto counter,number within=section]{example}[2][]{
  mystyle,label=#2,
  title=Example~\thetcbcounter,
  overlay unbroken and first={
      \path
        let
        \p1=(title.north east),
        \p2=(frame.north east)
        in
        node[anchor=west,font=\sffamily,color=myblue,text width=\x2-\x1] 
        at (title.east) {#1};
  }
}
\newtcolorbox[auto counter]{assumption}[2][]{
  mystyle,label=#2,
  colback=white,
  rightrule=0pt,
  toprule=0pt,
  title=Assumption SLR.\thetcbcounter,
  overlay unbroken and first={
      \path
        let
        \p1=(title.north east),
        \p2=(frame.north east)
        in
        node[anchor=west,font=\sffamily,color=myblue,text width=\x2-\x1] 
        at (title.east) {#1};
  }
}

\begin{document}

\section{Test section}
\begin{example}{first}
test
\end{example}
\begin{assumption}{}
test
\end{assumption}
\begin{example}[Optional title]{}
test
\end{example}
\begin{assumption}[Optional title with some more words for the example so it
spans two lines]{fourth}
test
\end{assumption}

Box \ref{first}

box \ref{fourth}
\end{document}

在此处输入图片描述

编辑:关于您的额外要求:

\documentclass{article}
\usepackage[many]{tcolorbox}
\usetikzlibrary{calc}
% from https://tex.stackexchange.com/a/62252/121799
\let\Sectionmark\sectionmark
\def\sectionmark#1{\def\Sectionname{#1}\Sectionmark{#1}}
\let\Subsectionmark\subsectionmark
\def\subsectionmark#1{\def\Subsectionname{#1}\Subsectionmark{#1}}
\let\Subsubsectionmark\subsubsectionmark
\def\subsubsectionmark#1{\def\Subsubsectionname{#1}\Subsubsectionmark{#1}}


\definecolor{myblue}{RGB}{0,163,243}

\tcbset{mystyle/.style={
  breakable,
  enhanced,
  outer arc=0pt,
  arc=0pt,
  colframe=myblue,
  colback=myblue!20,
  attach boxed title to top left,
  boxed title style={
    colback=myblue,
    outer arc=0pt,
    arc=0pt,
    top=3pt,
    bottom=3pt,
    },
  fonttitle=\sffamily
  }
}

\newtcolorbox[auto counter,number within=section]{example}[2][\Sectionname]{
  mystyle,label=#2,
  title=Example~\thetcbcounter,
  overlay unbroken and first={
      \path
        let
        \p1=(title.north east),
        \p2=(frame.north east)
        in
        node[anchor=west,font=\sffamily,color=myblue,text width=\x2-\x1] 
        at (title.east) {#1};
  }
}
\newtcolorbox[auto counter]{assumption}[2][\Sectionname]{
  mystyle,label=#2,
  colback=white,
  rightrule=0pt,
  toprule=0pt,
  title=Assumption SLR.\thetcbcounter,
  overlay unbroken and first={
      \path
        let
        \p1=(title.north east),
        \p2=(frame.north east)
        in
        node[anchor=west,font=\sffamily,color=myblue,text width=\x2-\x1] 
        at (title.east) {#1};
  }
}

\begin{document}

\section{Test section}
\begin{example}{first}
test
\end{example}
\begin{assumption}{x}
test
\end{assumption}
\begin{example}[Optional title]{y}
test
\end{example}
\begin{assumption}[Optional title with some more words for the example so it
spans two lines]{fourth}
test
\end{assumption}

Box \ref{first}

box \ref{fourth}
\end{document}

在此处输入图片描述

相关内容