[Tex/LaTex] Fancy Section headers

sectioningtikz-pgf

I wanted to add the lines from the edge of the left margin to the colored box in my section headers so this it looks like:

enter image description here

Here is the code I have thus far:

\documentclass[11pt]{book}
    \usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,headsep=10pt,letterpaper]{geometry}
    \usepackage[demo]{graphicx}
    \usepackage{lipsum}
    \usepackage{xcolor}
    \definecolor{mybluei}{RGB}{52,177,201}
    \definecolor{myblueii}{RGB}{63,200,244}
    \definecolor{myblueiii}{RGB}{199,234,253}
    \definecolor{ultramarine}{RGB}{0,45,97}
    \usepackage{tikz}
    \usetikzlibrary{shadows.blur}
    \usetikzlibrary{shapes.symbols}
    \usepackage{etoolbox,fancyhdr}
    \usepackage[explicit,calcwidth]{titlesec}

    \makeatletter
    \newcommand{\thechapterimage}{}%
    \newcommand{\chapterimage}[1]{\renewcommand{\thechapterimage}{#1}}%
    \def\@makechapterhead#1{%
    {\parindent \z@ \raggedright \normalfont
    \ifnum \c@secnumdepth >\m@ne
    \if@mainmatter
    \fi\fi\par\vspace*{120\p@}\stepcounter{chapshift}}}
    \makeatother

    \newcommand\SectionFont{\usefont{T1}{qhv}{b}{n}\selectfont}
    \titleformat{\section}
      {\normalfont\SectionFont\color{myblueii}}
      {\colorbox{mybluei}{%
        \parbox[c][16pt][c]{40pt}{%
          \centering\textcolor{white}{\SectionFont\Large\thesection}%
          }%
        }%
      }
      {1em}
      {#1}
      [\vspace{-1.2\baselineskip}%
        \color{myblueii}\hspace*{\dimexpr40pt+2\fboxsep\relax}%
        \rule{\dimexpr\textwidth-40pt-2\fboxsep\relax}{1pt}%
      ]

    \usetikzlibrary{calc}
    \pagestyle{plain}
    \newcounter{chapshift}
    \addtocounter{chapshift}{-1}
    \newcommand\BoxColor{%
      ultramarine}

    \def\subsectiontitle{}
    \renewcommand{\sectionmark}[1]{\markright{\sffamily\normalsize#1}{}}
    \renewcommand{\subsectionmark}[1]{\def\subsectiontitle{#1}}
    \pagestyle{fancy}
    \renewcommand{\headrule}{{\color{myblueii}%
    \hrule width\headwidth height\headrulewidth depth\headrulewidth}}
    \fancyhf{} \fancyhead[LE,RO]{\normalsize\colorbox{myblueii}{\color{white}\sffamily\bfseries\strut\quad\thepage\quad}}
    \fancyhead[LO]{\textcolor{mybluei} \rightmark%
    \begin{tikzpicture}[overlay,remember picture]
      \node[fill=\BoxColor,inner sep=0pt,rectangle,text width=1cm,
        text height=4cm,align=center,anchor=north east]
      at ($ (current page.north east) + (-0cm,-2*\thechapshift cm) $)
      {\rotatebox{90}{\parbox{4cm}{%
       \centering\textcolor{white}{\bfseries\scshape\rightmark \\ \sffamily\subsectiontitle}}}};
      \end{tikzpicture}}
    \fancyhead[RE]{Testing Book%
      \begin{tikzpicture}[overlay,remember picture]
      \node[fill=\BoxColor,inner sep=0pt,rectangle,text width=1cm,
        text height=4cm,align=center,anchor=north west]
      at ($ (current page.north west) + (-0cm,-2*\thechapshift cm) $)
      {\rotatebox{90}{\parbox{4cm}{%
        \centering\textcolor{white}{\bfseries\scshape\rightmark \\ \sffamily\subsectiontitle}}}};
      \end{tikzpicture}} 

\makeatother

\begin{document}
\chapterimage{bishade}
\chapter{Testing}
\section{Test One}
\lipsum[1-2]
\section{Test Two}
\lipsum[1-2]
\chapter{Testing Three}
\section{Test Four}
\lipsum[1-2]
\section{Test Two}
\lipsum[1]
\newpage
\section{Test Two}
\lipsum[1]
\end{document}

Best Answer

It's easier if you replace the command with a node, so you can use that to draw everything else. The line that goes over the text width is kind of manual for now. Meaning that it spans the text width and then the length of the node, plus the inner sep, is removed from that value.

Output

enter image description here

Code

    \documentclass[11pt]{book}
    \usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,headsep=10pt,letterpaper]{geometry}
    \usepackage[utf8]{inputenc}
    \usepackage[demo]{graphicx}
    \usepackage{lipsum}
    \usepackage{xcolor}
    \definecolor{mybluei}{RGB}{52,177,201}
    \definecolor{myblueii}{RGB}{63,200,244}
    \definecolor{myblueiii}{RGB}{199,234,253}
    \definecolor{ultramarine}{RGB}{0,45,97}
    \usepackage{tikz}
    \usetikzlibrary{shadows.blur}
    \usetikzlibrary{shapes.symbols}
    \usepackage{etoolbox,fancyhdr}
    \usepackage[explicit,calcwidth]{titlesec}

    \makeatletter
    \newcommand{\thechapterimage}{}%
    \newcommand{\chapterimage}[1]{\renewcommand{\thechapterimage}{#1}}%
    \def\@makechapterhead#1{%
    {\parindent \z@ \raggedright \normalfont
    \ifnum \c@secnumdepth >\m@ne
    \if@mainmatter
    \fi\fi\par\vspace*{120\p@}\stepcounter{chapshift}}}
    \makeatother

    \newcommand\SectionFont{\usefont{T1}{qhv}{b}{n}\selectfont}
    \titleformat{\section}
      {\normalfont\SectionFont\color{myblueii}}
      {\tikz[baseline=(a.base), overlay]{
            \node[fill=mybluei, anchor=south west, outer sep=0, draw=none, inner sep=.5mm, 
                text=white,font=\Large, text width=1.5cm, align=center, minimum height=8.5mm,
                label={[text=myblueii]right:#1}
            ] (a) {\thesection};
        \foreach \x in {.25,.5,.75}{%
        \draw[mybluei, ultra thick] (a) ($(a.south west)!\x!(a.north west)$) --++ (-4,0);
        }
        \draw[myblueii, ultra thick] ($(a.south east)!.25!(a.north east)$) --++ (\textwidth-1.6cm,0);
        }
      }
      {1em}
      {}

    \usetikzlibrary{calc}
    \pagestyle{plain}
    \newcounter{chapshift}
    \addtocounter{chapshift}{-1}
    \newcommand\BoxColor{%
      ultramarine}

    \def\subsectiontitle{}
    \renewcommand{\sectionmark}[1]{\markright{\sffamily\normalsize#1}{}}
    \renewcommand{\subsectionmark}[1]{\def\subsectiontitle{#1}}
    \pagestyle{fancy}
    \renewcommand{\headrule}{{\color{myblueii}%
    \hrule width\headwidth height\headrulewidth depth\headrulewidth}}
    \fancyhf{} \fancyhead[LE,RO]{
        \normalsize\colorbox{myblueii}{\color{white}\sffamily\bfseries\strut\quad\thepage\quad}
        }
    \fancyhead[LO]{\textcolor{mybluei} \rightmark%
    \begin{tikzpicture}[overlay,remember picture]
      \node[fill=\BoxColor,inner sep=0pt,rectangle,text width=1cm,
        text height=4cm,align=center,anchor=north east]
      at ($ (current page.north east) + (-0cm,-2*\thechapshift cm) $)
      {\rotatebox{90}{\parbox{4cm}{%
       \centering\textcolor{white}{\bfseries\scshape\rightmark \\ \sffamily\subsectiontitle}}}};    
      \end{tikzpicture}}
    \fancyhead[RE]{Testing Book%
      \begin{tikzpicture}[overlay,remember picture]
      \node[fill=\BoxColor,inner sep=0pt,rectangle,text width=1cm,
        text height=4cm,align=center,anchor=north west]
      at ($ (current page.north west) + (-0cm,-2*\thechapshift cm) $)
      {\rotatebox{90}{\parbox{4cm}{%
        \centering\textcolor{white}{\bfseries\scshape\rightmark \\ \sffamily\subsectiontitle}}}};
      \end{tikzpicture}} 

\makeatother

\setlength{\headheight}{14pt}

\begin{document}
\chapterimage{bishade}
\chapter{Testing}
\section{Test One}
\lipsum[1-2]
\section{Test Two}
\lipsum[1-2]
\chapter{Testing Three}
\section{Test Four}
\lipsum[1-2]
\section{Test Two}
\lipsum[1]
\newpage
\section{Test Two}
\lipsum[1]
\end{document}