2
%
%---------------------------------------------------------------------------------------------------------- %
\documentclass[11pt,a4paper,reqno,fleqn]{amsbook} %
%---------------------------------------------------------------------------------------------------------- %
%
\usepackage[dotinlabels]{titletoc} % more advanced but essential here
\usepackage[explicit]{titlesec} %
\usepackage[english]{babel} %
\usepackage{mathptmx} % place after txfonts
\usepackage[automark]{scrpage2}
\usepackage{textpos}
\usepackage{blindtext}
\usepackage[bookmarks=true]{hyperref} % For creating hyperlinks in cross references
\usepackage{setspace}
\usepackage{ulem} % for underlining - keep
\let\chead\undefined % un-define
\let\cfoot\undefined % un-define
\usepackage[Lenny]{fncychap} %
\usepackage{fancyhdr} %
\usepackage{xcolor,colortbl} % define colors, color table, order important
\usepackage[flequ]{mathtools} % necessary
\usepackage[position=below,labelfont={bf},textfont=normal]{caption,subfig} % 08_03_2014 (subfig from package)
\usepackage{imakeidx}

\special{papersize=210mm,297mm} % printer choice

% ----------- lengths ------------------------------------------------------------------------------- %
\setlength{\paperwidth}{175mm} % width of book [165mm] - set in geometry
\setlength{\textwidth}{135mm} % width of text [127mm]

\setlength{\paperheight}{235mm} % height of book [205mm]
\setlength{\topmargin}{-22mm} % clear above header [-22mm]
\setlength{\headheight}{16mm} % height of head [15mm]
\setlength{\headsep}{4mm} % between header and text [3mm]
\setlength{\textheight}{195mm} % height of text inclusive of header/footer [172]

% ------------ paragraphs --------------------------------------------------------------------------- %

\setlength{\parskip}{3.4pt} % gap between paragraphs [3pt]
\setlength{\parindent}{0pt} % indentation of paragraphs

\makeatletter % only needed in preamble
\renewcommand\small{\@setfontsize\small{10}{11}}
\renewcommand\normalsize{\@setfontsize\normalsize{11}{13}}
\renewcommand\large{\@setfontsize\large{12.2}{14}}
\renewcommand\Large{\@setfontsize\Large{14.4}{16}}
\makeatother

\renewcommand{\ULthickness}{1.0pt}

\def\mymediumspace{\kern 0.4pt}
\def\mythickspace{\kern 0.6pt}
\def\mygraphicspace{\kern 0.75pt}
\def\comma{\kern 0.4pt}
\def\figtab{\kern 0.7pt} % layout of fig/table numbering - good 22_02_2014

% ------------------------------------- COUNTERS TO SET GLOBALLY ------------------------------------ %

\setcounter{secnumdepth}{4} %
\setcounter{section}{1} % was 1
\setcounter{tocdepth}{2} % depth in contents lists
\setcounter{equation}{2}

\setlength{\linewidth}{\textwidth}

\newcommand\Hrule{\noindent\rule{\linewidth}{2.5pt}}

\setlength{\fboxsep}{3pt} %
\setlength{\fboxrule}{0.7pt} %

