1

I have colored boxes like the ones in the link:

How to label them? My simple code is:

\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}
2
  • 1
    Welcome to TeX.SX! Option label=yourlabelhere.
    – TeXnician
    Apr 29, 2017 at 11:08
  • The tag labels is not meant for cross-reference labels!
    – user31729
    Apr 29, 2017 at 11:14

1 Answer 1

3

Basically you should use the tcolorbox option label=. It is really advisable to read the tcb manual.

Here's one version taking your definitions and using a mandatory argument to take the label. Please modify it to your needs.

tcolorbox label

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .