章节标题的整个文本宽度的背景颜色 - koma 脚本

章节标题的整个文本宽度的背景颜色 - koma 脚本

我有一个与这个旧问题(已经回答过)非常相似的问题:

为标题提供横跨整个字体区域的背景颜色

但是,那里提供的解决方案仅适用于章节及以下。因为我想使用 komascripts 中的“scrreprt”类,所以我希望能够为章节标题提供背景颜色。它也应该适用于以下级别。

因此,我尝试按以下方式调整给定的解决方案:

\documentclass{scrreprt}

\usepackage{xcolor}
\usepackage{lipsum}

\addtokomafont{section}{\mysection}
\newcommand{\mysection}[1]{%
\setlength{\fboxsep}{0cm}%already boxed
\colorbox{orange}{%
    \begin{minipage}{\linewidth}%
        \vspace*{2pt}%Space before
        #1
        \vspace*{2pt}%Space after
    \end{minipage}%
}}

\addtokomafont{chapter}{\mychapter}
\newcommand{\mychapter}[1]{%
\setlength{\fboxsep}{0cm}%already boxed
\colorbox{red}{%
\begin{minipage}{\linewidth}%
    \vspace*{2pt}%Space before
    #1
    \vspace*{2pt}%Space after
\end{minipage}%
}}

