如何使偶数页上的分节符与奇数页一样左对齐?

如何使偶数页上的分节符与奇数页一样左对齐?

我对分节符有一个疑问,如你从我的图片中看到的。如果我想将偶数页上的“第 2 节”与奇数页上的“第 1 节”对齐到相同的位置,我必须使用类似

\begin{OneCol}
   \section{Section 2}
\end{OneCol}

这对我来说并不方便。

我想知道是否有办法让它在位于偶数页时自动移动到左侧。

提前致谢。 我的母亲

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

答案1

您可以使用\Ifthispageodd(可能需要第二次运行):

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

titlesec注意,不建议将 与 KOMA-Script 类一起使用。但您可以重新定义\chapterlinesformat\sectionlinesformat

在此处输入图片描述

代码(不含titlesec):

\documentclass[twoside]{scrbook}
%\providecommand*\Ifthispageodd{\ifthispageodd}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\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}

补充说明:

most包选项tcolorbox会加载 tcolorbox 库listingsutf8以及其他库。此库需要包listings,因此您会收到有关 的警告\float@addtolists detected!。要避免出现警告,您可以加载包scrhack(如警告消息所建议),或者如果您不需要 tcolorbox 库,则可以将选项替换most为、、、。manylistingsutf8externalmagazinevignetteposter

相关内容