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.

I'm using the memoir class, I would like to create a more fancy section title, but I don't have the necessary skills in LaTeX. I found many fancy section titles on this forum, but I don't know how to customize it to make it look the way I want it. Is it possible to modify the section title to look like this (I drew this myself in OmniGraffle):

enter image description here

However, with these options:

  • Date and subtitle are optional, not all sections have them
  • Date is not necessarily today, but some day in the past
  • No section number is shown
  • both section title and subtitle can be longer than single line

Any help greatly appreciated!

share|improve this question
    
Welcome to TeX.sx! –  texenthusiast Mar 15 '13 at 5:25
    
Thanks ;-) Great forum! –  m0squito Mar 17 '13 at 23:45
    
Yes it's great forum , one day everyone might answer/comment question to give back to TeX.SX as they build experience –  texenthusiast Mar 18 '13 at 0:12

2 Answers 2

up vote 4 down vote accepted

Here's one possibility, using the titlesec package; before each \section, some commands can be used: \undefds suppresses previous dates and subtitles; \sectiondate{<text>} assigns <text> as the date for the following \section, and \sectionsubtitle{<text>} assigns <text> as the subtitle for the following \section.

Since titlesec was used, somre restrictions apply (See About memoir and titlesec incompatibility). The code:

\documentclass[article]{memoir}
\usepackage[margin=3cm]{geometry}% just for the example
\usepackage[T1]{fontenc}
\usepackage[explicit]{titlesec}
\usepackage{xcolor}
\usepackage{textcase}
\usepackage{lmodern}
\usepackage{lipsum}

\makeatletter
\newcommand*\nameundef[1]{%
  \expandafter\let\csname #1\endcsname\@undefined}
\newcommand\sectiondatefont{\normalfont\sffamily\itshape\bfseries\Large}
\newcommand\sectionsubtitlefont{\normalfont\sffamily\LARGE}
\newcommand\sectiondate[1]{\def\@sectiondate{#1}}
\newcommand\sectionsubtitle[1]{\def\@sectionsubtitle{#1}}
\titleformat{\section}
  {\normalfont\bfseries\huge\sffamily}{}{0em}
  {\colorbox{gray}{%
    \parbox[t]{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}{%
      \vspace*{1ex}%
      \@ifundefined{@sectiondate}
        {}{{\sectiondatefont\@sectiondate}\\*}%
      \raggedright%
      \textcolor{white}{\huge\MakeTextUppercase{#1}}%
      \@ifundefined{@sectionsubtitle}
        {}{\\*{\sectionsubtitlefont\@sectionsubtitle}}%
      \vspace*{1ex}
      }%
    }%
  }
\newcommand\undefds{%
  \nameundef{@sectiondate}\nameundef{@sectionsubtitle}}
\makeatother

\begin{document}

\sectiondate{March 15, 2013}
\sectionsubtitle{Test Subtitle}
\section{Test Section With a Title Spanning Two Lines}
\lipsum[4]

\undefds
\section{Test Section}
\lipsum[4]

\undefds
\sectiondate{April 23, 2009}
\section{Another Test Section}
\lipsum[4]

\undefds
\sectionsubtitle{Another Test Subtitle}
\section{Test Section}
\lipsum[4]

\end{document}

An image of the result:

enter image description here

share|improve this answer
    
Thank you so much for your work, this is exactly what I was looking for! –  m0squito Mar 16 '13 at 0:33

A possible improvement of Gonzalo's solution is using a key-value syntax:

\documentclass[article]{memoir}
\usepackage[margin=3cm]{geometry}% just for the example
\usepackage[T1]{fontenc}
\usepackage[explicit]{titlesec}
\usepackage{xcolor}
\usepackage{textcase}
\usepackage{lmodern}
\usepackage{lipsum}
\usepackage{keyval}
\usepackage{microtype}

\makeatletter
\define@key{mosquito}{date}{\def\mosquito@date{#1}}
\define@key{mosquito}{subtitle}{\def\mosquito@subtitle{#1}}
\newcommand\sectiondatefont{\normalfont\sffamily\itshape\bfseries\Large}
\newcommand\sectionsubtitlefont{\normalfont\sffamily\LARGE}

\titleformat{\section}
  {\normalfont\bfseries\sffamily}{}{0em}
  {\colorbox{gray}{%
    \parbox{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}{%
      \vspace{.5ex}%
      \ifx\mosquito@date\@empty\else
        {\sectiondatefont\mosquito@date\endgraf}
      \fi
      \raggedright
      \textcolor{white}{\huge\textls[50]{\MakeTextUppercase{#1}}\endgraf}
      \addvspace{.5ex}
      \ifx\mosquito@subtitle\@empty\else
        \endgraf{\sectionsubtitlefont\mosquito@subtitle\endgraf}
      \fi
      \addvspace{.5ex}
      }%
    }%
  }
\newcommand{\Section}[1][]{%
  \setkeys{mosquito}{date={},subtitle={},#1}%
  \section*}
\makeatother

\begin{document}

\Section[date={March 15, 2013},
  subtitle={Test Subtitle}]
  {Test Section With a Title Spanning Two Lines}
\lipsum[4]

\Section{Test Section}
\lipsum[4]

\Section[date={April 23, 2009}]
  {Another Test Section}
\lipsum[4]

\Section[subtitle={Another Test Subtitle}]
  {Test Section}
\lipsum[4]

\end{document}

I added perhaps better control of the vertical spacing and also letterspaced the uppercase title. Unfortunately Computer Modern and Latin Modern fonts lack sans serif small caps.

enter image description here

share|improve this answer
    
Thank you so much! I love this forum already, I can't believe how fast I got great answers to my question. –  m0squito Mar 16 '13 at 0:34
    
This is a nice approach! –  Gonzalo Medina Mar 16 '13 at 2:03
    
I applied your approach egreg and I love the way it looks in my document. However, I just noticed that it caused my TOC to disappear. I am using \usepackage[pdftex]{hyperref} to produce a PDF with TOC. Is there a way to fix this, or is this a hyperref issue? Thanks! –  m0squito Mar 18 '13 at 1:26
    
@m0squito You should use \section instead of \section* and add \setcounter{secnumdepth}{0} in the preamble, so that sections won't be numbered, but will go to the TOC nonetheless. –  egreg Mar 18 '13 at 9:27
    
Thank you, this did the trick! –  m0squito Mar 18 '13 at 16:12

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.