使用方框文本和附加的 TikZ 绘图创建自定义环境

使用方框文本和附加的 TikZ 绘图创建自定义环境

我正在尝试在 LaTeX 中创建一个包含带圆角的方框文本的自定义环境,并且我想将 TikZ 图形附加到方框的左上角。

我尝试过实现这一点,但在正确定位元素方面遇到了问题。这是我尝试过的代码:

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

在此代码中,我使用 tcolorbox 包来处理框内文本,并且包含了一个 TikZ 图形,我想将其放置在框的左上角。

但是,我在定位方面遇到了问题,元素没有按预期对齐。我希望 TikZ 绘图中的圆圈位于框的左上角,其他元素也相应地定位。这是我得到的输出: 在此处输入图片描述

有人可以帮我修改代码以实现所需的布局吗?

提前感谢您的帮助!

答案1

你想要这样的东西吗?

花式盒装标题为花式盒装

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

相关内容