1

Following my question about the customisation of the section heading, I would like to have the box with the section number always in the outside of the page ( I am using the twoside option). That is, for odd pages, the box with the section number should be at the right, while for even pages, the box with the section number should be left.

For even pages:

enter image description here

and for odd pages:

enter image description here

Here comes the issues:

  1. I changed the code manually to get the section number at the desired place for the snippets. But, of course, this is not acceptable for big documents. Any help in doing this automatically?
  2. For odd pages, I could not manage to get the box outside the text area, as in even pages.

Once again, any guidance is welcome.

Here is my MWE:

\documentclass[openright]{scrbook}
\usepackage{geometry}
\geometry{a4paper,twoside, margin=3cm}
\usepackage{blindtext}
\usepackage{xcolor}
\usepackage{xhfill}

\renewcommand*{\sectionformat}{%
    \usekomafont{section}%
    \makebox[0pt][r]{%
    \fboxrule=1.5pt\fcolorbox{blue}{white!0}{\color{black}\thesection}\color{blue}\rule[.6ex]{10pt}{1.5pt}
    }%
}
\makeatletter
\renewcommand*{\sectionlinesformat}[4]{%
    \@hangfrom{\hskip #2#3}{\color{blue}#4}$\;$%
    \xrfill[.6ex]{1.5pt}[blue]%
}
\makeatother

\begin{document}
    \chapter{Introduction}
    \section{Section 1}
    \Blindtext[2]
    \section{Section 2}
    \Blindtext[2]
    \section{Section 3}
    \Blindtext[2]
    \section{Section 4}
    \Blindtext[2]
\end{document}

Thanks!

1 Answer 1

2

You could use KOMA-Script command \Ifthispageodd:

\documentclass[twoside,open=right]{scrbook}
\usepackage{geometry}
\geometry{margin=3cm}
\usepackage{blindtext}% only for dummy text
\usepackage{xcolor}
\usepackage{xhfill}

\renewcommand*{\sectionformat}{%
  \fboxrule=1.5pt\fcolorbox{.}{white}{\normalcolor\thesection}%
}
\renewcommand*{\subsectionformat}{%
  \fboxrule=1.5pt\fcolorbox{.}{white}{\normalcolor\thesubsection}%
}

\makeatletter
\renewcommand*{\sectionlinesformat}[4]{%
  \Ifthispageodd
    {\rule[.6ex]{10pt}{1.5pt}\;#4\;\xrfill[.6ex]{1.5pt}[.]\hspace{-1pt}\usekomafont{#1}\makebox[0pt][l]{\rule[.6ex]{11pt}{1.5pt}#3}}
    {\makebox[0pt][r]{#3\rule[.6ex]{10pt}{1.5pt}\;}#4\;\xrfill[.6ex]{1.5pt}[.]}%
}
\makeatother

\addtokomafont{section}{\color{blue!50!black}}
\addtokomafont{subsection}{\color{green!50!black}}

\begin{document}
\chapter{Introduction}
\section{Section 1}
\subsection{Subsection}
\Blindtext[2]
%\vspace{4\baselineskip}
\section{Section 2}
\Blindtext[1]
%\clearpage
\section{Section 3}
\Blindtext[2]
\section{Section 4, very long section title that requires more than one line}
\Blindtext[2]
\end{document}

enter image description here

Warning: \Ifthispageodd has limitations. If the command is excecuted on one page, but the content is shifted to the next page, it goes wrong. Uncomment the line \vspace{4\baselineskip} to see the problem. So you have to controll your document when you finish and fix the issue using eg. \clearpage.


Additional remark: The code in the question uses the same format for section, subsection and subsubsection headings. I have added a subsection to show this behavior and how to use different colors for section and subsection headings.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .