使用 koma 腳本自訂部分

使用 koma 腳本自訂部分

我想知道你是否可以提供一些幫助。我想自訂scrbook類別文檔中的章節標題。與此類似的東西: 在此輸入影像描述

框中的節編號,後面跟著居中的行節名稱和填滿文字寬度的另一條垂直居中的行。

到目前為止,我已經在這個 StackExchange 中搜尋了解決方案,但沒有成功。我已經檢查過Koma 腳本手冊並發現兩個可能的命令可以執行類似的操作: \sectionformat\sectionlinesformat。但老實說,我對乳膠的了解非常有限,無法讓我了解何時使用其中一種。

看看這個論壇中找到的類似解決方案,我設法得到了這個:

\documentclass[openright]{scrbook}
\usepackage{geometry}
\geometry{a4paper,twoside, margin=3cm}
\usepackage{blindtext}% only dummy text
\usepackage{xcolor}

\renewcommand*{\sectionformat}{%
    \usekomafont{section}
    \makebox[0pt][r]{\fboxrule=1.5pt\fcolorbox{black}{white!0}{\color{black}\thesection}\rule[.7ex]{10pt}{1.5pt}}
 }

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

我從中得到: 在此輸入影像描述

但我還沒有找到一種方法來添加水平垂直文字高度對齊線來填充文字寬度。

我嘗試的另一個選擇是使用:

%%%%% Simple + underline
\renewcommand{\sectionlinesformat}[4]{%
\Ifstr{#1}{section}{%
    \parbox[c]{\linewidth}{%
      \raggedsection{\hskip #2{\color{black}#3}}{#4}\par%
      \kern-.75\ht\strutbox\rule{\linewidth}{1pt}%
    }%
  }{%
    {\hskip #2#3}{#4}}% 
}

看,這裡\sectionlinesformat用的是\sectionformat,但不知道為什麼。為此我得到:

在此輸入影像描述

我發現的第三個選項是使用\makeatletterand \makeatother

\makeatletter
\renewcommand\@seccntformat[1]{%
  \makebox[0pt][l]{\rule[-\dimexpr\fboxsep+2pt\relax]{\columnwidth}{1.5pt}}%
  \colorbox{blue}{%
    \rule[-2pt]{0pt}{0pt}%
    \color{white}\csname the#1\endcsname
  }%
  \quad
}
\makeatother

我得到:

在此輸入影像描述

關於如何獲得所需結果的任何指導?

另外,如果我們可以選擇將節號及其框與文字或頁邊距對齊,那就太好了。

答案1

像這樣的東西嗎?

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

\renewcommand*{\sectionformat}{%
    \usekomafont{section}%
    \fboxrule=1pt\fcolorbox{blue}{white!0}{\color{black}\thesection}%
    \color{blue}\rule[.7ex]{10pt}{1pt}\>%
}
\makeatletter
\renewcommand*{\sectionlinesformat}[4]{%
    \@hangfrom{\hskip #2#3}{\color{blue}#4}\>%
    \xrfill[.7ex]{1pt}[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}

答案2

僅作為@Vincent 答案的補充:

如果只有部分(而不是小節和子小節)應使用不同的格式:

\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}{\color{black}\thesection}%
  \rule[.7ex]{10pt}{1pt}\;%
}

\NewCommandCopy{\originalsectionlinesformat}{\sectionlinesformat}
\renewcommand*{\sectionlinesformat}[4]{%
  \originalsectionlinesformat{#1}{#2}{#3}{#4\Ifstr{#1}{section}{\;\xrfill[.6ex]{1.5pt}[.]}{}}%
}

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

\begin{document}
\chapter{Introduction}
\section{Section 1}
\subsection{Subsection}
\subsubsection{Subsubsection}
\Blindtext[2]
\end{document}

在此輸入影像描述

如果小節和子小節應使用相同的格式但不同的顏色:

\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}%
  \rule[.7ex]{10pt}{1pt}\;%
}
\renewcommand*{\subsectionformat}{%
  \fboxrule=1.5pt\fcolorbox{.}{white}{\normalcolor\thesubsection}%
  \rule[.7ex]{10pt}{1pt}\;%
}
\renewcommand*{\subsectionformat}{%
  \fboxrule=1.5pt\fcolorbox{.}{white}{\normalcolor\thesubsubsection}%
  \rule[.7ex]{10pt}{1pt}\;%
}

\NewCommandCopy{\originalsectionlinesformat}{\sectionlinesformat}
\renewcommand*{\sectionlinesformat}[4]{%
  \originalsectionlinesformat{#1}{#2}{#3}{#4\;\xrfill[.6ex]{1.5pt}[.]}%
}

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

\begin{document}
\chapter{Introduction}
\section{Section 1}
\subsection{Subsection}
\subsubsection{Subsubsection}
\Blindtext[2]
\end{document}

在此輸入影像描述

相關內容