Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Simple Question, but it seems to tricky for me. I don't know how to redefine the sectioning in a way, that the whole content (incl. subsections, ...) assigned to one \section{...} command would be framed and/or shaded by framed/mdframed. Even various tries and after hours of searching, I've honestly no clue how to handle that and would be very glad about some help.

BTW: I'm looking for a solution with no dependencies on packages like titlesec or similars... just a clean redefinition if possible. =)

share|improve this question
1  
Welcome to TeX.SE. A simple \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 5:23
    
@Peter Grill Well, it is not my ambition to handle that manually, otherwise there were no need for questioning. On the other hand, how should I do a MWE if I don't know where to redefine the sectioning? All reference points I know so far is \section{} and it only modifies the title - thats it. –  triton Sep 16 '12 at 7:04
    
@triton: In this case you could mock-up exactly how you would like your main document to look. If I understood the question, a MWE could be \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 8:19

1 Answer 1

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}
share|improve this answer
    
Brilliant! Thank you so much, I always thought I just could modify the title with \section{\@startsection{..}}. –  triton Sep 16 '12 at 8:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.