将章节标题编号扩展到页边距

将章节标题编号扩展到页边距

在我之前的问题中,我得到了一个非常有用的答复,关于将 KomaScript 文章文档中的章节标题扩展到边缘:将节标题下划线延伸至边距

MWE的答案是:

\PassOptionsToPackage{svgnames,table,dvipsnames}{xcolor}
\documentclass[a4paper,10pt,oneside,DIV=9]{scrartcl}

% Packages
\usepackage{scrlayer-scrpage}
\usepackage{xcolor} % showframe loads this
\usepackage[normalem]{ulem}
\usepackage{hyperref}

% demo only
\usepackage{lipsum}
\usepackage{mwe}

% theme colours
\definecolor{MyThemeColour}{cmyk}{0.10,0.10,1.00,0} % personal yellow

\newlength{\mysecnumhang}
\setlength{\mysecnumhang}{30pt}
% KOMA and general setup
\renewcommand*{\sectionformat}{%
  \makebox[\mysecnumhang][l]{\thesection\autodot\enskip}%
}
\renewcommand*{\subsectionformat}{%
  \makebox[\mysecnumhang][l]{\thesubsection\autodot\enskip}%
}
\renewcommand*{\subsubsectionformat}{%
  \makebox[\mysecnumhang][l]{\thesubsubsection\autodot\enskip}%
}

