2

I would like to modify the following code :

\documentclass{article}
\usepackage[many]{tcolorbox}
\usetikzlibrary{calc}
\tcbset{mystyle/.style={
  breakable,
  enhanced,
  outer arc=0pt,
  arc=0pt,
 % colframe=blue,
 % colback=blue!20,
  attach boxed title to top left,
  boxed title style={
    colback=blue,
    outer arc=0pt,
    arc=0pt,
    top=3pt,
    bottom=3pt,
    },
  fonttitle=\sffamily,
  }
}
\newtcolorbox[auto counter]{Exercise}[1][]{
  mystyle,
  title=Exercise~\thetcbcounter,
  colframe=blue,
  colback=blue!10,
  overlay unbroken and first={
      \path
        let
        \p1=(title.north east),
        \p2=(frame.north east)
        in
        node[anchor=west,font=\sffamily,color=blue,text width=\x2-\x1] 
        at (title.east) {#1};
  }
}
\newtcolorbox[auto counter]{Example}[1][]{
  mystyle,
  colframe=red,
  colback=red!10,
  rightrule=0pt,
  toprule=0pt,
  title=Example \thetcbcounter,
  overlay unbroken and first={
      \path
        let
        \p1=(title.north east),
        \p2=(frame.north east)
        in
        node[anchor=west,font=\sffamily,color=red,text width=\x2-\x1] 
        at (title.east) {#1};
  }
}
\begin{document}
One...
\begin{Exercise}[The title of my exercise 1 on the right]
Here my exercise 1...
\end{Exercise}
Two...
\begin{Example}[The title of my example 1 in the center]
Here my example 1...
\end{Example}
Three..
\begin{Exercise}[The title of my exercise 2 on the right]
Here my exercise 2...
\end{Exercise}
Four...
\begin{Example}[The title of my example 2 in the center]
Here my example 2...
\end{Example}
Five..
\end{document}

to get this :

my expected result

Could you help me ? Thanks !

1 Answer 1

1
\documentclass{article}
\usepackage[many]{tcolorbox}
\usetikzlibrary{calc}
\tcbset{mystyle/.style={
  breakable,
  enhanced,
  outer arc=0pt,
  arc=0pt,
 % colframe=blue,
 % colback=blue!20,
  attach boxed title to top left,
  boxed title style={
    colback=#1,
    outer arc=0pt,
    arc=0pt,
    top=3pt,
    bottom=3pt,
    },
  fonttitle=\sffamily,
  }
}
\newtcolorbox[auto counter]{Exercise}[1][]{
  mystyle=blue,
  title=Exercise~\thetcbcounter,
  colframe=blue,
  colback=white,
  overlay unbroken and first={
      \path
        let
        \p1=(title.north east),
        \p2=(frame.north east)
        in
        node[anchor=east,font=\sffamily,color=blue,text width=\x2-\x1, align=right] 
        at (title-|frame.east) {#1};
  }
}

\newtcolorbox[auto counter]{Example}[1][]{
  mystyle=red,
  colframe=red,
  colback=white,
  title=Example \thetcbcounter,
  overlay unbroken and first={
      \path
        let
        \p1=(title.north east),
        \p2=(frame.north east)
        in
        node[anchor=east,font=\sffamily,color=red,text width=\x2-\x1, align=center] 
        at (title-|frame.east) {#1};
  }
}
\begin{document}
One...
\begin{Exercise}[The title of my exercise 1 on the right]
Here my exercise 1...
\end{Exercise}
Two...
\begin{Example}[The title of my example 1 in the center]
Here my example 1...
\end{Example}
Three..
\begin{Exercise}[The title of my exercise 2 on the right]
Here my exercise 2...
\end{Exercise}
Four...
\begin{Example}[The title of my example 2 in the center]
Here my example 2...
\end{Example}
Five..
\end{document}

enter image description here

3
  • +1 so simple -- can we use frame.east and title.east in place of north east in \p1 and \p2
    – js bibra
    Apr 15, 2021 at 0:14
  • 1
    @jsbibra Yes, \p1 and \p2 are used to calculate a width, As north east and east are on the same vertical line, it doesn't matter what you take. In any case, this was on the original code and I didn't touched it.
    – Ignasi
    Apr 15, 2021 at 7:31
  • I would like to know how to manage the indentation inside the box where we write the statement of the exercise. Thank you so much !
    – insan
    Apr 15, 2021 at 12:12

You must log in to answer this question.

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