使用带有环境 \tcolorbox 和个人标题的标签

使用带有环境 \tcolorbox 和个人标题的标签

我对在乳胶文档中使用标签有点困惑,首先,我使用此命令创建个人标题

\newcommand {\mytitleTeo}[1]{                     
   \node[fill=red!30,
      rounded corners,
      draw=black,
      text=black,
      line width=1pt,
      inner sep=4pt,
      anchor=west,
      xshift=11pt]
   at (frame.north west){\bfseries #1};
}

之后我在下一个代码中使用它

\newtcolorbox[auto counter,number within=chapter, number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}}]{theo}[1]{%
colback = white,
enhanced,
overlay unbroken and first={\mytitleTeo{\textbf{Teorema~\thetcbcounter: #1}}},  
colframe=black,
boxrule=1pt,
arc=3mm,
breakable,
top=15pt,
before=\vskip18pt,

}

问题是我不知道如何在这个环境中使用 \label{},因为当我执行 \ref{} 时,它生成的数字与 \newtcolorbox 的数字不一致

多谢。

我认为这段代码可以编译

\documentclass[12pt,oneside,onecolumn,openright]{book} 
\usepackage[T1]{fontenc}
\usepackage{titlesec, blindtext, color}
\definecolor{gray75}{gray}{0.75}
\newcommand{\hsp}{\hspace{20pt}}
\titleformat{\chapter}[hang]{\Huge\bfseries} 
{\thechapter\hsp\textcolor{gray75}{|}\hsp}{0pt}{\Huge\bfseries}
\usepackage {graphicx}
\usepackage{amsfonts, amsmath, amsthm, amssymb, amscd, latexsym 
,cancel,stmaryrd}
\usepackage{pifont}
\usepackage{wrapfig}
%\usepackage[margin=2cm, font=scriptsize, small, bf]{caption}
\usepackage{appendix}
\usepackage{eso-pic}

\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{dsfont}
\usepackage{subfigure,caption}
\usepackage{listings}
\usepackage{mathtools}
\usepackage{tcolorbox}
\tcbuselibrary{breakable,skins}
\usepackage{helvet}
\usepackage[full]{textcomp}
%\usepackage{xcolor}
\usepackage{nameref}
\makeindex
\usepackage{pstricks}
\usepackage{natbib}
\usepackage{xparse}
\usepackage{xargs}
\usepackage{multicol}
%\usepackage[ruled,vlined,lined,linesnumbered,algochapter]{algorithm2e} 
\usepackage{framed}
\usepackage{titletoc} 
%usepackage{etoolbox}
%\usepackage{algorithm}
%\usepackage{algorithmic}
\usepackage[breaklinks,colorlinks=true, pdfstartview=FitV, linkcolor=blue, 
citecolor=blue, urlcolor=blue]{hyperref}
\usepackage{ upgreek }
%\usepackage{pgf,tikz}
%\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\usetikzlibrary{positioning ,shadows ,backgrounds ,arrows.meta, 
circuits.ee.IEC} 
%\usepackage{amsmath}
\usepackage{bm}
\usepackage{lmodern}
%\usepackage{fancyhdr}
%\usepackage{mathrsfs}
%\usetikzlibrary{arrows}
\usepackage{enumerate} 
%\usepackage[dvipsnames]{xcolor}
%\usepackage{vmargin}
\usepackage[spanish]{babel}
\usepackage[utf8]{inputenc}
\usepackage[margin=3cm]{geometry} 
\usepackage{makeidx}
\setlength{\parindent}{0cm}
%\usepackage{ mathrsfs }
%\usepackage{ amssymb }
%\usepackage{fancybox, calc}
%\usepackage{framed, color}
%\usepackage{graphicx}
%\usepackage{apacite}
\usepackage{float}
\usepackage{latexsym}
\usepackage{amssymb,amsfonts,amsthm}
\usepackage{pdfpages}
\usepackage{epstopdf}
\usepackage[all]{xy}
\usepackage{hyperref}
%\usepackage[colorlinks=false]{hyperref}
\usepackage{verbatim}
\usepackage[colorinlistoftodos]{todonotes}
%\usepackage[hidelinks]{hyperref}
\newcommand {\mytitleTeo}[2]{                    
\node[fill=red!30,
  rounded corners,
  draw=black,
  text=black,
  line width=1pt,
  inner sep=4pt,
  anchor=west,
  xshift=11pt]
  at (frame.north west){\bfseries #1};
  \label{#2},
    }
  \newtcolorbox[auto counter,number within=chapter, number freestyle= 
  {\noexpand\thechapter.\noexpand\arabic{\tcbcounter}}]{theo}[2]{%
     colback = white,
     enhanced,
     overlay unbroken and first={\mytitleTeo{\textbf{Teorema~\thetcbcounter: 
   #1}}{#2}}, 
     colframe=black,
     boxrule=1pt,
     arc=3mm,
     breakable,
     top=15pt,
     before=\vskip18pt,
    }
\hypersetup{colorlinks=true,linkcolor=Blue}
\begin{document}
\chapter{asdg}
\begin{theo}{Envoltura convexa, afín y positiva}{hola} \label{hola}
  wergregf
\end{theo}
\begin{theo}{sg}{hola} \label{hola2}
   wergregf
\end{theo}
\hyperref[hola]{Teorema \thechapter.\ref{hola}}
\hyperref[hola2]{Teorema \thechapter.\ref{hola2}}
\chapter{jjkk}
\begin{theo}{Envoltura convexa, afín y positiva}{} \label{hola3}
  wergregf
\end{theo}
\begin{theo}{sg}{hola} 
\label{hola4}
   wergregf
\end{theo}
\end{document}

现在我认为它有效

答案1

您无需将 a\node与 a混合使用tcolorbox,只需 atcolorbox即可完成所有操作。此外,使用命令\newtcbtheorem,您可以声明定理及其方面(请参阅tcolorbox文档或https://tex.stackexchange.com/a/236230/1952)。

一旦声明,定理框有两个参数,定理标题和供进一步参考的标签。此标签是一个后缀,将添加到由第五个参数 innewtcbtheorem和 a label separator(:默认情况下) 形成的前缀中。

您可以从以下代码开始。我已经从您的代码中清除了序言,因为您有很多不必要或重复的包。如果您给我们提供一个,那么回答会更容易minimum working (or not) example,其中最小值表示尽可能短。在您的例子中,正文很短,但序言不短。

\documentclass[a4paper]{book} 
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage[margin=3cm]{geometry} 
\usepackage{lmodern}

\usepackage[most]{tcolorbox}

\setlength{\parindent}{0cm}

\tcbset{%
    mytheostyle/.style={
        enhanced,
        colback=white,
        colframe=black,
        fonttitle=\bfseries,
        colbacktitle=red!30,
        coltitle=black,
        boxrule=1pt,
        attach boxed title to top left={%
            yshift*=-\tcboxedtitleheight/2, 
            xshift=5mm},
        boxed title style={colframe=black}
    }
}

\newtcbtheorem[auto counter, number within=chapter]{mytheorem}{Teorema}{mytheostyle}{Teo}

\begin{document}

\chapter{uno}

\begin{mytheorem}{Un teorema}{1}
Esto es un teorema
\end{mytheorem}

\begin{mytheorem}{Otro teorema}{2}
Este es el teorema~\ref{Teo:2}, diferente del teorema~\ref{Teo:1}
\end{mytheorem}

Este es el teorema~\ref{Teo:2}, diferente del teorema~\ref{Teo:1} y diferente del teorema~\ref{Teo:3}

\chapter{dos}

\begin{mytheorem}{Otro teorema más}{3}
Este es el primer teorema del segundo capítulo
\end{mytheorem}

\end{document}

在此处输入图片描述

相关内容