如何在各个部分周围绘制渐变框

如何在各个部分周围绘制渐变框

如何使用TikZ比文本稍大并覆盖整个部分文本行的边距到边距渐变来实现边距渐变?以下代码产生了如下所示的结果

\documentclass[a4paper,svgnames,10pt]{book}
\usepackage[utf8x]{inputenc}
\usepackage{tikz}
\usepackage[explicit]{titlesec}
\usepackage{xcolor}
\usepackage{colortbl}
\usepackage[margin=1.5cm]{geometry}
\usepackage{lipsum}
\usepackage{calc}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
\usepackage[cm-default]{fontspec}
\setromanfont{FreeSerif}
\setsansfont{FreeSans}
\setmonofont{FreeMono}
\usepackage{xgreek}
\setmainfont{Arial}

%%%%%%%%%%%%%%%%%%%% fancy heading  \section %%%%%%%%%%%%%%%%%%%%%%

\titleformat{\section}[block]%              
    {\huge\bfseries%
     \tikz[overlay] \shade[left color=LightSkyBlue,right color=white,] (0,-1ex) rectangle (\textwidth,1em);}%    
    {\thesection}%                   
    {1em}%
    {#1}

%%%%%%%%%%%%%%%%%% fancy heading  \chapter %%%%%%%%%%%%%%%%%%%%%%%%

\newcommand*\chapterlabel{}
\titleformat{\chapter}
{\gdef\chapterlabel{}
  \normalfont\sffamily\Huge\bfseries\scshape}
{\gdef\chapterlabel{\thechapter\ }}{0pt}
{\begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-3cm] at (current page.north west)
    {\begin{tikzpicture}[remember picture, overlay]
        \draw[fill=LightSkyBlue] (0,0) rectangle
        (\paperwidth,3cm);
        \node[anchor=east,xshift=.9\paperwidth,rectangle,
        rounded corners=20pt,inner sep=11pt,
        fill=MidnightBlue]
        {\color{white}\chapterlabel#1};
      \end{tikzpicture}
    };
  \end{tikzpicture}
}
\titlespacing*{\chapter}{0pt}{50pt}{0pt}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\part{Developmental Disturbance of Oral and Paraoral tissues}

\chapter{Soso and Mimi}

\section{Toto}
\lipsum[1]

\section{Developmental Disturbance of Oral and Paraoral tissues and Related Structures}
\lipsum[1]

\section{Lolo}
\lipsum[1]

\end{document}

生成的图像

期望的输出应该是这样的 在此处输入图片描述

答案1

这是一个选项;最终的数字和标题放在具有所需水平阴影的节点内:

\documentclass[a4paper,svgnames,10pt]{book}
\usepackage[utf8x]{inputenc}
\usepackage{tikz}
\usepackage{varwidth}
\usepackage{linegoal}
\usepackage[explicit]{titlesec}
\usepackage[margin=1.5cm]{geometry}
\usepackage{lipsum}

\definecolor{myblue}{RGB}{158,158,255}

\titleformat{\section}
  {\normalfont\Large\bfseries}{}{0em}
  {%
  \begin{tikzpicture}
  \node[inner xsep=0pt,text width=\textwidth,
    align=left,left color=myblue,right color=myblue!10] 
    {\begin{varwidth}[t]{5em}\thesection\hfill\end{varwidth}\hspace{0.5em}\parbox[t]{\linegoal}{\raggedright #1}};
  \end{tikzpicture}%
  }
\titleformat{name=\section,numberless}
  {\normalfont\Large\bfseries}{}{0em}
  {%
  \begin{tikzpicture}
  \node[inner xsep=0pt,text width=\textwidth,
    align=left,left color=myblue,right color=myblue!10] 
    {\parbox[t]{\linewidth}{\raggedright#1}};
  \end{tikzpicture}%
  }

\pagestyle{plain}

\begin{document}

\chapter{Test chapter}
\section{A test numbered section with a really long title spanning several lines just for the example}
\lipsum[4]
\section*{A test unnumbered section with a really long title spanning several lines just for the example}
\lipsum[4]

\end{document}

在此处输入图片描述

相关内容