\begin{document}
\tableofcontents
\chapter{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long chapter}
\lipsum[1]
\section{Example section}
\lipsum[1]
\section*{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
\lipsum[1]
\end{document}

这给出了以下结果:

部分的工作背景颜色,不是章节的工作背景颜色 目录的工作背景颜色

该解决方案对 \section 有效,但对 \chapter 无效。对于 \chapter,标题会刷新到外边距。有趣的是,它似乎对自动创建的“章节”(如目录)有效。

我也试过从 koma 脚本编辑器中找到解决方案并进行了一些编辑:

\documentclass{scrreprt}
\usepackage{xcolor}

\addtokomafont{section}{%
    \makebox[0pt][l]{%
\color{red}\rule[-\dp\strutbox]{\linewidth}{\baselineskip}%
    }%
}

\addtokomafont{chapter}{%
    \makebox[0pt][l]{%
\color{red}\rule[-\dp\strutbox]{\linewidth}{\baselineskip}%
    }%
}
\begin{document}
\chapter{Chapter - not ok}
\section{Section - ok}
\end{document}

这对于章节也适用。但是对于章节,背景颜色的范围比文本的宽度更宽,如下所示:

在此处输入图片描述

Ulrike 提供的代码关联适用于章节,但仅限于章节:

\documentclass[12pt,a4paper,twoside]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\inputencoding{utf8}
\usepackage{mathpazo}
\usepackage{setspace, lipsum}
\usepackage{xcolor}
\usepackage{mdframed}
\mdfdefinestyle{chapter}{innerrightmargin=3cm,backgroundcolor=red,hidealllines=true}
\addtokomafont{chapter}
\makeatletter
\renewcommand*{\@@makechapterhead}[1]{%
\chapterheadstartvskip
\begin{mdframed}[style=chapter]
\setlength{\parindent}{\z@}\setlength{\parfillskip}{\fill}%
\normalfont\sectfont\nobreak\size@chapter{}%
  \if@chapterprefix
  \let\@tempa\raggedsection
\else
  \let\@tempa\@hangfrom
\fi
\@tempa{\ifnum \c@secnumdepth >\m@ne%
      \if@chapterprefix
        \expandafter\size@chapterprefix
      \else
        \expandafter\size@chapter
      \fi
      {\chapterformat}%
      \if@chapterprefix
        \size@chapterprefix{}\endgraf\nobreak\vskip.5\baselineskip
      \fi
  \fi
}%
{\raggedsection \interlinepenalty \@M \size@chapter{#1}\par}%
\end{mdframed}
\nobreak\chapterheadendvskip
}
\renewcommand*{\@@makeschapterhead}[1]{%
\chapterheadstartvskip%
\begin{mdframed}[style=chapter]
{\normalfont\sectfont\nobreak\size@chapter{}%
\setlength{\parindent}{\z@}\setlength{\parfillskip}{\fill}%
\raggedsection \interlinepenalty \@M \size@chapter{#1}\par}%
\end{mdframed}%
\nobreak\chapterheadendvskip%
}
\makeatother

\begin{document}
\tableofcontents

\chapter{Introduction}
\lipsum[1]
\chapter{Main Part}
\lipsum[2]
\chapter{Conclusion}
\lipsum[3]
\section{Summary}
\section{Future developments}
\end{document}

结果几乎正是我想要的:

章节工作背景

现在背景仅限于章节,而且我不懂任何 TeX,因此不知道如何将其应用于章节(可能还有小节等)。我尝试用章节替换章节,但这(显然?)没有奏效。

是否可以通过第一种方式修复标题的对齐方式,或者通过第二种方式使背景颜色变小,或者将第三种方式也适用于各个部分?

提前感谢并致以最诚挚的问候:)

答案1

因此,我找到了一个可以满足我所有需求的解决方案,尽管它可能是一个肮脏的黑客攻击:

\documentclass[12pt,a4paper,twoside]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\inputencoding{utf8}
\usepackage{lipsum}

\usepackage{xcolor}
\usepackage{mathpazo}
\usepackage{setspace, lipsum}
\usepackage{mdframed}
\mdfdefinestyle{chapter}{innerrightmargin=3cm,backgroundcolor=red,hidealllines=true}

\addtokomafont{section}{\mysection}
\newcommand{\mysection}[1]{%
\setlength{\fboxsep}{0cm}%already boxed
\colorbox{orange}{%
    \begin{minipage}{\linewidth}%
        \vspace*{2pt}%Space before
        #1
        \vspace*{2pt}%Space after
    \end{minipage}%
}}

\addtokomafont{chapter}
\makeatletter
\renewcommand*{\@@makechapterhead}[1]{%
\chapterheadstartvskip
\begin{mdframed}[style=chapter]
\setlength{\parindent}{\z@}\setlength{\parfillskip}{\fill}%
\normalfont\sectfont\nobreak\size@chapter{}%
  \if@chapterprefix
  \let\@tempa\raggedsection
\else
  \let\@tempa\@hangfrom
\fi
\@tempa{\ifnum \c@secnumdepth >\m@ne%
      \if@chapterprefix
        \expandafter\size@chapterprefix
      \else
        \expandafter\size@chapter
      \fi
      {\chapterformat}%
      \if@chapterprefix
        \size@chapterprefix{}\endgraf\nobreak\vskip.5\baselineskip
      \fi
  \fi
}%
{\raggedsection \interlinepenalty \@M \size@chapter{#1}\par}%
\end{mdframed}
\nobreak\chapterheadendvskip
}
\renewcommand*{\@@makeschapterhead}[1]{%
\chapterheadstartvskip%
\begin{mdframed}[style=chapter]
{\normalfont\sectfont\nobreak\size@chapter{}%
\setlength{\parindent}{\z@}\setlength{\parfillskip}{\fill}%
\raggedsection \interlinepenalty \@M \size@chapter{#1}\par}%
\end{mdframed}%
\nobreak\chapterheadendvskip%
}
\makeatother

\begin{document}
\tableofcontents

\chapter{A chapter}
\lipsum[1]
\section{A section}
\lipsum[2]
\chapter{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long chapter}
\lipsum[3]
\section{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
\lipsum[4]
\end{document}

这为自动创建的章节(如目录等)的标题提供了彩色背景:

彩色背景(toc)

这也为您提供了短章节和节标题的彩色背景:

彩色背景(单线)

最后但同样重要的一点是,这还为您提供了非常长的章节和章节标题的彩色背景:

彩色背景(多行)

我希望这对将来的某人有帮助:)谢谢你的帮助。

相关内容