Envolva o ambiente mdframed em um comando

Envolva o ambiente mdframed em um comando

Criei (copiei) um ambiente simples para criar uma moldura em torno de um trecho de texto. O ambiente usa o mdframesambiente para encaixotar o conteúdo e uma macro simples changemarginque é usada para adicionar algum espaço em branco à esquerda e à direita da caixa.

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

Este é o resultado. Funciona perfeitamente. insira a descrição da imagem aqui

Minha pergunta é: posso usar meu ambiente como um comando?

\Boxed{title}{content}

Responder1

Na verdade, é possível usar um mdframedambiente dentro de um comando se ele estiver encapsulado em um BoxedInternalambiente interno.

Melhorei o exemplo com um argumento opcional para BoxedInternalambiente e \Boxedmacro.

A questão é se tais comandos wrapper são úteis em geral.

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

insira a descrição da imagem aqui

informação relacionada