如何使用“newtcolorbox”和“newmdtheoremenv”创建相同类型的灰色框?

如何使用“newtcolorbox”和“newmdtheoremenv”创建相同类型的灰色框?

我想转换为使用newtcolorbox而不是newmdtheoremv。这里我有一个简单的、缩进的灰色框:

在此处输入图片描述

  1. 我该如何实现同样的效果newtcolorbox
  2. 我如何才能在命令中使用自定义标题和不使用标题之间进行调整\begin{greybox}?有时我可能想要例如例子,...作为标题,有时我可能根本不想要任何标题。
  3. 如果我想让文本出现在标题下方而不是直接出现在标题之后,我该怎么做?

\documentclass{article}
\usepackage{parskip}

\usepackage{ntheorem} % greybox COMMAND
\usepackage[framemethod=tikz]{mdframed} % greybox COMMAND

\theoremstyle{nonumberplain}
\newmdtheoremenv[
    backgroundcolor=gray!25,
    hidealllines=true,
    %leftline=false,
    %linewidth=10pt,
    %linecolor=white,
    leftmargin=15pt,
    rightmargin=15pt,
    innertopmargin=3pt,
    innerbottommargin=3pt,
    innerrightmargin=5pt,
    innerleftmargin=5pt,
    skipabove=12pt,
    skipbelow=20pt
]{greybox}{Example}

\begin{document}

Here is some text before to show the indentation.

\begin{greybox}
    Here is my grey box.
    
    With some text in it.
\end{greybox}

Here is some text after to show the indentation.

\end{document}

答案1

根据您的风格声明一个新的tcolorboxwith 。然后从中派生出几个环境:带或不带标题、标题在单独一行等。newtcolorbox

\documentclass{article}
\usepackage{parskip}
\usepackage{tcolorbox}

\newtcolorbox{mybasecolorbox}[1][]{%
  colback=gray!25, colframe=gray!25,
  coltitle=black, fonttitle=\bfseries,
  sharp corners,
  width=(\linewidth-30pt),
  title=#1}

\newenvironment{mytitlebox}[1][]{%
  \centering
  \mybasecolorbox[#1]
  \itshape
}{%
  \endmybasecolorbox
}

\newenvironment{myinlinebox}[1][]{%
  \mytitlebox
  {\upshape\bfseries #1}%
}{%
  \endmybasecolorbox
}

\newenvironment{example}{%
  \mytitlebox[Example]
}{%
  \endmytitlebox
}

\newenvironment{example2}{%
  \myinlinebox[Example]
}{%
  \endmyinlinebox
}


\begin{document}

Here is some text before to show the indentation.

\begin{example}
  Here is my grey box.\par
  With some text in it.
\end{example}

\begin{example2}
  Here is my grey box.\par
  With some text in it.
\end{example2}

\begin{mytitlebox}
  Here is my grey box.\par
  With some text in it.
\end{mytitlebox}

\begin{mytitlebox}[Foobar]
  Here is my grey box.\par
  With some text in it.
\end{mytitlebox}

\begin{myinlinebox}[Foobar]
  Here is my grey box.\par
  With some text in it.
\end{myinlinebox}

Here is some text after to show the indentation.

\end{document}

在此处输入图片描述

相关内容