%---------------------------------------------------------------------------------------------------------- %
\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}
答案1
框架的默认宽度由\fboxrule
(形状的代码frame
可以在文件中找到frame.tts
)给出,因此改变这个长度可以控制规则的宽度。
\color
您可以通过在适当的位置插入命令来更改规则颜色(和字体颜色)\titleformat
(参见下面的第一个示例)。
如需进一步自定义(例如添加阴影或颜色背景),我建议您使用tcolorbox
包(见下面的例子):
\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}
答案2
加载framed
包后,我定义了一个frshaded
可以完成这项工作的环境。正如您在序言中看到的那样,它可以用shadecolor
和framecolor
键以及和\FrameRule
长度进行参数化\FrameSep
:
\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}
答案3
您可以使用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}}}
这使
完整代码:
%-----------------------------------------------------------------------------------%
\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}