如何使用“tcolorbox”建立如下所示的“範例”環境?

如何使用“tcolorbox”建立如下所示的“範例”環境?

在此輸入影像描述

如何使用「tcolorbox」套件建立如下圖所示的「範例」環境?

答案1

我認為這裡的要點有以下幾點。

  • 您可以定義一個example框,以便範例本身就是盒子的一部分,而解決方案是降低部分。然後,在框中,您可以使用\tcblower切換到解決方案。
  • 您可以使用 鍵before lower設定框的解決方案部分的標題。
  • 如果您想要對範例進行編號的選項,您可以使用theorems庫來定義帶有 的方塊\newtcbtheorem。如果您這樣做,帶有星號的盒子環境版本將沒有編號。

所以,我會用這樣的東西

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\newtcbtheorem{example}{Example}{
    theorem style=plain,
    arc=2mm,
    boxrule=.2mm,
    coltitle=black,
    colframe=blue!50!black,
    colback=blue!5!white,
    fonttitle=\sffamily\bfseries,
    coltitle=blue!50!black,
    before lower={\textcolor{blue!50!black}{\textsf{\textbf{Solution:}}}\ },
}{}
\begin{document}
\begin{example*}{}
The result (1.40) can be obtained by direct Taylor series expansion of \(1/\lvert\mathbf{x} - \mathbf{x}'\rvert\). Try to finish it by yourself.
\tcblower
From the Taylor series:
\[
\Phi(\mathbf{x}) = \Phi(0) + \mathbf{x} \cdot \nabla\Phi(0) + \frac{1}{2} \sum_{i=1}^3 \sum_{j=1}^3 x_i x_j \frac{\partial^2\Phi}{\partial x_i \partial x_j} + \cdots
\]
\end{example*}
\end{document}

答案2

請注意,我認為我不應該回答“為我做”。

您既沒有提供程式碼,也沒有提供任何導致您遇到困難的指示,甚至沒有提供任何關於所顯示的框的哪些功能對您重要的提示。以下內容可能有用,也可能沒有用作為起點。 (但是您至少可以從套件文件中獲得同樣有用的範例,以及更好的註釋。)

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{xparse,skins,hooks}
\colorlet{bcol}{blue!50!black}
\NewTColorBox
{egbox}{ O {\linewidth} O {} }
{
  bocs,
  before upper app={\textsf{\textcolor{bcol}{Example: }}},
  before lower app={\textsf{\textcolor{bcol}{Solution: }}},
  halign lower=left,
  width=#1,
  #2,
}
\tcbset{%
  bocs/.style={%
    colframe=bcol,
    colback=gray!20,
    boxrule=.75mm,
    boxsep=.25mm,
    toptitle=.75mm,
    bottomtitle=.75mm,
    fonttitle=\sffamily,
  },
}
\begin{document}
\begin{egbox}
  Some stuff.
  \tcblower
  Some more stuff.
\end{egbox}
\begin{egbox}[.5\linewidth]
  Some stuff.
  \tcblower
  Some more stuff.
\end{egbox}
\begin{egbox}[.4\linewidth][colframe=red]
  Some stuff.
  \tcblower
  Some more stuff.
\end{egbox}
  
\end{document}

各種顏色和寬度的盒子

相關內容