5

I'm preparing a document using tcolorbox as basic cells, like this:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable}
\newtcolorbox[auto counter, number within=section]{mybox}[1]{title=\thetcbcounter: #1}

\begin{document}

\section{Monday}
\begin{mybox}{A long long story}
Explanation
\end{mybox}

\section{Tuesday}
\begin{mybox}{Another long long story}
Explanation
\end{mybox}

\end{document}

Counter inside tcolorbox

Each section has multiple cells. Both the title and the content tend to be somewhat long. In my case, it feels visually annoying to put the counter in the title, so I wish to move it outside as an individual tag, like this:

Counter outside tcolorbox

The main cell is still horizontally aligned in the page, and the tag cell refers to the main cell's counter. Is there a convenient or proper way to do this?

6

You can use the overlay unbroken and first key to add a node with the counter (adjust the settings according to your needs):

\documentclass{article}
\usepackage[many]{tcolorbox}
\newtcolorbox[auto counter, number within=section]{mybox}[1]{
  enhanced,
  title=#1,
  overlay unbroken and first ={
    \node[anchor=north east,rounded corners,draw=black!80,fill=gray!30,line width=0.5mm,text width=2.5em,align=center,minimum height=4ex] at ([xshift=-\marginparsep]frame.north west) {\thetcbcounter};
  }
}

\begin{document}

\section{Monday}
\begin{mybox}{A long long story}
Explanation
\end{mybox}

\section{Tuesday}
\begin{mybox}{Another long long story}
Explanation
\end{mybox}

\end{document}

enter image description here

Same idea, but with a \tcbox inside the \node to place the counter:

\documentclass{article}
\usepackage[many]{tcolorbox}
\newtcolorbox[auto counter, number within=section]{mybox}[1]{
  enhanced,
  title=#1,
  overlay unbroken and first ={
    \node[anchor=north east,inner sep=0pt,outer sep=0pt] at ([xshift=-\marginparsep]frame.north west) {\tcbox[boxsep=0pt]{\thetcbcounter}};
  }
}

\begin{document}

\section{Monday}
\begin{mybox}{A long long story}
Explanation
\end{mybox}

\section{Tuesday}
\begin{mybox}{Another long long story}
Explanation
\end{mybox}

\end{document}

enter image description here

  • Very nice, Gonzalo. :-) – karlkoeller Mar 9 '15 at 18:40
  • @Gonzalo Medina, Can two same tcolorbox macro with an exactly same format and design but just a custom heading change be referred using cref for both will have same counter but will be used in the same chapter for two purposes say Boys & Girls. – Deb bijan Aug 9 '18 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.