Based on the comments, it seems that you want to be able to set a particular section to be framed or not, without having to locate the end of that section to insert an additional command. One way to do this is to use \FramedSection as defined below instead of the normal \section.
The \FramedSection start a \begin{mdframed} and sets WithinFramedSection to be true so that we know that this framed section needs to be closed upon a subsequent \FramedSection, or a normal \section.
The closing of this mdframed environment is done via a call to \EndFramedIfNeeded. This is needed upon:
- a subsequent
\FramedSection
- a subsequent
\section
- and at the end of the document
Notes:
- I have used
newtoggle from the etoolbox package as I prefer that syntax versus the \newif syntax. But if you don't want to include an additional package it should be pretty straightforward to adapt this to use \newif or some other conditional methods.
- The
lipsum package was used just to provide dummy text. It is not needed in your document.
Code:
\documentclass{article}
\usepackage{etoolbox}
\usepackage{mdframed}
\usepackage{lipsum}
\newtoggle{WithinFramedSection}%
\togglefalse{WithinFramedSection}%
\let\OldSection\section%
\def\section{\EndFramedIfNeeded\OldSection}%
\newcommand{\EndFramedIfNeeded}{%
\iftoggle{WithinFramedSection}{%
\global\togglefalse{WithinFramedSection}%
\end{mdframed}% End previous framed section
}{}%
}%
\newcommand{\FramedSection}[1]{%
\EndFramedIfNeeded%
\global\toggletrue{WithinFramedSection}%
\begin{mdframed}\OldSection{#1}%
}%
% If the last \section is a \FramedSection, we need to end
% the mdframed environment.
\AtEndDocument{\EndFramedIfNeeded}%
\begin{document}
\FramedSection{Framed Section Name 1}
\subsection{Framed Sub section name 1}
\lipsum[1]
%
\section{Unframed Section}
\lipsum[2]
%
\FramedSection{Framed Section Name 2}
\subsection{Framed Sub section name 2}
\lipsum[3]
\end{document}
\begin{mdframed}...\end{mdframed}with the content you want framed placed within seems to work. Perhaps this is a good case where it would help if you composed a fully compilable MWE that illustrates the problem you are having. – Peter Grill Sep 16 '12 at 05:23\documentclass{article}followed by everything in my answer from\begin{document}to\end{document}. Then your intent would be clearer. Basically try to show what you want as opposed to just describing it. – Peter Grill Sep 16 '12 at 08:19