3

I've a question about section divider as you can see from my picture. If I want to align "Section 2" on an even page the same position as "Section 1" which is on an odd page, I've to use command like \begin{OneCol} \section{Section 2} \end{OneCol} and that was not convenient for me.

I wonder if there is anyway to make its move to the left side automatically whenever it lies on the even page.

Thanks in advance. my mwe

\documentclass[twoside]{scrbook}
\usepackage{adjmulticol}
\usepackage{xcolor,graphicx,kantlipsum,multicol}
\usepackage[papersize={21cm,23cm},top=1.8cm, bottom=2cm, outer=7cm, inner=2cm, heightrounded, marginparwidth=5.0cm, marginparsep=0.5cm]{geometry}
\usepackage[most]{tcolorbox}
\definecolor{titlebgdark}{RGB}{0,163,243}
\definecolor{titlebglight}{RGB}{191,233,251}
%=============================================================================================
%CREATE NEW COMMAND for one or two columns
\newenvironment{OneCol}{\normalsize
    \begin{adjmulticols}{1}{0cm}{-5.5cm}}{\end{adjmulticols}}
\newenvironment{TwoCol}{\normalsize
    \begin{adjmulticols}{2}{0cm}{0cm}}{\end{adjmulticols}}
%=====================================================================================
%This part is used for colorful chapter style
\usepackage[explicit]{titlesec}

