I'm attempting to put a frame around my section headings. I am using the package mdframed which adds functionality to the framed package. It basically draws a box around an object, in an environment. So I wonder how I can do this. Can I use \renewcommand, for example? I'd really like to use mdframed to create the frame.
- 25,784
- 829
3 Answers
The titlesec package provides this functionality built-in. Here's an example:
\documentclass{article}
\usepackage{titlesec}
\titleformat{\section}[frame]
{\normalfont} {} {5pt} {\Large\bfseries\filcenter\thesection.\quad}
\begin{document}
\section{A section}
\end{document}

If this isn't what you want, you should probably use the package in conjunction with mdframed to modify your sections anyway, since it provides a simple, yet powerful system for modifying all section headings.
- 218,180
A very hands-on approach to modifying the sectional representation is possible by redefining \section as a whole. Vincent Zoonekynd's LaTeX website on sections does exactly this, and provides 37 different section definitions, including framing the title. Not always as pretty/clean as using a package (as in the other answers), but it does give the user control over every detail of the typesetting.
Here's example 31 in the form of an MWE, with modifications to use lipsum:
\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\makeatletter
\def\section{\@ifstar\unnumberedsection\numberedsection}
\def\numberedsection{\@ifnextchar[%]
\numberedsectionwithtwoarguments\numberedsectionwithoneargument}
\def\unnumberedsection{\@ifnextchar[%]
\unnumberedsectionwithtwoarguments\unnumberedsectionwithoneargument}
\def\numberedsectionwithoneargument#1{\numberedsectionwithtwoarguments[#1]{#1}}
\def\unnumberedsectionwithoneargument#1{\unnumberedsectionwithtwoarguments[#1]{#1}}
\def\numberedsectionwithtwoarguments[#1]#2{%
\ifhmode\par\fi
\removelastskip
\vskip 3ex\goodbreak
\refstepcounter{section}%
\hbox to \hsize{%
\fbox{%
\hbox to 1cm{\hss\bfseries\Large\thesection.\ }%
\vtop{%
\advance \hsize by -1cm
\advance \hsize by -2\fboxrule
\advance \hsize by -2\fboxsep
\parindent=0pt
\leavevmode\raggedright\bfseries\Large
#2
}%
}}\nobreak
\vskip 2mm\nobreak
\addcontentsline{toc}{section}{%
\protect\numberline{\thesection}%
#1}%
\ignorespaces
}
\def\unnumberedsectionwithtwoarguments[#1]#2{%
\ifhmode\par\fi
\removelastskip
\vskip 3ex\goodbreak
% \refstepcounter{section}%
\hbox to \hsize{%
\fbox{%
% \hbox to 1cm{\hss\bfseries\Large\thesection.\ }%
\vtop{%
% \advance \hsize by -1cm
\advance \hsize by -2\fboxrule
\advance \hsize by -2\fboxsep
\parindent=0pt
\leavevmode\raggedright\bfseries\Large
#2
}%
}}\nobreak
\vskip 2mm\nobreak
\addcontentsline{toc}{section}{%
% \protect\numberline{\thesection}%
#1}%
\ignorespaces
}
\makeatother
\pagestyle{empty}
\begin{document}
\section*{Introduction}
\lipsum[1]
\section{Suite}
\lipsum[2]
\section{Suite}
\lipsum[3]
\section{Fin}
\lipsum[4]
%\tableofcontents
\end{document}

- 603,163
If you also need the \Section* version, then the definition has to be extended
\documentclass{article}
\usepackage{mdframed}
\makeatletter
\newcommand\Section[2][]{\begin{mdframed}[linewidth=5pt]%
\ifx\relax#1\relax\section{#2}\else\section[#1]{#2}\fi
\end{mdframed}}
\makeatother
\begin{document}
\Section{A section}
\end{document}

\@dblargfor doubling the argument when an optional argument is not present. – egreg Oct 14 '11 at 20:18:-|– Werner Oct 14 '11 at 20:33