[Tex/LaTex] Shaded box with section number

sectioningshading

The code below is not working. This is because, I have savenotes in my TeX file, it works when I remove savenotes. Is there a way that I can get the code working without removing savenotes?

Here is the code:

    %---------------------Preamble---------------%
\documentclass[b5paper,twoside,10pt,openany]{book}
\usepackage[margin=1in]{geometry}       
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[bitstream-charter]{mathdesign}
\usepackage{amsmath}
\usepackage{sectsty}
\usepackage[english]{babel}
\usepackage{microtype}          
\usepackage{natbib}             
\pagenumbering{gobble}          
\usepackage{fancyhdr}           
\usepackage{ragged2e}
\usepackage{booktabs}
\captionsetup[table]{skip=10pt}
\providecommand\phantomsection{}
\setcounter{secnumdepth}{3}
\usepackage{tabu}
\allsectionsfont{\sffamily}
\usepackage[explicit]{titlesec}
\usepackage{tikz}

%\usetikzlibrary{shapes.misc,arrows}
%defining subsection titles
\newcommand\titlebar{%
\tikz[baseline,trim left=0cm,trim right=3cm] {
    \node [
        fill=black!90,
        text = white,
        anchor= base east,
        minimum height=3.5ex] (a) at (3cm,0) {
        \textbf{\arabic{chapter}.\thesection}
    };
}%
}
\titleformat{\section}{\large\sf}{\titlebar}{0.25cm}{\textcolor{black}{#1}}      %% Change color if needed and remove \sf.
\titlespacing*{\section}{-2cm}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\renewcommand*{\thesection}{\arabic{section}}

%defining subsection titles
\newcommand\subtitlebar{%
\tikz[baseline,trim left=0cm,trim right=3cm] {
    \node [
        fill=black!90,
        text = white,
        anchor= base east,
        minimum height=3.5ex] (b) at (3cm,0) {
        \textbf{\arabic{chapter}.\arabic{section}.\thesubsection}
    };
}%
}

\titleformat{\subsection}{\normalfont\sf}{\subtitlebar}{0.2cm}{\textcolor{black}{#1}}  %% Change color if needed and remove \sf.
\titlespacing*{\subsection}{-1.8cm}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\renewcommand*{\thesubsection}{\arabic{subsection}}

%---------------------Document starts here---------------%
\begin{document}    

%---------------------Table of contents---------------%
\pagestyle{empty}
\begin{onehalfspacing}          % here doublespacing can be used. Adjust the margins of the page!
\setcounter{tocdepth}{1}        % to display a detailed table of contents with each subsection
\tableofcontents
\end{onehalfspacing}
%\pagestyle{empty}
%---------------------Adding space in the whole thesis---------------%
\cleardoublepage
\begin{spacing}{1.3}
%\sectionfont{\noindent\fbox}  % makes a box around the section title.

%---------------------Headers, Footers and Page numbers---------------%
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{ \markboth{#1}{} }
\lhead{}
\chead{}
\renewcommand{\headrulewidth}{0.4pt} %
\renewcommand{\footrulewidth}{0.4pt} %
\pagenumbering{arabic}\setcounter{page}{9}
\fancyhf{}
\fancyhead[LE]{Chapter \thechapter}
\fancyhead[RO]{\nouppercase\leftmark}
\fancyfoot[LE,RO]{\thepage}

%---------------------Chapters---------------%
\chapter{First Chapter}

\section{Introduction}

\begin{savenotes}
\begin{table}

this is a test table

\end{table}
\end{savenotes}

\section{Conclusion}

%------------------------------------%

\end{spacing}
\end{document} 

Thanks Harish,

The first problem which I mentioned is that for some reason: My TeX file shows incompatibility error with titlesec package.

Here is the code:

\documentclass[b5paper,twoside,10pt,openany]{book}
%\setlength{\headheight}{15pt}
\usepackage[margin=1in]{geometry}       % sets up the margins of the paper
\usepackage[english]{babel}
\usepackage{charter}
\usepackage[T1]{fontenc}
\usepackage[bitstream-charter]{mathdesign}
\usepackage{amsmath}
\usepackage{sectsty}
\usepackage{titleps}            
\usepackage[english]{babel}
\usepackage{microtype}          
\usepackage{natbib}             
\usepackage{fancyhdr}           
\usepackage{footnote}           
\usepackage{graphicx}           
\usepackage{setspace}           
\usepackage{ragged2e}
\usepackage{lscape}             
\usepackage{threeparttable}     
\usepackage{booktabs}
\usepackage{caption}            
\captionsetup[table]{skip=10pt}
\usepackage[normalem]{ulem}     
\providecommand\phantomsection{}
\setcounter{secnumdepth}{3}
\usepackage{tabu}
\usepackage[explicit]{titlesec}

\begin{document}

this is a test document

\section{test 1}

\section{test 2}

\end{document}

Best Answer

You can modify the code given at this answer.

\documentclass{book}
\usepackage[explicit]{titlesec}
\usepackage{tikz}
%\usetikzlibrary{shapes.misc,arrows}
%defining subsection titles
\newcommand\titlebar{%
\tikz[baseline,trim left=0cm,trim right=3cm] {
    \node [
        fill=black!90,
        text = white,
        anchor= base east,
        minimum height=3.5ex] (a) at (3cm,0) {
        \textbf{\arabic{chapter}.\thesection}
    };   
}%
}
\titleformat{\section}{\large\sf}{\titlebar}{0.25cm}{\textcolor{black}{#1}}      %% Change color if needed.
\titlespacing*{\section}{-2cm}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\renewcommand*{\thesection}{\arabic{section}}

%defining subsection titles
\newcommand\subtitlebar{%
\tikz[baseline,trim left=0cm,trim right=3cm] {
    \node [
        fill=black!90,
        text = white,
        anchor= base east,
        minimum height=3.5ex] (b) at (3cm,0) {
        \textbf{\arabic{chapter}.\arabic{section}.\thesubsection}
    };
}%
}

\titleformat{\subsection}{\normalfont\sf}{\subtitlebar}{0.2cm}{\textcolor{black}{#1}}  %% Change color if needed.
\titlespacing*{\subsection}{-1.8cm}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\renewcommand*{\thesubsection}{\arabic{subsection}}


\begin{document}
\chapter{Some chapter}
Some text here
\section{Nucleic Acids}
Some text here
\subsection{Nucleotides}
Some text here
\end{document}

enter image description here

Change the dimensions and colours as per your taste.

Edit

You are loading too many packages and some of them almost twice (charter). The conflict is between titleps and titlesec. If I comment \usepackage{titleps} the conflict goes.

\documentclass[b5paper,twoside,10pt,openany]{book} %
\setlength{\headheight}{15pt} \usepackage[margin=1in]{geometry} % sets up the margins of the paper
\usepackage[english]{babel} 
%\usepackage{charter}  %% why to load two times?
\usepackage[T1]{fontenc} 
\usepackage[bitstream-charter]{mathdesign} 
\usepackage{amsmath} 
\usepackage{sectsty} 
%\usepackage{titleps}
\usepackage[english]{babel} 
\usepackage{microtype}
\usepackage{natbib}
\usepackage{fancyhdr}
\usepackage{footnote}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage{ragged2e} 
\usepackage{lscape}
\usepackage{threeparttable}
\usepackage{booktabs} 
\usepackage{caption}
\captionsetup[table]{skip=10pt} 
\usepackage[normalem]{ulem}
\providecommand\phantomsection{} \setcounter{secnumdepth}{3} 
\usepackage{tabu} 
\usepackage[explicit]{titlesec}
\usepackage{tikz}
%\usetikzlibrary{shapes.misc,arrows}
%defining subsection titles
\newcommand\titlebar{%
\tikz[baseline,trim left=0cm,trim right=3cm] {
    \node [
        fill=black!90,
        text = white,
        anchor= base east,
        minimum height=3.5ex] (a) at (3cm,0) {
        \textbf{\arabic{chapter}.\thesection}
    };
}%
}
\titleformat{\section}{\large\sf}{\titlebar}{0.25cm}{\textcolor{black}{#1}}      %% Change color if needed and remove \sf.
\titlespacing*{\section}{-2cm}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\renewcommand*{\thesection}{\arabic{section}}

%defining subsection titles
\newcommand\subtitlebar{%
\tikz[baseline,trim left=0cm,trim right=3cm] {
    \node [
        fill=black!90,
        text = white,
        anchor= base east,
        minimum height=3.5ex] (b) at (3cm,0) {
        \textbf{\arabic{chapter}.\arabic{section}.\thesubsection}
    };
}%
}

\titleformat{\subsection}{\normalfont\sf}{\subtitlebar}{0.2cm}{\textcolor{black}{#1}}  %% Change color if needed and remove \sf.
\titlespacing*{\subsection}{-1.8cm}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\renewcommand*{\thesubsection}{\arabic{subsection}}


\begin{document}
\chapter{Some chapter}
this is a test document

\section{test 1}

\section{test 2}

\end{document}

enter image description here

Also remove \sf from \titleformat{\section}.. if you like.

Related Question