[Tex/LaTex] Replacing \fancyhdr with \titlesec in complicated design documents

fancyhdrtitlesec

Thes page on the picture below (from this post) has been created within \fancyhdr package, but I need to something like this keeping incompatible \titlesec usage and using twopage layout.

enter image description here

Really, I have no idea how to use tikz codes with \titlesec interface, but to save your time, I prepared the draft. It's no much, but…

\documentclass[a4paper]{article}

\usepackage{tikz}
\usepackage{geometry}
\geometry{% Please make twoside settings 
   paperwidth=210mm, paperheight=297mm,
   bindingoffset=0mm, asymmetric,
   left=13mm,  %% or inner=23mm
   right=18mm, %% or outer=18mm
   top=11mm, bottom=11mm,
   headsep=3mm,
   footskip=3mm
}

\usepackage{titletoc}
\usepackage[pagestyles]{titlesec}
\usepackage[svgnames,x11names]{xcolor}

\newpagestyle{redfancy}[]{% Please insert the settings}
\newpagestyle{bluefancy}[]{% Please insert the settings}

\begin{document}

% 1st red page
\pagestyle{redfancy} % color: Header/Footer/Margin color: Firebrick2
\sethead[][][\thesection\quad\sectiontitle]{\thesection\quad\sectiontitle}{}{}
\setfoot[\thepage][][]{}{}{\thepage}

\section{First Red section}
\subsection{Red A}
\subsubsection{Red AA}
\subsubsection{Red AB}

\newpage
% 2nd red page
\section{Second Red section}
\subsection{Red B}
\subsubsection{Red BA}
\subsubsection{Red BB}


\newpage % Blue page
\pagestyle{bluefancy} % color: Header/Footer/Margin color: DodgerBlue1
\section{Blue section}

\end{document}

Here is the sketch of desired result:

enter image description here


Update

According the requests of the people who nicely managed to answer this question, I'll explain what I need from titlesec packgage.

First is the dynamical design. During TOC, intro, outro I need the more simple design, and then – fancy color design (Red page, Blue page etc).

enter image description here

Before, I did it this way:

%\usepackage[pagestyles]{titlesec}

\newpagestyle{tocandintro}[]{\headrule\setheadrule{0.5mm}\footrule\setfootrule{0.5mm}}

\pagestyle{tocandintro}
\sethead[Table of contents][][]{}{Table of contents}{\textsf{目次}}
\setfoot[\thepage][][ABC Project]{ABC Project}{}{\thepage}

\tableofcontents

\sethead[Intro][][]{}{}{Intro}
\setfoot[\thepage][][ABC Project]{ABC Project}{}{\thepage}
<Intro text>
\pagestyle{fancy} % And after - color fancy design

Then, I need dynamical color titles, as shown on the picture below.

enter image description here

Before, I did it this way:

% --- Section
\colorlet{SectionFrameColor}{blue!50}
\colorlet{SectionFillColor}{blue!20}
\colorlet{SectionFontColor}{blue!80}

\titleformat{\section}[hang]{\Large\sffamily\color{SectionFontColor}}
{\begin{tikzpicture}[baseline={([yshift=-.8ex]current bounding box.center)}]
\node[thick,draw=SectionFrameColor,fill=SectionFillColor,rectangle,rounded corners,text=white] {\thesection};
\end{tikzpicture}}{12pt}{}%
[{\titlerule[1pt]}]

\titleformat{\paragraph}[hang]{\bfseries}{}{}{}[]
% And than re-define section color...

As you see, besides decorative needs I want to change the \paragraph format.

Best Answer

You would need to tweak this and I don't recommend it because I think the use of titlesec is going to cause more trouble than it is worth. However, if you insist:

Step 1: Initial Solution

This solution uses tikzpagenodes together with the background package. This leaves the page style commands from titlesec to just handle the content of the header. The page frame and footer content is constructed using TiKZ on the background of the page. The even odd rule ensures that only the outside of the frame is filled with colour and that a 'hole' is created to contain the page content.

\documentclass[a4paper]{article}

\usepackage[svgnames,x11names]{xcolor}% you need this before tikz to avoid an option clash
\usepackage{tikz,tikzpagenodes}
\usepackage{geometry}
\geometry{% Please make twoside settings
  paperwidth=210mm, paperheight=297mm,
  bindingoffset=0mm, asymmetric,
  left=13mm,  %% or inner=23mm
  right=18mm, %% or outer=18mm
  top=11mm, bottom=11mm,
  headsep=3mm,
  footskip=3mm
}

