[Tex/LaTex] How does one change the frame size and colors in the frame option of titlesec as in MWE

sectioning

%---------------------------------------------------------------------------------------------------------- %
\documentclass[11pt,a4paper,reqno,fleqn,xcolor=x11names]{book} %
%---------------------------------------------------------------------------------------------------------- %
%
\usepackage{amsthm,amsmath,amsfonts} %
\usepackage{titlesec} %
\usepackage[dotinlabels]{titletoc} % more advanced but essential here
\usepackage[english]{babel} %


\titleformat{\section}[frame] %
{\bfseries\itshape\fontsize{12.8}{14}\selectfont}
        {}{8pt}
{\;\;\thesection\hskip 0.7em}

\begin{document}

\chapter{This is Chapter No 1}

\section{A titlesec section in a frame - too tall and not shaded}

\end{document}

Best Answer

The default width of the frame is given by \fboxrule (the code for the frame shape can be found in the file frame.tts), so changing this length you can control the rule width.

You can change the rule color (and the font color) by inserting \color commands at appropriate locations in \titleformat (see first example below).

For further customizations (such as adding a shading or a color background), I'd suggest you to use the tcolorbox package (see examples below):

\documentclass{book}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\usepackage[many]{tcolorbox}
\usepackage[english]{babel} 

\titleformat{\section}[frame] 
  {\setlength\fboxrule{3pt}\bfseries\itshape\fontsize{12.8}{14}\selectfont\color{red}}
  {}{5pt}
  {\color{black}$\;\;$\thesection\hskip 0.7em#1}

\begin{document}

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

\titleformat{\section}
  {\normalfont\bfseries\itshape\fontsize{12.8}{14}\selectfont}
  {}{0pt}
  {\begin{tcolorbox}[]\thesection\hskip0.7em#1\end{tcolorbox}}
\section{Another test section}

\titleformat{\section}
  {\normalfont\bfseries\itshape\fontsize{12.8}{14}\selectfont}
  {}{0pt}
  {\begin{tcolorbox}[
      enhanced,
      boxrule=0pt,
      arc=0pt,
      outer arc=0pt,
      interior code={\fill[overlay,cyan!30] (frame.north west) rectangle (frame.south east);},
    ]\thesection\hskip0.7em#1\end{tcolorbox}}
\section{Yet another test section}

\titleformat{\section}
  {\normalfont\bfseries\itshape\fontsize{12.8}{14}\selectfont}
  {}{0pt}
  {\begin{tcolorbox}[
      enhanced,
      boxrule=0pt,
      arc=0pt,
      outer arc=0pt,
      interior code={\shade[draw=black,left color=cyan!30,right color=orange!30] (frame.north west) rectangle (frame.south east);},
    ]\thesection\hskip0.7em#1\end{tcolorbox}}
\section{Yet another test section}

\end{document}

enter image description here