\documentclass{book}
\usepackage{titlesec}
\usepackage{color}
\definecolor{darkblue}{rgb}{0,0.08,0.45}
\titleformat{\chapter}[frame] % was display
{\normalfont\Large\sffamily\bfseries}
{\vspace*{-2cm}
\leavevmode\leaders\vrule height7pt width3pt depth0pt%
\hfill\kern1pt\chaptername\hspace{3.6pt}\thechapter\kern8pt%
\leaders\vrule height7pt width3pt depth0pt\hfill}
{8pt} % was 3pt
{\vspace*{-5pt}\hrule\vspace{6pt}}
[\vspace{2pt}\hrule\vspace{1cm}] % draws line under box
\newcommand\Bheadfont{\fontsize{12pt}{\baselineskip}\selectfont}
\titleformat{\section}[frame] % was hang
{\normalfont\sc\color{darkblue}\Large}
{\thesection\hskip0.618em\Bheadfont}
{3pt}{}[]
\titleformat{\subsection}[frame] %
{\normalfont\sc\color{darkblue}}
{\thesubsection\hskip0.618em}
{3pt}{}[]
\begin{document}
\chapter{This is a framed chapter}
\section{This is a framed section}
\subsection{This is a framed sub section}
\end{document}
标题hang
给出了这种结果:
2.3 - 解线性方程
我想使用框架,但保持数字和文本在线。锦上添花的是彩色框架和彩色填充;这可能吗?如果可能的话,我想\fcolorbox
避免\fbox
。
答案1
您可以使用framed
包来实现这一点。由于它仅定义框架或阴影(但不是两者)环境,因此我定义了一个frshaded*
环境,您可以轻松自定义它,选择框架宽度、框架与内容的分离、框架颜色和填充颜色。以下是一个例子:
\documentclass{book}
\usepackage[x11names]{xcolor}
\usepackage[explicit]{titlesec}
\usepackage{framed}
\colorlet{shadecolor}{Thistle3!30!}
\colorlet{framecolor}{PaleVioletRed3}
\definecolor{darkblue}{rgb}{0,0.08,0.45}
\newenvironment{frshaded*}{%
\def\FrameCommand{\fboxrule=\FrameRule\fboxsep=\FrameSep \fcolorbox{framecolor}{shadecolor}}%
\MakeFramed {\advance\hsize-\width \FrameRestore}}%
{\endMakeFramed}
\titleformat{\chapter}[hang] %
{\color{PaleVioletRed4}\normalfont\Large\sffamily\bfseries\FrameSep 20pt\FrameRule=3pt}
{}
{0pt} % was 3pt
{\begin{frshaded*}\chaptername\hspace{3.6pt}\thechapter\kern8pt#1\end{frshaded*}\vskip-0.8ex\color{framecolor} \titlerule[1.2pt]}
\newcommand\Bheadfont{\fontsize{12pt}{\baselineskip}\selectfont}
\titleformat{\section}[hang] %
{\normalfont\scshape\color{darkblue}\Large\colorlet{shadecolor}{LightSteelBlue2!25!}\colorlet{framecolor}{darkblue} \FrameRule=1pt}
{}
{0pt}{\begin{frshaded*}\thesection\hskip0.618em\Bheadfont#1\end{frshaded*}}
\titleformat{\subsection}[hang] %
{\normalfont\scshape\color{darkblue}\colorlet{shadecolor}{LightSteelBlue2!25!}\colorlet{framecolor}{darkblue} \FrameRule=1pt\FrameSep=6pt}
{}
{0pt}{\begin{frshaded*}\thesubsection\hskip0.618em#1\end{frshaded*}}
\begin{document}
\chapter{This is a framed chapter}
\section{This is a framed section}
\subsection{This is a framed sub section}
\end{document}