\makeatletter
\titlelabel{\thetitle\enspace}
\patchcmd{\chapter}{\if@openright\cleardoublepage\else\clearpage\fi}{}{}{}
\renewcommand\tocsection[3]{\indentlabel{\hspace{0pt}#1 \hspace{0pt}#2\hspace{6.4pt}}#3} %
\renewcommand\tocsubsection[3]{\indentlabel{\hspace{0pt}#1 \hspace{0pt}#2\hspace{6.4pt}}#3} %

\renewcommand{\thechapter}{\arabic{chapter}}
\renewcommand{\thesection}{\thechapter\mythickspace.\mythickspace\arabic{section}}
\renewcommand{\thesubsection}{\thesection\mythickspace.\mythickspace\arabic{subsection}}
\makeatother

\ChNameVar{\fontsize{20}{20}\usefont{OT1}{phv}{m}{n}\selectfont} % for fancy header
\ChNumVar{\fontsize{60}{90}\usefont{OT1}{ptm}{m}{n}\selectfont}
\ChTitleVar{\Large\bfseries\rm}
\ChRuleWidth{2.5pt}

\setlength{\marginparwidth}{32mm} % 32mm
\setlength{\marginparsep}{11mm} % 7mm
\setheadwidth[0pt]{textwithmarginpar}
\setfootwidth[0pt]{head}

\newlength\widestnum
\setlength\widestnum{36pt} %


\titleformat{\section}[frame] %                                                                                               line 1
  {\bfseries\itshape\fontsize{13.6}{20}\selectfont}{}{-1pt} %                                 line 2
  {\makebox[\linewidth+2pt] % pushes fill and number to right                                     line 3
    {\colorbox{red!15} %                                                                                                                    line 4
    {\parbox{\linewidth-8pt} % -ve shortens the fill                                                          line 5
  {\parbox{\widestnum}{\hspace{7pt}\thesection} %                                                               line 6
  \parbox{\dimexpr\linewidth-\widestnum\relax} %                                                                line 7
    {\strut#1\strut}}}}} %                                                                                                              line 8

\titleformat{\subsection}[frame] %                                                                                        line 1
  {\bfseries\itshape\fontsize{12.6}{18}\selectfont}{}{-1pt} %                                 line 2
  {\makebox[\linewidth+2pt] % pushes fill and number to right                                     line 3
    {\colorbox{blue!15} %                                                                                                               line 4
    {\parbox{\linewidth-8pt} % -ve shortens the fill                                                          line 5
  {\parbox{\widestnum}{\hspace{7pt}\thesubsection} %                                                        line 6
  \parbox{\dimexpr\linewidth-\widestnum\relax} %                                                                line 7
    {\strut#1\strut}}}}} %                                                                                                              line 8

\titlespacing{\chapter}{0pt}{8pt}{6pt}[0pt] %
\titlespacing{\section}{0pt}{8pt}{6pt}[0pt]
\titlespacing{\subsection}{0pt}{8pt}{6pt}[0pt]


\begin{document}

\vspace{12pt}

\chapter{This is chapter ONE}

\section{The section frame and RED fill do not line up.}

\subsection{The subsection frame and BLUE fill do not line up.}

\end{document}
2
  • What's the purpose of using amsbook when you change all of its setup? Use book instead. And, please, avoid the Lenny chapter style, one of the worst I've ever seen.
    – egreg
    Jan 20, 2015 at 21:41
  • Thanks for your advice - I admit to being a novice at this. What are the benefits of changing over to book, now that I've settled things like theorems?
    – keith77777
    Jan 21, 2015 at 11:25

1 Answer 1

2

This is ugly and you should be using some tikz way for doing stuff like this. Let me know if you are interested in using tikz and titlesec. Anyway, this should give you some idea.

Instead of using frame option of titlesec, you can use fcolorbox. This won't break lines and if you want breaks, use a \parbox inside.

\documentclass{article}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\titleformat{\section}[block]%
     {\bfseries\itshape\fontsize{13.6}{20}\selectfont}
     {}
     {1pt}%
     {\fcolorbox{black}{red}{#1}} %
\titleformat{\subsection}[block]%
     {\bfseries\itshape\fontsize{13.6}{20}\selectfont}
     {}
     {1pt}%
     {\fcolorbox{black}{blue!40}{#1}} %
\titlespacing{\section}{0pt}{8pt}{6pt}[0pt]

\begin{document}
  \section{The section frame and RED fill do not line up.}
  Some text
  \subsection{This is a sub section}
\end{document}

enter image description here

3
  • I would be interested in using \tikz and titlesec if you have the time. I would love to have more visible control.
    – keith77777
    Jan 21, 2015 at 11:08
  • @user24039 Do you want section numbers or not? If yes, whether they should be inside the coloured rectangle or not? Some explanation will be useful. And may be you can ask a new question with some code too.
    – user11232
    Jan 21, 2015 at 12:07
  • Yes, please. and inside the frame.
    – keith77777
    Jan 22, 2015 at 12:42

You must log in to answer this question.

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