Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

example

   \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}


    \titlelabel{\llap{\thetitle\quad}}% for section headings in margins

     \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}
     \renewcommand{\headrulewidth}{0pt}
    \usepackage{amssymb}

     \parindent=0.5in
    \parskip=10pt
     \linespread{1.3}

     \renewcommand{\abstractname}{\Large Abstract} %To increase Abstract font size
     \begin{document}
     \maketitle 
     \begin{abstract}
      blahblah
     \end{abstract}
     blah blah
     \end{document}
share|improve this question
    
Welcome to TeX.sx! –  Peter Jansson Mar 3 '13 at 11:05
add comment

3 Answers

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}}

\renewcommand\section{\@ifstar{\myheading}{\mysection}}

\newcommand\mysection[1]{%
  \sectprelude{section}{\thesectiontext}\origsection{#1}}
\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

\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}

enter image description here

share|improve this answer
    
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
add comment

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{}
\fancyhead[RO]{\helv\rightmark\hspace{0.5em}\graysquare\hspace{0.5em}\thepage}
\fancyhead[LE]{\helv\thepage\hspace{0.5em}\graysquare\hspace{0.5em}\leftmark}
\renewcommand{\headrulewidth}{0pt}

\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:

enter image description here

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.

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

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

enter image description here

\documentclass{scrbook}

\usepackage{xcolor}
\usepackage{lipsum}

\definecolor{sectcolor}{rgb}{0.9,0.9,0.9}
\renewcommand*{\othersectionlevelsformat}[3]{%
  \llap{\colorbox{sectcolor}{\makebox[2.5em][r]{#3\autodot}}\quad}%
}

\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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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