
在我提出關於自訂章節標題,我希望帶有部分編號的框始終位於頁面外部(我正在使用“twoside”選項)。也就是說,對於奇數頁,帶有節號的框應位於右側,而對於偶數頁,帶有節號的框應位於左側。
對於偶數頁:
對於奇數頁:
問題來了:
- 我手動更改了程式碼,以在片段所需的位置獲取節號。但是,當然,這對於大文檔來說是不可接受的。自動執行此操作有什麼幫助嗎?
- 對於奇數頁,我無法像偶數頁一樣將框移到文字區域之外。
再次歡迎任何指導。
這是我的 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}
謝謝!
答案1
您可以使用 KOMA-Script 指令\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}
警告:\Ifthispageodd
有限制。如果在一頁上執行指令,但內容卻轉移到下一頁,就會出錯。取消註解該行\vspace{4\baselineskip}
即可查看問題。因此,當您完成並使用例如解決問題時,您必須控制您的文件。\clearpage
。
補充說明:問題中的程式碼對節、小節和小節標題使用相同的格式。我添加了一個小節來展示此行為以及如何為節和小節標題使用不同的顏色。