3

I am a new learner in LaTeX and want to add label for referencing to the below code done in tcolorbox. This program example was provided by @Gonzalo Medina in his improved version where both tcbcounter and optional title is included.

I need a label option for both examples and assumptions.

Actually, after each section, I would be using blocks example and assumptions for each section so an option to also add current section title in the optional title text would be additionally of much help.

\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

1 Answer 1

3

You could add an argument for the label

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

enter image description here

EDIT: As for your additional request:

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

enter image description here

2
  • Can you please provide me a solution how to make the optional title to be the current section title so that every time I don't have to type the section title if needed... @marmot
    – Deb bijan
    Jul 31, 2018 at 10:34
  • @DebBijan Please see my update.
    – user121799
    Jul 31, 2018 at 11:00

You must log in to answer this question.

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