9

I've used the titlesec package to produce section headers like this:

screenshot
Click for a full-resolution version.

Using this minimal code:

\documentclass{article}
\usepackage[compact]{titlesec}
\usepackage{blindtext}
\usepackage{tikz}\usetikzlibrary{shapes.misc}

\newcommand\secbar {
    \tikz[baseline, trim left=3.2cm] 
    {
        \fill [white] (3cm,0.1ex) rectangle +(0.2cm,1.1ex);
        \draw [gray!95, fill=gray!80] (0cm,0.1ex) rectangle (3cm,1.1ex);        
    }
}
\newcommand\subsecbar {
    \tikz[baseline, trim left=2.2cm] 
    {
        \fill [white] (2cm,0.1ex) rectangle +(0.2cm,1.1ex);
        \fill [blue!40] (0cm,0.1ex) rectangle (2cm,1.1ex);      
    }

\titleformat{\section}{\large}{}{0cm}{\secbar}
\titleformat{\subsection}{\large}{}{0cm}{\subsecbar}

\begin{document}
\section{A section}
\subsection{A subsection}
\end{document}

However, I'd like the use the memoir package, which is incompatible with titlesec. I know that memoir has its own commands to alter sections. For example, \setsechook{Hello} will place "Hello" before the section header. However, it also places a newline after "Hello". I haven't made able to make much progress looking at the memoir documentation.

Can someone guide me to reproducing the same section header commands as above but using the memoir package and without titlesec?

8

One possibility:

\documentclass{memoir}
\usepackage{tikz}

\setcounter{secnumdepth}{0}

\newcommand\secbar{%
    \tikz[baseline, trim left=3.2cm] 
    {
        \fill [white] (3cm,0.1ex) rectangle +(0.2cm,1.1ex);
        \draw [gray!95, fill=gray!80] (0cm,0.1ex) rectangle (3cm,1.1ex);        
    }%
}
\newcommand\subsecbar{%
    \tikz[baseline, trim left=2.2cm] 
    {
        \fill [white] (2cm,0.1ex) rectangle +(0.2cm,1.1ex);
        \fill [blue!40] (0cm,0.1ex) rectangle (2cm,1.1ex);      
    }%
}
\makeatletter
\renewcommand{\section}{%
  \sechook%
  \@startsection{section}{1}%  level 1
      {\secindent}%            heading indent
      {\beforesecskip}%        skip before the heading
      {\aftersecskip}%         skip after the heading
      {\noindent\llap{\secbar}\normalfont\secheadstyle}} % font
\setsecheadstyle{\large\memRTLraggedright}
\renewcommand{\subsection}{%
  \subsechook%
  \@startsection{subsection}{2}%  level 2
      {\subsecindent}%            heading indent
      {\beforesubsecskip}%        skip before the heading
      {\aftersubsecskip}%         skip after the heading
      {\noindent\llap{\subsecbar}\normalfont\subsecheadstyle}} % font
\setsubsecheadstyle{\large\memRTLraggedright}
\makeatother

\begin{document}

\chapter{Test Chapter}
\section{Test Section}
\subsection{Test Subsection}

\end{document}

enter image description here

By the way, you don't really need TikZ to draw the colored bars:

\documentclass{memoir}
\usepackage{xcolor}

\setcounter{secnumdepth}{0}

\newcommand\MyBar[5]{%
  {\setlength\fboxsep{0pt}\color{#1}\fbox{\color{#2}\rule{#3}{#4}}}\hspace{#5}}

\newcommand\secbar{\MyBar{gray!95}{gray!80}{2.8cm}{1ex}{0.2cm}}
\newcommand\subsecbar{\MyBar{blue!40}{blue!40}{1.8cm}{1ex}{0.2cm}}%

\makeatletter
\renewcommand{\section}{%
  \sechook%
  \@startsection{section}{1}%  level 1
      {\secindent}%            heading indent
      {\beforesecskip}%        skip before the heading
      {\aftersecskip}%         skip after the heading
      {\noindent\llap{\secbar}\normalfont\secheadstyle}} % font
\setsecheadstyle{\large\memRTLraggedright}
\renewcommand{\subsection}{%
  \subsechook%
  \@startsection{subsection}{2}%  level 2
      {\subsecindent}%            heading indent
      {\beforesubsecskip}%        skip before the heading
      {\aftersubsecskip}%         skip after the heading
      {\noindent\llap{\subsecbar}\normalfont\subsecheadstyle}} % font
\setsubsecheadstyle{\large\memRTLraggedright}
\makeatother

\begin{document}

\chapter{Test Chapter}
\section{Test Section}
\subsection{Test Subsection}

\end{document}
  • Very in-depth answer. I've chosen this as the right answer. – TSGM Jun 12 '13 at 10:37
5

Why all that recoding? Just adding \leavevmode\llap{\secbar} to \setsecheadstyle seems to work fine.

\documentclass{memoir}
\usepackage{xcolor}

\setcounter{secnumdepth}{0}

\newcommand\MyBar[5]{%
{\setlength\fboxsep{0pt}\color{#1}\fbox{\color{#2}\rule{#3}{#4}}}\hspace{#5}}

\newcommand\secbar{\MyBar{gray!95}{gray!80}{2.8cm}{1ex}{0.2cm}}
\newcommand\subsecbar{\MyBar{blue!40}{blue!40}{1.8cm}{1ex}{0.2cm}}%


\setsecheadstyle{\large\memRTLraggedright\leavevmode\llap{\secbar}}
\setsubsecheadstyle{\large\memRTLraggedright\leavevmode\llap{\subsecbar}}




\begin{document}

\chapter{Test Chapter}
\section{Test Section}
\subsection{Test Subsection}

\end{document}
  • I had no idea it could be done so simply. Thank you! – TSGM Jun 12 '13 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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