4

I've created (copied) a simple environment in order to create a frame around a piece of text. The environment uses the mdframes environment in order to box the content and a simple macro changemargin that is used to add some white space at the left and at the right of the box.

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

This is the result. It works perfectly. enter image description here

My question is, can I use my environment like a command?

\Boxed{title}{content}
incud
  • 165

1 Answers1

5

It is in fact possible to use a mdframed environment within a command if it is wrapped in an internal BoxedInternal environment.

I've improved the example with an optional argument to the BoxedInternal environment and \Boxed macro.

The question is whether such wrapper commands are useful in general.

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

enter image description here

  • It works perfectly – incud Jan 29 '17 at 10:55
  • @Ignus: Happy TeXing then. And tcolorbox has such \Boxed commands out of the box (pun intended ;-), perhaps you can give it a try also –  Jan 29 '17 at 10:56
  • Can I ask you another thing? Why Boxed takes three arguments insted of two? – incud Jan 29 '17 at 10:59
  • @Ignus: You can use it this way \Boxed[backgroundcolor=green]{Title}{Content}, the first argument with [...] is handed to the mdframed environment, so you could change the view etc. of the underlying box. If you never need this feature, just don't think about [...], just always use it as \Boxed[title]{content} and all is fine –  Jan 29 '17 at 11:06