3
%---------------------------------------------------------------------------------------------------------- %
\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}
3
  • ...also, don't write your entire question in the title and only post code blocks. Write a succinct title with some description in the question body. Your MWE is appreciated though.
    – Werner
    Commented Feb 5, 2015 at 19:54
  • What changes exactly do you want to make? Change the color for the rules used? Use a background color inside the frame? Please describe your intent as exactly as possible. Commented Feb 5, 2015 at 20:03
  • Specifically as you say: line color and weight; shading color. But more interesting would be "what is the logic to determine how to go about things like this?"
    – keith77777
    Commented Feb 5, 2015 at 20:10

3 Answers 3

7

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

2

Loading the framed package, I defined a frshaded environment that does the job. It can be parametrised, as you can see from the preamble, with the shadecolor and framecolor keys, and the \FrameRule and \FrameSep lengths:

\documentclass[11pt,a4paper,reqno,fleqn,x11names]{book} %
\usepackage[showframe]{geometry}
\usepackage{amsthm,amsmath,amsfonts} %
\usepackage{framed}
\usepackage[explicit]{titlesec} %
\usepackage[dotinlabels]{titletoc} % more advanced but essential here
\usepackage[english]{babel} %
\usepackage{xcolor}
\colorlet{shadecolor}{Thistle3!20!}
\colorlet{framecolor}{PaleVioletRed3}
\definecolor{darkblue}{rgb}{0,0.08,0.45}
\setlength\FrameRule{2pt}
\setlength\FrameSep{5pt}
\newenvironment{frshaded}{%
\def\FrameCommand{\fboxrule=\FrameRule\fboxsep=\FrameSep \fcolorbox{framecolor}{shadecolor}}%
\MakeFramed {\advance\hsize-\width \FrameRestore}}%
{\endMakeFramed}

\titleformat{\section} %
{\bfseries\itshape\fontsize{12.8}{14}\selectfont\color{Thistle4!80!}}
        {}{8pt}
{\begin{frshaded}\thesection\hskip 0.7em#1\end{frshaded}}

\begin{document}

\chapter{This is Chapter No 1}

\section{A titlesec section in a frame}

\end{document} 

enter image description here

2

You can use fcolorbox:

\newlength{\mylen}
\settowidth{\mylen}{\hspace{\fboxsep}\thesection~}%

\titleformat{\section}%[frame] %
{\bfseries\itshape\fontsize{12.8}{14}\selectfont}
        {}{0pt}
{\fboxrule2pt\noindent\fcolorbox{red}{olive}{%
\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
{\hangindent=\mylen\hangafter=1 \thesection~#1}}}

which gives

enter image description here

Full code:

%-----------------------------------------------------------------------------------%
\documentclass[11pt,a4paper,reqno,fleqn]{book} %
%---------------------------------------------------------------------------------- %
%
\usepackage[x11names]{xcolor}
\usepackage{amsthm,amsmath,amsfonts,showframe} %
\usepackage[explicit]{titlesec} %
%\usepackage[dotinlabels]{titletoc} % more advanced but essential here
%\usepackage[english]{babel} %
\newlength{\mylen}
\settowidth{\mylen}{\hspace{\fboxsep}\thesection~}%

\titleformat{\section}%[frame] %
{\bfseries\itshape\fontsize{12.8}{14}\selectfont}
        {}{0pt}
{\fboxrule2pt\noindent\fcolorbox{red}{olive}{%
\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule\relax}
{\hangindent=\mylen\hangafter=1 \thesection~#1}}}

\begin{document}

\chapter{This is Chapter No 1}

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

\end{document}
5
  • Many thanks to all three of you: Gonzalo Medina, Bernard and Harish Kumar. This has bugged me for a long time. I was not aware that changing parameters globally would affect the set-up of the frame inside of the formatting command. Thanks. again.
    – keith77777
    Commented Feb 6, 2015 at 14:12
  • 1
    @user24039 You can upvote atleast ;-)
    – user11232
    Commented Feb 6, 2015 at 14:14
  • Sorry, not clear what upvote means.
    – keith77777
    Commented Feb 7, 2015 at 17:36
  • @user24039: Thanks are conveyed better in this site by up voting and accepting the answers. You know accepting already (press the fade check mark). Up voting means pressing the upward pointing (nib like) arrow above that check mark.
    – user11232
    Commented Feb 7, 2015 at 23:09
  • Up-voted both remaining answers.
    – keith77777
    Commented Feb 9, 2015 at 11:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .