# Replacing \fancyhdr with \titlesec in complicated design documents

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.

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}
paperwidth=210mm, paperheight=297mm,
bindingoffset=0mm, asymmetric,
left=13mm,  %% or inner=23mm
right=18mm, %% or outer=18mm
top=11mm, bottom=11mm,
footskip=3mm
}

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

\begin{document}

% 1st red page
\pagestyle{redfancy} % color: Header/Footer/Margin color: Firebrick2
\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:

## 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).

Before, I did it this way:

%\usepackage[pagestyles]{titlesec}

\pagestyle{tocandintro}
\setfoot[\thepage][][ABC Project]{ABC Project}{}{\thepage}

\tableofcontents

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

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.

• Please, could you post the code used in fancyhdr? – Javier Bezos Oct 15 '14 at 14:12
• I am very sorry, besides fancy header, footer and margin I need 'titlesec' for other needs, too (for example, for titles design). Also, there is already exist the solution with 'fancyhdr' (check link in the question field), so if don't use 'titlesec', there is no reason to post current question. – Gurebu Bokofu Oct 15 '14 at 22:24
• Does the configuration you posted not work with my solution? – cfr Oct 20 '14 at 15:34
• I've edited my answer to show how to combine the new code you posted with the code in my solution. There is nothing new here: I've just demonstrated that it works so far as I can tell. (That is, so far as I understand your updated question.) – cfr Oct 20 '14 at 18:50
• OK, I think it's enough. Please accept the 100 reputation as reward. @Peter Grill, I'm sorry, you've done the great work too, but because of the forum rules I can to give the reputation reward only to one person. All what I can do for you is push This answer is useful near your answer... – Gurebu Bokofu Oct 21 '14 at 6:51

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}
paperwidth=210mm, paperheight=297mm,
bindingoffset=0mm, asymmetric,
left=13mm,  %% or inner=23mm
right=18mm, %% or outer=18mm
top=11mm, bottom=11mm,
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}%

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


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}

...

paperwidth=210mm, paperheight=297mm,
bindingoffset=0mm,
right=13mm,  %% or inner=23mm
left=18mm, %% or outer=18mm
top=11mm, bottom=11mm,
footskip=3mm
}


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}

...

paperwidth=210mm, paperheight=297mm,
bindingoffset=0mm,
left=13mm,  %% or inner=23mm
right=18mm, %% or outer=18mm
top=11mm, bottom=11mm,
footskip=3mm
}


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}
bindingoffset=0mm,
left=13mm,  %% or inner=23mm
right=18mm, %% or outer=18mm
top=11mm, bottom=11mm,
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}%

\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):

# Step 4: Incorporate Code from Updated Question

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}
bindingoffset=0mm,
left=13mm,  %% or inner=23mm
right=18mm, %% or outer=18mm
top=11mm, bottom=11mm,
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}%
\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}[]{}
\backgroundsetup{%
contents={},
}

\begin{document}

\pagestyle{tocandintro}
\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
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:

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:

Here is a preliminary version (might need some tweaking upon further testing):

## Code:

\documentclass[a4paper]{article}
\usepackage[demo]{graphicx}
\usepackage[all]{background}

\usepackage{geometry}
paperwidth=210mm, paperheight=297mm,
bindingoffset=0mm, asymmetric,
inner=23mm,  %% or inner=23mm
outer=18mm, %% or outer=18mm
top=11mm,
bottom=11mm,
footskip=3mm,
%   showframe,
}

\usepackage{lipsum}
\usepackage{layout}
\usepackage{tikz}
\usetikzlibrary{calc}
%\usepackage{showframe}

\usepackage{changepage}
\strictpagecheck

\newlength{\MinimumPageNodeHeight}
\setlength{\MinimumPageNodeHeight}{3.0ex}

\newcommand*{\FooterXLocation}{\dimexpr1.0in+\hoffset+\oddsidemargin+0.5\textwidth\relax}%

\newcommand*{\VOffsetBottom}{\dimexpr\paperheight-\VLocationBottom-0.5\MinimumPageNodeHeight\relax}% vertical offset
\newcommand*{\HOffsetInner}{\dimexpr1.0in+\hoffset+\oddsidemargin}% horizontal offset
\newcommand*{\HOffsetOuter}{\dimexpr1.0in+\hoffset+\evensidemargin}% horizontal offset

