7

How would you implement a margin-to-margin gradient using TikZ that is slightly larger than the text and that cover the whole lines of section text? The following code produced the results shown below

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

the resulting image

The desired output should be like this enter image description here

2

1 Answer 1

10

Here's one option; the eventual number and the title are placed inside a node with the desired horizontal shading:

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

enter image description here

1
  • 1
    @DrAdel You're welcome! Don't forget that you can accept the answer by clicking the checkmark to its left. In case of doubt, please see How do you accept an answer?. Aug 7, 2013 at 18:28

You must log in to answer this question.

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