tcolorbox 中的标签

tcolorbox 中的标签

我有类似链接中的彩色盒子:

如何标记它们?我的简单代码是:

\documentclass{book}
\usepackage[listings,many]{tcolorbox}
\usetikzlibrary{calc}
\usepackage{listingsutf8}

\definecolor{myblue}{RGB}{20,105,176}

\tcbuselibrary{listingsutf8,breakable,skins}
\tcbset{listing engine=listings}
\tcbset{mystyle/.style={
  breakable,
  enhanced,
  rightrule=0pt,
  toprule=0pt,
  outer arc=0pt,
  arc=0pt,
  colframe=myblue,
  colback=white,
  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]{meubox}[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};
  }
}

\newtcblisting[use counter from=meubox]{meuboxcodigo}[2][]{
mystyle,
listing options={style=tcblatex},
listing only,
title=\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};
}
}

\newtcblisting[use counter from=meubox]{meuboxuso}[2][]{%
mystyle,
title=Exemplo \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}

\chapter{text 1}
\section{section text}

%label:box1
\begin{meubox}
Text text text
\end{meubox}

%label:box2
\begin{meuboxcodigo}[Preâmbulo para Língua Portuguesa]{}
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[brazil]{babel}
\usepackage[T1]{fontenc}
\usepackage{indentfirst}
\end{meuboxcodigo}

%label:box3
\begin{meuboxuso}{}
\begin{flushright}
Alinhando o texto.
\end{flushright}
\end{meuboxuso}

text text text \ref{box1}, \ref{box2},\ref{box3}
\end{document}

答案1

基本上你应该使用tcolorbox选项label=。建议你阅读 tcb 手册。

这是一个采用您的定义并使用强制参数来获取标签的版本。请根据您的需要进行修改。

tcolorbox 标签

\documentclass{book}
\usepackage[listings,many]{tcolorbox}
\usetikzlibrary{calc}

\definecolor{myblue}{RGB}{20,105,176}

\tcbset{listing engine=listings}
\tcbset{mystyle/.style={
  breakable,
  enhanced,
  rightrule=0pt,
  toprule=0pt,
  outer arc=0pt,
  arc=0pt,
  colframe=myblue,
  colback=white,
  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]{meubox}[2][]{
  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};
  },
  label=#2
}

\newtcblisting[use counter from=meubox]{meuboxcodigo}[2][]{
mystyle,
listing options={style=tcblatex},
listing only,
title=\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};
},
label=#2
}

\newtcblisting[use counter from=meubox]{meuboxuso}[2][]{%
mystyle,
title=Exemplo \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};
  },label=#2
}

\begin{document}

\chapter{text 1}
\section{section text}

%label:box1
\begin{meubox}{box1}
Text text text
\end{meubox}

%label:box2
\begin{meuboxcodigo}[Preâmbulo para Língua Portuguesa]{box2}
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[brazil]{babel}
\usepackage[T1]{fontenc}
\usepackage{indentfirst}
\end{meuboxcodigo}

%label:box3
\begin{meuboxuso}{box3}
\begin{flushright}
Alinhando o texto.
\end{flushright}
\end{meuboxuso}

text text text \ref{box1}, \ref{box2},\ref{box3}
\end{document}

相关内容