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}
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
}
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
}
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):
Step 4: Incorporate Code from Updated Question
This is the result of incorporating the additional customisation you added to your 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}
\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
:
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:
I don't know how to tackle the problem with fancyhdr
, but since you use titlesec
, I suggest doing it with the titleps
module of titlesec
instead. For unnumbered subsections, I had to define a new command (nonumsubsection
) because titlesec
doesn't seem to manage the formatting of starred (sub)sections with an optional argument :
\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@Book{Dodgson,
author = {Lewis Carroll},
title = {The Hunting of the Snark},
year = {1876},
publisher = {Macmillan},
owner = {Bernard},
timestamp = {2016.01.27}
}
@book{lear72,
author = {Edward Lear},
year = {1872},
title = {More Nonsense Pictures, Rhymes, Botany, Etc. },
address = {London},
publisher = {Robert John Bush}
}
@book{Lear12,
author = {Edward Lear},
year = {2011},
title ={The Dong with a Luminous Nose},
address= {Petaluma (CA)},
Publisher = {Pomegranate Press}
}
\end{filecontents}
\usepackage{lipsum}
\usepackage{etoolbox, chngcntr}
\usepackage[pagestyles, toctitles]{titlesec}
\newcommand\nonumsubsection[2][]{{\setcounter{secnumdepth}{0}\def\thesubsection{\relax}\ifblank{#1}{\subsection{#2}}{\subsection[#1]{#2}}
\setcounter{secnumdepth}{2}}\counterwithin{subsection}{section}}
\newpagestyle{ownstyle}{%
\headrule
\sethead{\thesection~\sectiontitle}{}{\ifnumequal{\value{secnumdepth}}{0}{\subsectiontitle}{\thesubsection~\subsectiontitle}}%
\setfoot{}{\thepage}{}
}%
\pagestyle{ownstyle}
\newpagestyle{bibetc}{%
\sethead{\scshape\sectiontitle}{}{}
\setfoot{}{\thepage}{}
}%
\begin{document}
\thispagestyle{plain}
\tableofcontents
\section[Short Section]{Some section with quite a lengthy title}
\lipsum
\subsection[Short subsection]{Very very very long title of subsection}
\lipsum
\nonumsubsection[Unnumbered subsection]{Very very very long title of unnumbered subsection}
\lipsum
\subsection{Another numbered subsection}
\lipsum
\pagestyle{bibetc}
\nocite{*}
\bibliographystyle{plain}
\bibliography{\jobname}
\end{document}
Best Answer
Is it something like this you want? I used the companion package
titleps
: