Estendendo o título da seção além das margens e ímpar/par não funciona com o fancyhdr

Estendendo o título da seção além das margens e ímpar/par não funciona com o fancyhdr

Estou trabalhando com o seguinte código:

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

%*******************Header & Footer***********************
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LO,RE]{\footnotesize UNIT TITLE}
\fancyfoot[LO,RE]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\setlength{\headwidth}{\textwidth}
\addtolength{\headwidth}{\marginparsep}
\addtolength{\headwidth}{\marginparwidth}

%***********************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}

Isso produz um artigo com um estilo específico de título de seção. Minhas perguntas são: 1.) Como posso ajustar a largura da caixa cinza atrás do título de uma seção, para que ela se estenda além das notas da margem?

2.) Existe uma razão pela qual, apesar de especificar LO, RE para os títulos, eles ainda aparecem apenas no lado esquerdo, apesar de a página ser ímpar ou par? Gostaria de ter o cabeçalho do título UNIT à esquerda para páginas ímpares e à direita para páginas pares.

Responder1

Não há twoside=semiopção para artigo.

Você pode emular a impressão frente e verso (para cabeçalhos alternados) usando os campos esquerdo e direito.

Também simplifiquei a definição da barra cinza abaixo do título da seção. Não esqueça de definir 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,
  headheight=13.6pt,
  %showframe,% remove for production version
]{geometry}

%*******************Header & Footer***********************
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyheadoffset[R]{\dimexpr\marginparsep+\marginparwidth}
\fancyfootoffset[R]{\dimexpr\marginparsep+\marginparwidth}
\fancyhead[L]{\ifodd\value{page}\footnotesize UNIT TITLE\fi}
\fancyhead[R]{\ifodd\value{page}\else\footnotesize UNIT TITLE\fi}
\fancyfoot[L]{\ifodd\value{page}\thepage\fi}
\fancyfoot[R]{\ifodd\value{page}\else\thepage\fi}
\renewcommand{\headrulewidth}{0pt}

%***********************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}

insira a descrição da imagem aqui

Responder2

Esta não é uma resposta que forneça uma solução completa porque não está claro quais aspectos do seu layout você deseja manter e quais descartar; manter todos eles provavelmente não é o que você realmente deseja.

Em relação à sua primeira pergunta, defina a linha do título como

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

Então a barra cinza se estenderá até a borda da margem. Porém, como você pode ver abaixo, ela colide com os comentários da margem, então pode ser melhor limitar a barra à área de texto ou não ter comentários na margem.

Em relação à sua segunda pergunta, observe que o arquivo de log contém um aviso que twoside=semifoi ignorado, pois não existe tal opção para a classe do artigo. E para documentos unilaterais existem apenas páginas ímpares no que diz respeito ao estilo da página. Se utilizar a opção twoside, você terá os dois tipos de títulos, mas terá que adaptar as margens através do pacote de geometria. Além disso, neste caso os comentários nas margens também irão saltar.

No código abaixo, incluí a linha

\usepackage{showframe}

para mostrar o layout da página. Remova-o para o documento final.

insira a descrição da imagem aqui

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

%*******************Header & Footer***********************
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LO,RE]{\footnotesize UNIT TITLE}
\fancyfoot[LO,RE]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\setlength{\headwidth}{\textwidth}
\addtolength{\headwidth}{\marginparsep}
\addtolength{\headwidth}{\marginparwidth}
\usepackage{showframe}

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

\titleformat{\section}{\sffamily \bf \Large}{}{0em}
{%
\begingroup
  \color{gray!30}%
  \titleline{\makebox[0pt][l]{\rule{\headwidth}{1.75em}}}%
\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}

informação relacionada