两个不同颜色的盒子(tcolorbox)

两个不同颜色的盒子(tcolorbox)

我想修改以下代码:

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

得到这个:

我的预期结果

你能帮助我吗?谢谢!

答案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}

在此处输入图片描述

相关内容