\titleformat{\chapter}[display]{\normalfont\Huge\bfseries}{}{-10pt}
{%
    \begin{tcolorbox}[enhanced, colback=titlebgdark, boxrule=0.2cm,
        colframe=titlebglight,arc=0pt,outer arc=0pt,leftrule=0pt,rightrule=0pt,
        fontupper=\color{white}\sffamily\bfseries\Huge,enlarge left by=-1in-\hoffset-\oddsidemargin,
        enlarge right by=-\paperwidth+1in+\hoffset+\oddsidemargin+\textwidth,width=\paperwidth,
        left=1in+\hoffset+\oddsidemargin,right=\paperwidth-1in-\hoffset-\oddsidemargin-\textwidth,
        top=0.5cm,bottom=0.5cm,%
        overlay={\node[fill=titlebgdark,draw=titlebglight,line width=0.1cm,inner sep=-0pt,
            text width=3cm,minimum height=3cm,align=center,font=\color{white}\rmfamily\bfseries\fontsize{80}{90}\selectfont
            ](chapname) at ([xshift=-4cm]frame.north east){\thechapter};
            \node[font=\color{black}\rmfamily\bfseries\fontsize{30}{30}\selectfont,inner sep=5pt] at ([xshift=-3cm,yshift=0.5cm] chapname.west){\MakeUppercase\chaptertitlename};}
        %   \node[font=\color{black}\rmfamily\bfseries\fontsize{15}{15}\selectfont,inner sep=5pt] at (chapname.west){\MakeUppercase\chaptertitlename};}
        ]#1
    \end{tcolorbox}%
}
%=======================================================================================
\titleformat{\section}[block]
{\Large\bfseries}
{}
{0pt}
{\hspace{-0cm}% Move into margin
    \tikz[overlay] \shade[left color=black!20, right color=white] (0,-1ex) rectangle (\dimexpr\textwidth+5.5cm,1.2em);% Set background shaded rectangle
    \makebox[2em][l]{\thesection}#1}% Set number + title

%========================================================================================
\begin{document}
\chapter{Introduction}
\section{Section 1}
\kant[1]
\newpage
\section{Section 2}
\kant[1]
\end{document}
3

You could use \ifthispageodd (could need a second run):

\ifthispageodd{}{\hskip\dimexpr-\marginparwidth-\marginparsep\relax}

Note, that the usage of titlesec together with a KOMA-Script class is not recommended. But you can redefine \chapterlinesformat and \sectionlinesformat.

enter image description here

Code (without titlesec):

\documentclass[twoside]{scrbook}
\usepackage{kantlipsum}% only for dummy text

\usepackage[papersize={21cm,23cm},top=1.8cm, bottom=2cm, outer=7cm, inner=2cm, heightrounded, marginparwidth=5.0cm, marginparsep=0.5cm]{geometry}

\usepackage{scrhack}
\usepackage[most]{tcolorbox}% loads also xcolor and graphicx
\definecolor{titlebgdark}{RGB}{0,163,243}
\definecolor{titlebglight}{RGB}{191,233,251}

%=============================================================================================
\usepackage{adjmulticol}% loads also multicol
%CREATE NEW COMMAND for one or two columns
\newenvironment{OneCol}{\normalsize
    \begin{adjmulticols}{1}{0cm}{-5.5cm}}{\end{adjmulticols}}
\newenvironment{TwoCol}{\normalsize
    \begin{adjmulticols}{2}{0cm}{0cm}}{\end{adjmulticols}}
%=====================================================================================

\usepackage{lmodern}% scalable font

\renewcommand\chapterformat{\thechapter}
\addtokomafont{chapter}{\color{white}\Huge}
\renewcommand\chapterlinesformat[3]{%
  \ifstr{#1}{chapter}{%
    \begin{tcolorbox}[enhanced, colback=titlebgdark, boxrule=0.2cm,fontupper=\usekomafont{chapter},
        colframe=titlebglight,arc=0pt,outer arc=0pt,leftrule=0pt,rightrule=0pt,
        enlarge left by=-1in-\hoffset-\oddsidemargin,
        enlarge right by=-\paperwidth+1in+\hoffset+\oddsidemargin+\textwidth,width=\paperwidth,
        left=1in+\hoffset+\oddsidemargin,right=\paperwidth-1in-\hoffset-\oddsidemargin-\textwidth,
        top=0.5cm,bottom=0.5cm,%
        overlay={\ifstr{#2}{}{}{%
            \node[fill=titlebgdark,draw=titlebglight,line width=0.1cm,inner sep=-0pt,
            text width=3cm,minimum height=3cm,align=center,font=\rmfamily\fontsize{80}{90}\selectfont
            ](chapname) at ([xshift=-4cm]frame.north east){#2};
            \node[font=\color{black}\rmfamily\fontsize{30}{30}\selectfont,inner sep=5pt] at ([xshift=-3cm,yshift=0.5cm] chapname.west){\MakeUppercase\chaptername};}%
            }
        ]\raggedchapter#3
    \end{tcolorbox}%
  }{%
    \@hangfrom{#2}{#3}% original definition for other levels
  }
}
%=======================================================================================
\renewcommand\sectionformat{\makebox[2em][l]{\thesection\autodot}}
\addtokomafont{section}{\rmfamily}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \ifstr{#1}{section}{%
    \ifthispageodd{}{\hskip\dimexpr-\marginparwidth-\marginparsep\relax}% Move into margin
    \tikz[overlay] \shade[left color=black!20, right color=white] (0,-1ex) rectangle (\dimexpr\textwidth+5.5cm,1.2em);% Set background shaded rectangle
          #3#4% Set number + title
  }{%
    \@hangfrom{\hskip #2#3}{#4}% original definition for other levels
  }%
}
\makeatother
%========================================================================================
\begin{document}
\tableofcontents
\chapter{Introduction}
\section{Section 1}
\kant[1-3]
\section{Section 2}
\kant[4]
\end{document}

Additional remarks:

Option most for package tcolorbox loads the tcolorbox library listingsutf8 amongst other libraries. This library requires package listings and therefore you get a warning regarding \float@addtolists detected!. To avoid the warning you can load package scrhack (as suggested by the warning message) or you could replace option most by many if you do not need the tcolorbox libraries listingsutf8, external, magazine, vignette, poster.

  • That's exactly what I'm talking about. Thanks for your kind. The reason I used "titlesec" because I just found that code posted here, so I grab it for my work even I knew that it isn't compatible with Koma-script but I don't know how to convert "titlesec" code to "Koma-script" code. Koma-script manual has many pages and it's kind of hard to understand in my point of view. I'm gonna learn from you code and will ask you here more questions about those codes if you don't mind. – kmmm Mar 21 '18 at 3:28
  • What's purpose of \usepackage{scrhack} and where did u find it?. Is it explain in Koma-script manual?. – kmmm Mar 21 '18 at 3:34
  • See my updated answer. Package scrhack is part of the KOMA-Script bundle. You can find its documentation in the KOMA-Script manual. \chapterlinesformat and \sectionlinesformat are explained in the Part II (KOMA-Script for Advanced Users and Experts) of the documentation. – esdd Mar 21 '18 at 7:47
  • Very clear answer. Thanks again. Like I said, I'm a new user and just read on the Part I of Koma-script right now. – kmmm Mar 21 '18 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.