\usepackage{titletoc}
\usepackage[pagestyles]{titlesec}
\usepackage{background}
\newcommand{\colourframe}[2][white]{%
  \clearpage
  \backgroundsetup{%
    angle=0,
    scale=1,
    opacity=1,
    contents={%
      \begin{tikzpicture}[remember picture, overlay]
        \path [fill=#2, even odd rule]
          (current page.south west) rectangle (current page.north east)
          (current page text area.south west) rectangle (current page text area.north east)
          ;
        \node [fill=#1, draw=black, anchor=north, text=black, inner xsep=5mm, inner ysep=1.5mm, yshift=-1.5mm] at (current page text area.south) {\thepage};
      \end{tikzpicture}},
    position=current page.south west,
    nodeanchor=south west
  }%
  \pagestyle{colourfancy}%
  \sethead[][][\thesection\quad\sectiontitle]{\thesection\quad\sectiontitle}{}{}}

\newpagestyle{colourfancy}[]{}

\begin{document}

  \colourframe[orange]{red}
  \section{First Red section}
  \subsection{Red A}
  \subsubsection{Red AA}
  \subsubsection{Red AB}

  \newpage
  % 2nd red page
  \section{Second Red section}
  \subsection{Red B}
  \subsubsection{Red BA}
  \subsubsection{Red BB}

  \colourframe[cyan]{blue}
  \section{Blue section}

\end{document}

red and blue frames

Note that I'm unsure why you say you want two-sided settings when you do not seem to really be using them! That is, you have set things up so that the margins are the same on every page. That means the outer margin will sometimes be larger than the inner one and sometimes smaller.

Step 2: Adjusting the Inner and Outer Margins

In your sketch, however, the output is closer to this:

\documentclass[a4paper,twoside]{article}

...

\geometry{% Please make twoside settings
  paperwidth=210mm, paperheight=297mm,
  bindingoffset=0mm,
  right=13mm,  %% or inner=23mm
  left=18mm, %% or outer=18mm
  top=11mm, bottom=11mm,
  headsep=3mm,
  footskip=3mm
}

larger inner and smaller outer margins

That makes the large margins on the inside and the small on the outside. A more usual layout would reverse that behaviour:

\documentclass[a4paper,twoside]{article}

...

\geometry{% Please make twoside settings
  paperwidth=210mm, paperheight=297mm,
  bindingoffset=0mm,
  left=13mm,  %% or inner=23mm
  right=18mm, %% or outer=18mm
  top=11mm, bottom=11mm,
  headsep=3mm,
  footskip=3mm
}

larger outer and smaller inner margins

so that the larger margin is on the outside and the two smaller inner margins combine on a double-page spread.

Step 3: Shrink the Frame

The code above does not allow the page itself any 'breathing' room. That is, the content is right bang up against the frame. This is non-optimal. It would be better to shrink the frame a little away from the contents of the page. This can be done using the calc library:

\documentclass[a4paper,twoside]{article}

\usepackage[svgnames,x11names]{xcolor}% you need this before tikz to avoid an option clash
\usepackage{tikz,tikzpagenodes}
\usetikzlibrary{calc}
\usepackage{geometry}
\geometry{% Please make twoside settings
  bindingoffset=0mm,
  left=13mm,  %% or inner=23mm
  right=18mm, %% or outer=18mm
  top=11mm, bottom=11mm,
  headsep=3mm,
  footskip=3mm
}

\usepackage{titletoc}
\usepackage[pagestyles]{titlesec}
\usepackage{background}
\newcommand{\colourframe}[2][white]{%
  \clearpage
  \backgroundsetup{%
    angle=0,
    scale=1,
    opacity=1,
    contents={%
      \begin{tikzpicture}[remember picture, overlay]
        \path [fill=#2, even odd rule]
          (current page.south west) rectangle (current page.north east)
          ($(current page text area.south west) - (2mm,2mm)$) rectangle ($(current page text area.north east) + (2mm,2mm)$)
          ;
        \node [fill=#1, draw=black, anchor=north, text=black, inner xsep=5mm, inner ysep=1.5mm, yshift=-1mm] at ($(current page text area.south) - (0,2mm)$) {\thepage};
      \end{tikzpicture}},
    position=current page.south west,
    nodeanchor=south west
  }%
  \pagestyle{colourfancy}%
  \sethead[][][\thesection\quad\sectiontitle]{\thesection\quad\sectiontitle}{}{}}

\newpagestyle{colourfancy}[]{}

\begin{document}

  \colourframe[orange]{red}
  \section{First Red section}
  \subsection{Red A}
  \subsubsection{Red AA}
  \subsubsection{Red AB}

  \newpage
  % 2nd red page
  \section{Second Red section}
  \subsection{Red B}
  \subsubsection{Red BA}
  \subsubsection{Red BB}

  \colourframe[cyan]{blue}
  \section{Blue section}

\end{document}

I've specified 2mm of shrinkage but you can adjust this as you wish. Personally, I would increase the margin sizes somewhat - I think things look much too squeezed, especially since information has to be included within this space. But if you are short of paper, this does seem to work.

Here's the resulting double-page spread (i.e. pages 2 and 3):

shrunken frame

Step 4: Incorporate Code from Updated Question

This is the result of incorporating the additional customisation you added to your question:

custom layout with smaller inner margins

I've adapted \colourframe[]{} so that it also configures the changes to the format for section titles, paragraph layout, headers and footers as shown in your example. The colour of the section titles etc. is based on the colour specified for the frame. As before, the optional argument specifies the background colour for the box containing the page number.

\documentclass[a4paper,twoside]{article}

\usepackage[svgnames,x11names]{xcolor}% you need this before tikz to avoid an option clash
\usepackage{tikz,tikzpagenodes}
\usetikzlibrary{calc}
\usepackage{geometry}
\geometry{% Please make twoside settings
  bindingoffset=0mm,
  left=13mm,  %% or inner=23mm
  right=18mm, %% or outer=18mm
  top=11mm, bottom=11mm,
  headsep=3mm,
  footskip=3mm
}

\usepackage{titletoc}
\usepackage[pagestyles]{titlesec}
\usepackage{background}
\newcommand{\colourframe}[2][white]{%
  \clearpage
  \backgroundsetup{%
    angle=0,
    scale=1,
    opacity=1,
    contents={%
      \begin{tikzpicture}[remember picture, overlay]
        \path [fill=#2, even odd rule]
          (current page.south west) rectangle (current page.north east)
          ($(current page text area.south west) - (2mm,2mm)$) rectangle ($(current page text area.north east) + (2mm,2mm)$)
          ;
        \node [fill=#1, draw=black, anchor=north, text=black, inner xsep=5mm, inner ysep=1.5mm, yshift=-1mm] at ($(current page text area.south) - (0,2mm)$) {\thepage};
      \end{tikzpicture}},
    position=current page.south west,
    nodeanchor=south west
  }%
  \pagestyle{colourfancy}%
  \sethead[][][\thesection\quad\sectiontitle]{\thesection\quad\sectiontitle}{}{}%
  \setfoot[][][]{}{}{}%
  \colorlet{SectionFrameColor}{#2!50}%
  \colorlet{SectionFillColor}{#2!20}%
  \colorlet{SectionFontColor}{#2!80}%
  \titleformat{\section}[hang]{%
    \Large\sffamily\color{SectionFontColor}%
  }{%
    \begin{tikzpicture}
      [
        baseline={([yshift=-.8ex]current bounding box.center)},
      ]
      \node [thick, draw=SectionFrameColor, fill=SectionFillColor, rectangle, rounded corners, text=white] {\thesection};
    \end{tikzpicture}%
  }{12pt}{}[{\titlerule[1pt]}]%
  \titleformat{\paragraph}[hang]{\bfseries}{}{}{}[]}

\newpagestyle{colourfancy}[]{}
\newpagestyle{tocandintro}[]{\headrule\setheadrule{0.5mm}\footrule\setfootrule{0.5mm}}
\backgroundsetup{%
  contents={},
}

\begin{document}

  \pagestyle{tocandintro}
  \sethead[Table of contents][][]{}{Table of contents}{\textsf{目次}}
  \setfoot[\thepage][][ABC Project]{ABC Project}{}{\thepage}

  \tableofcontents

  \clearpage% uncomment if you don't want to have Intro in the header of the final contents page
  \sethead[Intro][][]{}{}{Intro}
  Intro text

  \colourframe[orange]{red}
  \section{First Red section}
  \subsection{Red A}
  \subsubsection{Red AA}
  \subsubsection{Red AB}

  \newpage
  % 2nd red page
  \section{Second Red section}
  \subsection{Red B}
  \subsubsection{Red BA}
  \subsubsection{Red BB}

  \colourframe[cyan]{blue}
  \section{Blue section}

\end{document}

Adding asymmetric back into the page layout for geometry:

custom layout with asymmetric margins

Note that the margins are very like those you would get if using oneside in this case i.e. the margins are defined for left and right as opposed to inner and outer.

If you just want the larger margin on the inside, switch the values for the left and right margin rather than passing asymmetric. This gives you:

custom layout with larger inner margins

Related Question