將 mdframed 環境包裝到命令中

將 mdframed 環境包裝到命令中

我創建(複製)了一個簡單的環境,以便在一段文字周圍創建一個框架。環境使用mdframes環境來對內容進行裝箱,並changemargin使用簡單的巨集來在框的左側和右側添加一些空白。

\documentclass{article}
\usepackage[utf8]{inputenc}

% mdframe: put a certain amount of text in a box
\usepackage[framemethod=default]{mdframed}
\usepackage{showexpl}
\mdfdefinestyle{exampledefault}{
    rightline=true,
    innerleftmargin=10,
    innerrightmargin=10,
    frametitlerule=true,
    frametitlerulecolor=black,
    frametitlebackgroundcolor=white,
    frametitlerulewidth=1pt,
}

% macro to change margins
\def\changemargin#1#2{\list{}{\rightmargin#2\leftmargin#1}\item[]}
\let\endchangemargin=\endlist

% custom environment
\newenvironment{Boxed}[1]
{
    \begin{changemargin}{2cm}{2cm} 
    \begin{mdframed}[style=exampledefault, frametitle={#1}]
}
{
    \end{mdframed}
    \end{changemargin}
}

\usepackage{lipsum} % add some text

\begin{document}
    \lipsum
    \begin{Boxed}{I'm the title}
        I'm the content. I've a nice frame around me.
    \end{Boxed}
\end{document}

這就是結果。它工作完美。 在此輸入影像描述

我的問題是,我可以像命令一樣使用我的環境嗎?

\Boxed{title}{content}

答案1

事實上,如果命令包裝在內部環境中,則可以mdframed在命令中使用環境BoxedInternal

我透過BoxedInternal環境和\Boxed巨集的可選參數改進了該範例。

問題是這樣的包裝器命令通常是否有用。

\documentclass{article}

\usepackage[xcolor]{mdframed}

\def\changemargin#1#2{\list{}{\rightmargin#2\leftmargin#1}\item[]}
\let\endchangemargin=\endlist

\mdfdefinestyle{exampledefault}{backgroundcolor=yellow!10!white}

\newenvironment{BoxedInternal}[2][]
{%
  \begin{changemargin}{0cm}{0cm}%
    \begin{mdframed}[style=exampledefault, frametitle={#2},#1]
    }{%
    \end{mdframed}%
  \end{changemargin}%
}

\newcommand{\Boxed}[3][]{%
  \begin{BoxedInternal}[#1]{#2}
    #3
  \end{BoxedInternal}%
}

\begin{document}

\Boxed{Foo}{Foobar}


\end{document}

在此輸入影像描述

相關內容