\newbox\tmpseclinenobox
\renewcommand{\sectionlinesformat}[4]{%
  \sbox\tmpseclinenobox{#3}%
  \ifdim\wd\tmpseclinenobox>0pt\hspace{-\mysecnumhang}\fi
  \expandafter\headuline{#3#4}%
}

\newcommand\headuline{%
    \bgroup\markoverwith{\textcolor{MyThemeColour}{\rule[-0.75ex]{1pt}{1pt}}}\ULon% 2pt 2pt
    }

% recalc the text block after all the font loading
\KOMAoptions{DIV=last}

\begin{document}
\section*{Lipsum}
Some text.
\section{Lipsum}
\blinddocument
\end{document}

如果我将类别更改为scrbook并向其中添加一些“\chapter{}\s”,我似乎无法复制章节标题的效果。

有人觉得能够更新 MWE 来处理章节吗?

答案1

\chapterformat用同样的方式重新定义:

\renewcommand*{\chapterformat}{%
  \makebox[\mysecnumhang][l]{\thechapter\autodot\enskip}%
}

并重新定义\chapterlinesformat

\renewcommand{\chapterlinesformat}[3]{%
  \sbox\tmpseclinenobox{#2}%
  \ifdim\wd\tmpseclinenobox>0pt\hspace{-\mysecnumhang}\fi
  \expandafter\headuline{#2#3}%
}

或者

\renewcommand{\chapterlinesformat}[3]{%
  \Ifstr{#2}{}{}{\hspace{-\mysecnumhang}}%
  \expandafter\headuline{#2#3}%
}

例子:

\PassOptionsToPackage{svgnames,table,dvipsnames}{xcolor}
\documentclass[a4paper,10pt,oneside,DIV=9]{scrbook}

% demo only
\usepackage{mwe}

% Packages
\usepackage{scrlayer-scrpage}
\usepackage{xcolor} % showframe loads this
\usepackage[normalem]{ulem}
\usepackage{hyperref}

% theme colours
\definecolor{MyThemeColour}{cmyk}{0.10,0.10,1.00,0} % personal yellow

\newlength{\mysecnumhang}
\setlength{\mysecnumhang}{30pt}
% KOMA and general setup
\renewcommand*{\chapterformat}{%
  \makebox[\mysecnumhang][l]{\thechapter\autodot\enskip}%
}
\renewcommand*{\sectionformat}{%
  \makebox[\mysecnumhang][l]{\thesection\autodot\enskip}%
}
\renewcommand*{\subsectionformat}{%
  \makebox[\mysecnumhang][l]{\thesubsection\autodot\enskip}%
}
\renewcommand*{\subsubsectionformat}{%
  \makebox[\mysecnumhang][l]{\thesubsubsection\autodot\enskip}%
}

\renewcommand{\chapterlinesformat}[3]{%
  \Ifstr{#2}{}{}{\hspace{-\mysecnumhang}}%
  \expandafter\headuline{#2#3}%
}
\renewcommand{\sectionlinesformat}[4]{%
  \Ifstr{#3}{}{}{\hspace{-\mysecnumhang}}%
  \expandafter\headuline{#3#4}%
}

\newcommand\headuline{%
  \bgroup\markoverwith{\textcolor{MyThemeColour}{\rule[-0.75ex]{1pt}{1pt}}}\ULon% 2pt 2pt
}

% recalc the text block after all the font loading
\KOMAoptions{DIV=last}

\begin{document}
\addchap{Foo}
\addsec*{Lipsum}
Some text.
\section{Lipsum}
\blinddocument
\end{document}

补充说明:由于下划线的原因,解决方案仅限于一行标题。


关于以下评论:

如果您将标题中字母的边界框可视化(例如使用https://tex.stackexchange.com/a/57860),标题文本的对齐方式将可见:

在此处输入图片描述

图片代码:

\PassOptionsToPackage{svgnames,table,dvipsnames}{xcolor}
\documentclass[a4paper,10pt,oneside,DIV=9]{scrbook}

% demo only
\usepackage{mwe}

% Packages
\usepackage{scrlayer-scrpage}
\usepackage{xcolor} % showframe loads this
\usepackage[normalem]{ulem}
%\usepackage{hyperref}

% theme colours
\definecolor{MyThemeColour}{cmyk}{0.10,0.10,1.00,0} % personal yellow

\newlength{\mysecnumhang}
\setlength{\mysecnumhang}{30pt}
% KOMA and general setup
\renewcommand*{\chapterformat}{%
  \makebox[\mysecnumhang][l]{\thechapter\autodot\enskip}%
}
\renewcommand*{\sectionformat}{%
  \makebox[\mysecnumhang][l]{\thesection\autodot\enskip}%
}
\renewcommand*{\subsectionformat}{%
  \makebox[\mysecnumhang][l]{\thesubsection\autodot\enskip}%
}
\renewcommand*{\subsubsectionformat}{%
  \makebox[\mysecnumhang][l]{\thesubsubsection\autodot\enskip}%
}

\renewcommand{\chapterlinesformat}[3]{%
  \Ifstr{#2}{}{}{\hspace{-\mysecnumhang}}%
  \expandafter\headuline{#2#3}%
}
\renewcommand{\sectionlinesformat}[4]{%
  \Ifstr{#3}{}{}{\hspace{-\mysecnumhang}}%
  \expandafter\headuline{#3#4}%
}

\newcommand\headuline{%
  \bgroup\markoverwith{\textcolor{MyThemeColour}{\rule[-0.75ex]{1pt}{1pt}}}\ULon% 2pt 2pt
}


% recalc the text block after all the font loading
\KOMAoptions{DIV=last}

% show bounding box for each letter (https://tex.stackexchange.com/a/57860)
\makeatletter
\def\showboxes#1{%
  \begingroup\color{red}\fboxrule=.1pt \fboxsep=-\fboxrule
  \@showboxes#1\@showboxes\@empty
  \endgroup}
\def\@showboxes#1#2{%
  \ifx#2\@showboxes
    \fbox{\color{gray}#1}\expandafter\@gobble
  \else
    \setbox0=\hbox{#1\kern0pt#2}\setbox2=\hbox{#1#2}%
    \dimen0=\wd0 \advance\dimen0 -\wd2 % \dimen0 contains the kern between the two chars
    \fbox{\color{gray}#1}\kern-\dimen0
    \expandafter\@showboxes
  \fi#2}

\begin{document}
\chapter{\showboxes{Lipsum}}
\section{\showboxes{Lipsum}}
\end{document}

相关内容