\newcommand*{\PageBorderFillColor}{}%
\newcommand*{\PageNumberFillColor}{}%

\newcommand*{\SetPageStyle}[2]{%
\xdef\PageBorderFillColor{#1}%
\xdef\PageNumberFillColor{#2}%
}%

%% http://tex.stackexchange.com/questions/62241/how-to-get-the-current-chapter-name-section-name-subsection-name-etc
\let\Sectionmark\sectionmark
\def\sectionmark#1{\def\CurrentSectionName{#1}\Sectionmark{#1}}

\pagestyle{empty}
\newcommand{\MyTikzLogo}{% For a logo drawn with TikZ
\begin{tikzpicture}[remember picture,overlay,draw=black,ultra thick]
\checkoddpage
\ifoddpage
\fill [\PageBorderFillColor] (current page.south west) rectangle ++(\HOffsetInner,\paperheight);
\fill [\PageBorderFillColor] (current page.south west) rectangle ++(\paperwidth,\VOffsetBottom);
\fill [\PageBorderFillColor] (current page.south east) rectangle ++(-\HOffsetOuter,\paperheight);
\fill [\PageBorderFillColor] (current page.north east) rectangle ++(-\paperwidth,-\VOffsetTop);

\coordinate (Title Location) at
($(current page.north west)+(\HeaderXLocation,-\HeaderYLocation)+(\oddsidemargin,0)$);
\coordinate (Page Number Location) at
($(current page.north west)+(\FooterXLocation,-\FooterYLocation)$);
\else
\fill [\PageBorderFillColor] (current page.south east) rectangle ++(-\HOffsetInner,\paperheight);
\fill [\PageBorderFillColor] (current page.south east) rectangle ++(-\paperwidth,\VOffsetBottom);
\fill [\PageBorderFillColor] (current page.south west) rectangle ++(\HOffsetOuter,\paperheight);
\fill [\PageBorderFillColor] (current page.north east) rectangle ++(-\paperwidth,-\VOffsetTop);

\coordinate (Title Location) at
($(current page.north west)+(\dimexpr\HeaderXLocation,-\HeaderYLocation)+(\evensidemargin,0)+(\marginparsep,0)$);
\coordinate (Page Number Location) at
($(current page.north east)+(-\FooterXLocation,-\FooterYLocation)$);
\fi

\node [draw=none, fill=none, anchor=south west, text=black, font=\bfseries]
\node [
draw=black,
fill=\PageNumberFillColor,
minimum width=1.5cm,
minimum height=\MinimumPageNodeHeight,
anchor=center,
text=black
] at (Page Number Location) {\thepage};
\end{tikzpicture}
}

\SetBgContents{\MyTikzLogo}% Set tikz picture

\SetBgPosition{current page.north west}% Select location
\SetBgOpacity{1.0}% Select opacity
\SetBgAngle{0.0}% Select roation of logo
\SetBgScale{1.0}% Select scale factor of logo

\begin{document}
\SetPageStyle{red!75}{orange}
\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
\SetPageStyle{blue!50}{teal}
\section{Blue section}

\newpage % Green page
\SetPageStyle{green!50}{magenta}
\section{Green section}

\end{document}

• I am sorry, but is your solution compatible with titlesec package? It's OK to don't use its commands for page design, but i need to hold \usepackage{titlesec} in my preamble. – Gurebu Bokofu Oct 18 '14 at 6:31
• @GurebuBokofu: Can you provide a list of customizations you need via titlesec? The current solution was meant to produce the sketch of the desires results given in the MWE. – Peter Grill Oct 18 '14 at 17:09
• First, it's a header/footer contents which could be changed with layout (For example 'table of contents', 'intro', 'part 1' etc.). Second, it's a part/section/subsection/paragraph titles control (as in this question). – Gurebu Bokofu Oct 19 '14 at 2:48
• @GurebuBokofu The header and footer contents change by default in that way, don't they? That is, they are sensitive to various aspects of the current context (and can be customised independently of titlesec). I generally find that whatever I think titlesec would be useful for is more easily done by dispensing with it. But I admit others seem to find it works better than I do. – cfr Oct 19 '14 at 3:31
• @GurebuBokofu You said you'd no idea how to use TiKZ with titlesec but doesn't the question you linked to do precisely that? – cfr Oct 19 '14 at 3:32