3

I am usingtikz package to produce a grey section-header bar (rounded at one end), thus:

\documentclass[a5paper]{memoir}

\setsecnumdepth{chapter}        %   set numbering level
\maxsecnumdepth{chapter}        %   default=section, 2

\setcounter{tocdepth}{1}        %   levels < {n} not in TOC

\usepackage[explicit]{titlesec} 
\usepackage{tikz}
\usetikzlibrary{shapes.misc}

                            %   create grey section header-bar
\let\currentsectiontitle\relax      

\newcommand\titlebar{%sections
\tikz[baseline,trim left=3.1cm,trim right=3.0cm] {
    \fill [black!15] (2.5cm,-1ex) rectangle (\textwidth+3.1cm,2.5ex);
    \node [
        fill=black!15!white,
        anchor= base east,
        rounded rectangle,
        minimum height=3.5ex] at (2.8cm,0.13) {
%        \color{black}{\thesection}
            };
}}
\titleformat{\section}
    {\large\bfseries}
    {\titlebar}
    {0.1cm}
    {\gdef\currentsectiontitle{#1}#1}   %   http://glurl.co/dFH
\renewcommand*{\thesection}{\arabic{section}}

                            %   start MWE   

\begin{document}

\tableofcontents*               %   table of contents
                            %   ------------------------    
\mainmatter                 %   document content

\chapter{Chapter 1}
\label{chapter1}

CHAPTER 1 intro

\section{Section 1}
\label{section1}

Section 1 intro

\chapter{Chapter 2}
\label{chapter2}

CHAPTER 2 intro

\section{Section 2}
\label{section2}

Section 2 intro

\end{document}

This originated from http://glurl.co/dFH (then reduced to eliminate redundant commands, by myself, using trial and error). I have remarked out the original command that placed section number in node at left end of section-header bar.

The problem I have is that when I set \maxsecnumdepth to {chapter}, which is what I want (so that sections are neither numbered in the text, nor in the TOC), the grey tikz section-header bar disappears—I suspect because the macro above depends upon the section being numbered.

Can anyone advise me on the most appropriate way to resolve this issue?

Please note: when running the MWE between the two settings (chapter / section) for \maxscnumdepth remember to compile twice to see the issue shift as described.

Thank you.

*My MWE produces the Chapter numbering twice. Not sure why.

  • 1
    Please complete your code in order to show us a full compilable document (which is, of course, as minimal as possible). Thanks. – LaRiFaRi Aug 6 '14 at 15:39
  • 1
    most document classes define two formats for headings -- one for the starred version of the sectioning (no number) and one without star (numbered). i suspect that \secnumdepth uses the format for the starred version, and you probably haven't provided a suitable format for that. – barbara beeton Aug 6 '14 at 16:52
  • thanks @barbarabeeton; I wondered about something similar. Unfortunately, I don't know how to address it … – johnbrc Aug 6 '14 at 17:41
  • 1
    @johnbrc -- i'm not sufficiently familiar with the innards of memoir to be more specific, but i hope this can help point the way for someone else. – barbara beeton Aug 6 '14 at 17:45
  • titlesec is not really compatible with memoir. – egreg Aug 6 '14 at 19:31
2

Since you suppressed section numbering, you need the numberless key to \titleformat:

\documentclass[a5paper]{memoir}
\usepackage[explicit]{titlesec} 
\usepackage{tikz}
\usetikzlibrary{shapes.misc}

\setsecnumdepth{chapter}
\maxsecnumdepth{chapter}

\setcounter{tocdepth}{1}

\let\currentsectiontitle\relax      

\newcommand\titlebar{%sections
\tikz[baseline,trim left=3.1cm,trim right=3.0cm] {
    \fill [black!15] (2.5cm,-1ex) rectangle (\textwidth+3.1cm,2.5ex);
    \node [
        fill=black!15!white,
        anchor= base east,
        rounded rectangle,
        minimum height=3.5ex] at (2.8cm,0.13) {
%        \color{black}{\thesection}
            };
}}
\titleformat{name=\section,numberless}
    {\large\bfseries}
    {\titlebar}
    {0.1cm}
    {\gdef\currentsectiontitle{#1}#1}

\begin{document}

\chapter{Test chapter}
Test text
\section{Test section}
Test text
\section{Another test section}
Test text

\end{document}

enter image description here

A page of the result;

By the way, I hope you've seen About memoir and titlesec incompatibility. Be awere also that with your current settings, you'll get undesired results if the title spans more than one line.

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.