1

I'm trying to create a custom environment in LaTeX that includes boxed text with rounded corners, and I want to attach a TikZ drawing to the top-left corner of the box.

I've attempted to implement this, but I'm facing issues with positioning the elements correctly. Here's the code I've tried:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\usepackage{tikz}

\definecolor{beige}{HTML}{F2ECD8}
\definecolor{grn}{HTML}{7AA999}
\definecolor{purple}{HTML}{402A3E}
\definecolor{orange}{HTML}{BF7D65}
\definecolor{blue}{HTML}{2C858D}
\definecolor{prp}{HTML}{D96F66}


% Define a new counter for the example number
\newcounter{example}[section]
\renewcommand{\theexample}{\thesection.\arabic{example}}

% Define a new environment with boxed text, TikZ drawing, and counter
\newtcolorbox{myboxedtext}[1]{
  enhanced,
  colback=white,
  colframe=black,
  boxrule=0.5mm,
  before=\begin{tikzpicture}[remember picture],
  after=\end{tikzpicture},
  overlay unbroken and first = {
    \begin{tikzpicture}[remember picture,overlay]
      \def\cola{orange!25!purple}
      \def\colb{blue}
      \node[circle,fill=\cola,minimum size=7mm,inner sep=0pt] (circle) at (0,0) {};
      \node[fill=\cola,anchor=west,minimum height=7mm,minimum width={width("Exemple : #1")+7mm}] at (0,0) {};
      \node[fill=beige,line width=2pt,circle,draw=\cola,minimum size=8mm] at ({width("Exemple : #1")+9mm},0) {\refstepcounter{example}\theexample};
      \node[text=beige,anchor=west] at (0.1,0) {Exemple : #1};
    \end{tikzpicture}
  }
}

\begin{document}

%\section{Examples}

\begin{myboxedtext}{test}
  Your text goes here.
\end{myboxedtext}

\end{document}

In this code, I'm using the tcolorbox package for the boxed text, and I've included a TikZ drawing that I'd like to be positioned at the top-left corner of the box.

However, I'm encountering problems with the positioning, and the elements are not aligning as expected. I'd like the circle from the TikZ drawing to be at the top-left corner of the box, and the other elements to be positioned accordingly. This is the output I'm getting : enter image description here

Could someone please help me correct the code to achieve the desired layout?

Thank you in advance for your assistance!

4
  • Welcome! Just to be clear: do you want the title to be to the right of the circle? Or do you want it sticking out to the left?
    – cfr
    Commented Sep 14, 2023 at 15:33
  • I just want the whole tikz drawing to be at the top left of the box, I don't want to change the order of the circle and title. Thank you
    – Mar1One30
    Commented Sep 15, 2023 at 14:31
  • So you don't want the circle at the corner? You want the entire label to be moved up, is that it?
    – cfr
    Commented Sep 15, 2023 at 16:06
  • Does your example compile for you? Because I'm getting an error ....
    – cfr
    Commented Sep 15, 2023 at 16:11

1 Answer 1

1

Do you want something like this?

fancy boxed title for fancy box

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\usepackage{tikz}

\definecolor{beige}{HTML}{F2ECD8}
\definecolor{grn}{HTML}{7AA999}
\definecolor{purple}{HTML}{402A3E}
\definecolor{orange}{HTML}{BF7D65}
\definecolor{blue}{HTML}{2C858D}
\definecolor{prp}{HTML}{D96F66}


% Define a new counter for the example number
\newcounter{example}[section]
\renewcommand{\theexample}{\thesection.\arabic{example}}

% Define a new environment with boxed text, TikZ drawing, and counter
\colorlet{cola}{orange!25!purple}
\colorlet{colb}{blue}
\newtcolorbox[use counter=example,number within=section]{myboxedtext}[1]{
  enhanced,
  colback=white,
  colframe=black,
  boxrule=0.5mm,
  attach boxed title to top left={yshift=-.5\baselineskip},
  title={Exemple : #1},
  coltitle=beige,
  boxed title style={%
    empty,
    left=0pt,
    overlay unbroken and first={%
      \node [circle,fill=cola,minimum size=7mm,inner sep=0pt,anchor=center] (circle) at (frame.west) {};
      \node (n1) [fill=cola,anchor=west,minimum height=7mm,minimum width={width("Exemple : #1")+7mm}] at (frame.west) {};
      \node (c1) [circle,fill=cola,minimum size=7mm,inner sep=0pt,anchor=center]  at (n1.east) {};
      \node (c2) [fill=beige,line width=2pt,circle,draw=cola,minimum size=8mm,anchor=center] at (c1) {\thetcbcounter};
    },
  },
}

\begin{document}

%\section{Examples}

\begin{myboxedtext}{test}
  Your text goes here.
\end{myboxedtext}

\end{document}
2
  • Thank you. That's exactly what i wanted.
    – Mar1One30
    Commented Sep 16, 2023 at 12:03
  • @Mar1One30 You're welcome. Please consider accepting either this or another answer if one addresses your question satisfactorily. It makes it easier for other people to find solutions if they can see which questions have accepted answers. Disclaimer: accepting my answer will award me points; accepting any answer will award you points.
    – cfr
    Commented Sep 16, 2023 at 14:07

You must log in to answer this question.

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