0

How can I display \section in a colored background that starts on one side of the page (odd pages = left and even pages = right) as in the image below.

enter image description here

Edit

From this code:

\documentclass{book}

\usepackage{xcolor} \usepackage{titlesec} \definecolor{mygreen}{RGB}{204,226,158}

\titleformat{\section}[hang]{\scshape\Large{\rlap{\color{mygreen}\rule[-2.3ex]{\linewidth}{6ex}\vspace{-4.5ex}}}}{\thesection}{1em}{}

\usepackage[a4paper, inner=20mm, outer=60mm, top=25mm, bottom=25mm, marginparsep=5mm, marginparwidth=50mm, headsep =35pt, ]{geometry}

\usepackage{showframe}

\begin{document}

\section{Introduction}

\end{document}

  • 1
    What is your concrete problem? Using colors? Detecting odd and even pages? Configuration of \section? Or what else? Please show a minimal working example of what you've already tried. Currently we even do not know, which class you are using. There are different book doc classes, e.g., the standard document class book, but also memoir is IMHO a book class, scrbook is a book class, and many more. – cabohah Jul 31 '23 at 08:54
  • @cabohah Yes, I am using the book class. You can read the edit I just made if I was unclear earlier. I really thought the image diagram made sense. Sorry. – albin kabashi Jul 31 '23 at 19:21
  • Please show a minimal working example. What are you using to allow \titleformat? Part of your problem is that "stretching" suggests (to me) something like a rubber band, so that the content will also be distorted. I don't think you're looking for that. Just saying "look at the picture" doesn't really help. – Teepeemm Jul 31 '23 at 19:27
  • @Teepeemm hope the edit makes more sense now. – albin kabashi Jul 31 '23 at 19:41

1 Answers1

1

It looks like this will work:

\documentclass{book}

\usepackage{xcolor} \usepackage{titlesec} \definecolor{mygreen}{RGB}{204,226,158} \usepackage{ifthen} \usepackage[a4paper, inner=20mm, outer=60mm, top=25mm, bottom=25mm, marginparsep=5mm, marginparwidth=50mm, headsep =35pt, ]{geometry}

\newlength\lineAndMargin \setlength{\lineAndMargin}{\linewidth} \addtolength{\lineAndMargin}{2.9cm} % not sure why this value

\titleformat{\section}[block] {\scshape\Large \makebox[0pt][l]{\color{mygreen}% \ifthenelse{\isodd{\thepage}}{\hspace{-2cm}}{}% \rule[-2.3ex]{\lineAndMargin}{6ex}}} {\thesection} % label {1em} % sep {} % before code

\usepackage{showframe}

\begin{document}

\section{Introduction}

Section content

\clearpage

\section{Conclusion}

Section content

\end{document}

odd page output ... even page output Beware that this uses the conditional \isodd{\thepage}, which is not necessarily the best idea. If you find that your headings are acting like they're on the other page, you'll need to work through If Then Else for odd page/even page.

Teepeemm
  • 6,708