3

I am trying to customize my \section and \subsection command in such a way that they have two different styles. With the following code I am able to draw squared shades around the section numbering.

    \documentclass[a4paper,9pt]{extarticle}
    \usepackage[utf8]{inputenc}
    \usepackage{helvet}
      \renewcommand{\familydefault}{\sfdefault}
    \usepackage{titlesec}
      \titleformat*{\section}{\LARGE\mdseries}
      \titleformat*{\subsection}{\Large\bfseries}
    \usepackage{color}
    \makeatletter
      \definecolor{sectcolor}{rgb}{0.9,0.9,0.9}
      \let\origsection\section 
      \let\origsubsection\subsection
      \def\thesectiontext{\@arabic\c@section.}
      \def\thesubsectiontext{\thesectiontext\@arabic\c@subsection}
      \global\newsavebox{\sectbox}
      \def\makesectbox#1{\savebox\sectbox{\colorbox{sectcolor}
          {\makebox[1.47cm][r]{\normalfont\large\bfseries#1}}}}
      \renewcommand \thesection {\usebox{\sectbox}}
      \renewcommand \thesubsection {\usebox{\sectbox}}
      \renewcommand\section{\@ifstar{\myheading}{\mysection}}
      \newcommand\mysection[1]{\sectprelude{section}%
          {\thesectiontext}\origsection{#1}\mdseries}
      \titleformat{\mysection}
          {\fontsize{16}{19}\selectfont}{\thesection}{1em}{}
      \newcommand\myheading[1]{\sectprelude{section}%
          {\thesectiontext}\origsection*{#1}}
      \renewcommand\subsection[1]{\sectprelude{subsection}%
          {\thesubsectiontext}\origsubsection{#1}}
      \newcommand\sectprelude[2]{%
          \addtocounter{#1}{1}\makesectbox{#2}\addtocounter{#1}{-1}}
    \makeatother 
    \makeatletter
      \def\@seccntformat#1{\llap{\csname the#1\endcsname\quad}} 
    \makeatother 


    \begin{document}

    \section{Neuromancer}
    \vspace{1em}
     Count Zero Interrupt ...
    \section{Foundation series}

    \subsection{Foundation}
    \subsection{Foundation and Empire}
    \end{document}

The result of this code can be seen here:enter image description here

I however want, that there are no shaded boxes around the \subsection numbering and that the section number of the \section command is aligned with the section number of the \subsection command (meaning all the 2's on the left side of the dot have the same horizontal coordinate)

Does someone know a solution?

  • 2
    Have you exhausted the possibilities of sectsty & titlesec` packages or the special commands in memoir class? – musarithmia Sep 10 '15 at 20:24
  • This depends on the class in addition. For the standard classes there is \@sect doing the basic stuff, it's possible to decide there if it's a section or a subsection – user31729 Sep 10 '15 at 20:27
2

Here's one option:

\documentclass[a4paper,9pt]{extarticle}
\usepackage[utf8]{inputenc}
\usepackage{helvet}
\usepackage{xcolor}
\usepackage{titlesec}

\renewcommand{\familydefault}{\sfdefault}
\definecolor{sectcolor}{rgb}{0.9,0.9,0.9}

\titleformat{\section}
  {\normalfont\LARGE\selectfont\bfseries}
  {\llap{\colorbox{sectcolor}{%
    \makebox[1.4cm][r]{%
      \makebox[0.7cm][l]{\thesection.\hfill}}%
      }\hspace{10pt}%
    }%
  }
  {0em}
  {}
\titleformat{\subsection}
  {\normalfont\Large\bfseries}
  {\llap{\colorbox{white}{%
    \makebox[1.4cm][r]{%
      \makebox[0.7cm][l]{\thesubsection\hfill}}%
      }\hspace{10pt}%
    }%
  }
  {0em}
  {}

\begin{document}

\section{Neuromancer}
Count Zero Interrupt ...
\section{Foundation series}
\subsection{Foundation}
\subsection{Foundation and Empire}

\end{document}

enter image description here

Using instead

\titleformat{\section}
  {\normalfont\LARGE\selectfont\bfseries}
  {\llap{\colorbox{sectcolor}{%
    \makebox[1.4cm][l]{%
      \thesection.}%
      }\hspace{10pt}%
    }%
  }
  {0em}
  {}
\titleformat{\subsection}
  {\normalfont\Large\bfseries}
  {\llap{\colorbox{white}{%
    \makebox[1.4cm][l]{%
      \thesubsection}%
      }\hspace{10pt}%
    }%
  }
  {0em}
  {}

you get the numbers flushed left inside the color boxes:

enter image description here

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.