Use of shaded box for section numbering only?

I have been wondering "how to get shaded box for section numbers only"? Also, How to get grey square in between left mark and page number in the header? In code below I can get solid black square. I have attached image below of the desired output. This example is from TUGINDIA tutorials.

   \documentclass[12pt,twoside]{report}
\usepackage{titlesec}%for formatting chapter position and names

\titleformat{\chapter}[display]
{\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{0pt}{\huge}
\titlespacing*{\chapter}{0pt}{-20pt}{10pt}

\title{\bf \huge xxx}
\author{xxx}
\date{}   %to disable date
\usepackage[top=1.3in, left=1.5in, right=1in]{geometry} %for setting margins
\usepackage{grffile}  %to avoid showing path of figure inserted
\includeonly{chap1/chapter_1}

\usepackage{fancyhdr}
\pagestyle{fancy}
\newcommand{\helv}{%
\fontfamily{phv}\fontseries{}\fontsize{10}{12}\selectfont}
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}%formatting chapter title
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}
\fancyhf{}
\fancyhead[RO]{\helv \rightmark{} $\blacksquare$ \thepage}
\fancyhead[LE]{\helv \thepage{} $\blacksquare$ \leftmark}
\usepackage{amssymb}

\parindent=0.5in
\parskip=10pt

\renewcommand{\abstractname}{\Large Abstract} %To increase Abstract font size
\begin{document}
\maketitle
\begin{abstract}
blahblah
\end{abstract}
blah blah
\end{document}

-
Welcome to TeX.sx! –  Peter Jansson Mar 3 '13 at 11:05

Here is a way to get the Section headers with color. However, I did not spend the time to make the margins look like the example, since your question seemed focused on the box and color aspect, rather than the margin aspect.

Also, I only implemented the effect down to the subsection level, though you could follow my lead and extend it to subsubsection if you wished.

As for your question on the gray square separator by the page number, things like that are discussed here: Page number in the margin and separator

\documentclass{article}
\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[0.5in][r]{\normalfont\large\bfseries#1}}}}

\renewcommand \thesection {\usebox{\sectbox}}
\renewcommand \thesubsection {\usebox{\sectbox}}

\newcommand\mysection[1]{%
\sectprelude{section}{\thesectiontext}\origsection{#1}}
\sectprelude{section}{\thesectiontext}\origsection*{#1}}

\renewcommand\subsection[1]{%
\sectprelude{subsection}{\thesubsectiontext}\origsubsection{#1}}

\newcommand\sectprelude[2]{%

\makeatother

\begin{document}

\section{Introduction}

It was a dark and stormy night\ldots and then, a minute passed.

\section{Discussion}

In the beginning\ldots

\section{Conclusion}

\ldots and it was good

\subsection{Postscript}

Amen.

\end{document}


-
I will note that something needs to be done further than this if you wish to use a table of contents, because it ends up getting stuffed with gray boxes. Also, I included the section* variant in my example, but realize that it prints no gray box at all, because section* doesn't print out \thesection. –  Steven B. Segletes Mar 8 '13 at 19:37

Here's an option using the titlesec package and producing the colored boxes and the desired hanging; the example below also includes the necessary settings to produce the desired headers for the document:

\documentclass{book}
\usepackage[explicit]{titlesec}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage{fancyhdr}

\colorlet{sectcolor}{gray!65}

\newcommand\graysquare{\textcolor{sectcolor}{\rule{1ex}{1ex}}}
\newcommand{\helv}{\fontfamily{phv}\fontseries{}\fontsize{10}{12}\selectfont}

\pagestyle{fancy}

\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection.\ #1}}
\fancyhf{}

\titleformat{\section}
{\normalfont\Large\bfseries}
{\llap{\colorbox{sectcolor}{\makebox[3em][r]  {\thesection}}\hspace{1em}}}
{0em}{#1}

\begin{document}

\setcounter{chapter}{1}% just for the example
\section{Introduction}
A cross-reference to the last section:~\ref{sec:conclusion}.
\lipsum[4]
\section{Discussion}
\lipsum[4]
\section{Conclusion}
\label{sec:conclusion}
\lipsum[4]

\end{document}


An image of the result:

My solution changes only the formatting for the section numbers in the document body; cross-references and numbering in ToC entries will keep the standard representation.

-
Wow - I'm only 9 sec's too slow... that's nearly exactly the same I had :) –  cgnieder Mar 8 '13 at 19:47

If you use a KOMA-class you don't need any further packages:

\documentclass{scrbook}

\usepackage{xcolor}
\usepackage{lipsum}

\definecolor{sectcolor}{rgb}{0.9,0.9,0.9}
\renewcommand*{\othersectionlevelsformat}[3]{%
}

\begin{document}
\chapter{Test Chapter}
\section{Introduction}
A cross-reference to the last section:~\ref{sec:conclusion}.
\lipsum[4]
\section{Discussion}
\lipsum[4]
\section{Conclusion}
\label{sec:conclusion}
\lipsum[4]
\end{document}

-