为什么框架和颜色填充没有正确垂直对齐?

为什么框架和颜色填充没有正确垂直对齐?
%
%---------------------------------------------------------------------------------------------------------- %
\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}

答案1

这太丑了,你应该用某种tikz方式来做这样的事情。如果你有兴趣使用tikz和,请告诉我titlesec。无论如何,这应该能给你一些想法。

除了使用frame选项titlesec,您还可以使用fcolorbox。这样不会换行,如果您需要换行,请使用\parbox内部的 。

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

在此处输入图片描述

相关内容