2

I want to use different colors for section number and section name in \documentclass{book} using sectsty package. The section number must be put in color box. Give me an idea.

  • 1
    Idea: you give us some code showing how you are trying to use the package to do this, and we'll try to help with any problems you're having. Note that, as currently stated, your question may be unanswerable since you require the solution to use a particular package which may or may not support the functionality you desire. A minimal example should make clear why you are so hung up on that particular package and you should clarify whether you are really not open to alternative solutions, no matter how brilliant they may be. – cfr Sep 19 '15 at 16:23
4

I think sectsty is not well suited for this task. I'd suggest you the titlesec package instead. A little example (adjust the colors according to your liking):

\documentclass{book}
\usepackage{titlesec}
\usepackage{xcolor}

\colorlet{sectitlecolor}{red!60!black}
\colorlet{sectboxcolor}{cyan!30}
\colorlet{secnumcolor}{orange}

\titleformat{\section}
  {\normalfont\Large\bfseries\color{sectitlecolor}}{\colorbox{sectboxcolor}{\textcolor{secnumcolor}{\thesection}}}{1em}{}

\begin{document}

\chapter{A test chapter}
\section{A test section}

\end{document}

enter image description here

In a comment it was requested to move the section number to the left margin:

\documentclass{book}
\usepackage{titlesec}
\usepackage{xcolor}

\colorlet{sectitlecolor}{red!60!black}
\colorlet{sectboxcolor}{cyan!30}
\colorlet{secnumcolor}{orange}

\titleformat{\section}
  {\normalfont\Large\bfseries\color{sectitlecolor}}{\llap{\makebox[3em][l]{\colorbox{sectboxcolor}{\textcolor{secnumcolor}{\thesection}}}}}{0em}{}

\begin{document}

\chapter{A test chapter}
\section{A test section}

\end{document}
  • Can we typeset the number of each section in the left margin as using \makeatletter %typeset the number of each section in the left margin \def\@seccntformat#1{\protect\makebox[0pt][r]{\csname the#1\endcsname\quad}} \makeatother in sectsty – SG Sekaran Sep 19 '15 at 16:52
  • @SGSekaran Yes, we can. Please, next time mention all the requirements from the beginning, it's not nice to be altering answers every time a new modification is requested. – Gonzalo Medina Sep 19 '15 at 17:02
  • @SGSekaran Please see my updated answer. – Gonzalo Medina Sep 19 '15 at 17:05
3

Yes, you can do it with sectsty, but you need to change the definition of \@seccntformat, which is responsible for printing the section numbers.

Note that this will color the same also subsection and subsubsection titles; it's of course possible to define different coloring or boxing for them.

I used the same setting as Gonzalo.

\documentclass{book}

\usepackage{sectsty,xcolor}

\colorlet{sectitlecolor}{red!60!black}
\colorlet{sectboxcolor}{cyan!30}
\colorlet{secnumcolor}{orange}

\sectionfont{\color{sectitlecolor}}

\makeatletter
\renewcommand\@seccntformat[1]{%
  \colorbox{sectboxcolor}{\textcolor{secnumcolor}{\csname the#1\endcsname}}%
  \quad
}
\makeatother

\begin{document}

\chapter{A test chapter}
\section{A test section}

\end{document}

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.