# Extending section title over margins and odd/even not working with fancyhdr

I am working with the following code:

\documentclass[11pt,a4paper,twoside=semi]{article}

\usepackage{kantlipsum}
\usepackage{tikz}

%*****************Margins and MarginNotes******************

\usepackage[top=2.8cm, bottom=2.8cm,outer=7cm, inner=1.75cm, marginparwidth=4.25cm, marginparsep=0.75cm]{geometry}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyfoot[LO,RE]{\thepage}

%***********************Document*******************
\usepackage[explicit]{titlesec}

\titleformat{\section}{\sffamily \bf \Large}{}{0em}
{%
\begingroup
\color{gray!30}%
\titleline{\leaders\hrule height 1.75em \hskip 4pt plus 1.2 fill \kern 0pt\relax}%
\endgroup\vskip-1.323em\hskip0.5em\sffamily \thesection\hskip1em#1
\nobreak
}

\begin{document}
\section{Section 1}
\kant[12]
\marginpar{The reader should be careful to observe that the objects in space and time are the clue to the discovery of, certainly, our a priori knowledge, by means of analytic unity.}
\kant[8]
\marginpar{The reader should be careful to observe that the objects in space and time are the clue to the discovery of, certainly, our a priori knowledge, by means of analytic unity.}
\kant[7]\marginpar{The reader should be careful to observe that the objects in space and time are the clue to the discovery of, certainly, our a priori knowledge, by means of analytic unity.}
\section{Section 1}
\kant[7]
\marginpar{The reader should be careful to observe that the objects in space and time are the clue to the discovery of, certainly, our a priori knowledge, by means of analytic unity.}

\newpage

\end{document}


This produces an article with a specific style of section title. My questions are: 1.) How can I adjust the width of the grey box behind a section title, so that it extends over the margin notes?

2.) Is there a reason why, despite specifying LO, RE for the headings they still only appear on the left hand side, despite a page being odd or even? I'd like to have the UNIT title header on the left for odd pages and on the right for even pages.

• Regarding your second question: If you look into the logfile, you will see the warning that twoside=semi has been ignored, since there is no such option for the article class. And for onesided documents there are only odd pages as far as the page style is concerned. If you use the option twoside, you have both types of headings, but you will have to adapt the margins via the geometry package. Jan 21, 2017 at 15:41
• Thanks, what about the first question? Jan 21, 2017 at 15:56

There is no twoside=semi option for article.

You can emulate two sided printing (for alternating headers) by using the left and right fields.

I have also simplified the definition for the gray bar underneath the section title. Don't forget to set headheight.

\documentclass[11pt,a4paper]{article}

\usepackage{kantlipsum}
\usepackage{tikz}

%*****************Margins and MarginNotes******************

\usepackage[
top=2.8cm,
bottom=2.8cm,
outer=7cm,
inner=1.75cm,
marginparwidth=4.25cm,
marginparsep=0.75cm,
%showframe,% remove for production version
]{geometry}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyfootoffset[R]{\dimexpr\marginparsep+\marginparwidth}
\fancyfoot[L]{\ifodd\value{page}\thepage\fi}
\fancyfoot[R]{\ifodd\value{page}\else\thepage\fi}

%***********************Document*******************
\usepackage{titlesec}

\titleformat{\section}
{\sffamily\bfseries\Large}
{}
{0em}
{\graybackground}

\newcommand{\graybackground}[1]{%
\makebox[\textwidth][l]{%
\colorbox{gray!30}{%
\parbox{\dimexpr\textwidth+\marginparsep+\marginparwidth-2\fboxsep}{
\vspace{3pt}
#1\par
\vspace{3pt}
}%
}%
}%
}

\begin{document}

some text to get a page
\clearpage

\section{Section 1}
\kant[12]
\marginpar{\sloppy The reader should be careful to observe that the objects in space and time are the clue to the discovery of, certainly, our a priori knowledge, by means of analytic unity.}
\kant[8]
\marginpar{\sloppy The reader should be careful to observe that the objects in space and time are the clue to the discovery of, certainly, our a priori knowledge, by means of analytic unity.}
\kant[7]\marginpar{\sloppy The reader should be careful to observe that the objects in space and time are the clue to the discovery of, certainly, our a priori knowledge, by means of analytic unity.}
\section{Section 2}
\kant[7]
\marginpar{\sloppy The reader should be careful to observe that the objects in space and time are the clue to the discovery of, certainly, our a priori knowledge, by means of analytic unity.}

\newpage

\end{document}


This is not an answer that gives a complete solution because it is not clear which aspects of your layout you want to keep and which to drop; keeping all of them is probably not what you really want.

Regarding your first question, define the titleline as

\titleline{\makebox[0pt][l]{\rule{\headwidth}{1.75em}}}


Then the gray bar will extend to the border of the margin. However, as you see below, it collides with the comments in the margin, so it may be better to confine the bar to the text area, or to have no comments in the margin.

Regarding your second question, note that the log file contains a warning that twoside=semi has been ignored, since there is no such option for the article class. And for one-sided documents there are only odd pages as far as the page style is concerned. If you use the option twoside, you have both types of headings, but you will have to adapt the margins via the geometry package. Moreover, in this case the margin comments will also jump.

In the code below, I have included the line

\usepackage{showframe}


to show the page layout. Remove it for the final document.

\documentclass[11pt,a4paper]{article}

\usepackage{kantlipsum}
\usepackage{tikz}

%*****************Margins and MarginNotes******************

\usepackage[top=2.8cm, bottom=2.8cm,outer=7cm, inner=1.75cm, marginparwidth=4.25cm, marginparsep=0.75cm]{geometry}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyfoot[LO,RE]{\thepage}
\usepackage{showframe}

%***********************Document*******************
\usepackage[explicit]{titlesec}

\titleformat{\section}{\sffamily \bf \Large}{}{0em}
{%
\begingroup
\color{gray!30}%
\endgroup\vskip-1.323em\hskip0.5em\sffamily \thesection\hskip1em#1
\nobreak
}

\begin{document}
\section{Section 1}
\kant[12] \marginpar{The reader should be careful to observe that the
objects in space and time are the clue to the discovery of,
certainly, our a priori knowledge, by means of analytic unity.}
\kant[8] \marginpar{The reader should be careful to observe that the
objects in space and time are the clue to the discovery of,
certainly, our a priori knowledge, by means of analytic unity.}
\kant[7]\marginpar{The reader should be careful to observe that the
objects in space and time are the clue to the discovery of,
certainly, our a priori knowledge, by means of analytic unity.}
\section{Section 1}
\kant[7] \marginpar{The reader should be careful to observe that the
objects in space and time are the clue to the discovery of,
certainly, our a priori knowledge, by means of analytic unity.}

\newpage

\end{document}

• That's excellent. Regarding the second point, I would've liked to have all the margins on the right, but still have the headers alternative from left to right depending on whether the page is odd or even. I'm not fussed as to what the document class is. I tried scrbook as well but this did not give the effect that I'm after. Jan 21, 2017 at 16:59
• @wrb98 What's your plan regarding collisions of margin notes with the gray bar? Jan 21, 2017 at 17:43
• I won't use margin notes near to grey bars. It's mainly the second point now that I wish to focus on. Jan 21, 2017 at 17:44
• if you could amend your code in any way to allow the page headers/footers to stretch to the edge of the margins, whilst alternating depending on the odd/even page then your answer would be perfect. Jan 21, 2017 at 18:04