如何使用 KOMA 正确地为某些章节标题设置不同的样式

如何使用 KOMA 正确地为某些章节标题设置不同的样式

这是我的上一个问题提供更多详细信息。在我的文档中,有些章节有编号,有些章节没有编号(例如简介或致谢)。我设置了一个命令,\fancychapter以不同的方式设置带编号的章节的样式。

我的问题是,我修改了不同的内容,例如 KOMA 选项、字体或样式命令,因此我想使用范围在任何命令之后返回到文档默认设置\fancychapter。但是,如果第一的文档中的章节属于不同的范围,这会弄乱文档中的间距。所有这些都在链接的问题中用 MWE 描述。在这里,我按照评论中的要求发布了我的完整代码回答。

\documentclass{scrreprt}
\usepackage{calc}
\usepackage{xcolor}
\usepackage{xparse}
\usepackage{graphicx}
\usepackage{blindtext}
\KOMAoption{parskip}{false}

\makeatletter
\definecolor{chapmark}{RGB}{148,148,148}
\newcommand*\numberscale{}
\newlength{\chapter@mark@width}

% https://tex.stackexchange.com/a/406025
\gdef\gsetlength#1#2{%
  \begingroup
    \setlength\skip@{#2}% Local assignment to a scratch register.
    \global#1=\skip@    % Global assignement to #1;
                        % \relax is not necessary because of the following \endgroup.
  \endgroup             % \skip@ is restored by end of group.
}

\newcommand*{\chapter@mark}{%
  \scalebox{\numberscale}{\usekomafont{chapterprefix}\thechapter}\quad%
}
\let\chapterformat@original\chapterformat
\newcommand*{\chapterformat@decorated}{%
  \begin{minipage}[b]{\widthof{\chapter@mark}}
    \chapter@mark
  \end{minipage}%
}

% Add line below chapter title
\let\chapterheadendvskip@original\chapterheadendvskip
\newcommand*{\chapterheadendvskip@decorated}{%
  {%
    \setlength{\parskip}{0pt}%
    \setlength{\parindent}{0pt}%
    %\setlength{\chapter@mark@width}{\widthof{\chapter@mark}}%
    \par\noindent\vskip-\baselineskip%
    \makebox[0pt][l]{\raisebox{-1.2\baselineskip}[0pt][0pt]{%
      \hspace{\chapter@mark@width}\hspace{-1ex}%
      \rule[0pt]{\linewidth-\chapter@mark@width+1ex}{1pt}}}\par%
    \vskip\baselineskip
  }%
  \chapterheadendvskip@original%
}

\NewDocumentCommand\fancychapter{ o m }{%
  \begingroup
  \KOMAoption{chapterprefix}{false}
  \addtokomafont{chapterprefix}{\normalfont\fontfamily{LinuxBiolinumT-TLF}\selectfont}
  \renewcommand*\numberscale{5}
  \addtokomafont{chapterprefix}{\color{chapmark}}

  \let\chapterformat\chapterformat@decorated%
  \let\chapterheadendvskip\chapterheadendvskip@decorated%
  %
  \IfValueTF{#1}%
  {% with first optional argument:
    \chapter[#1]{%
      \gsetlength{\chapter@mark@width}{\widthof{\chapter@mark}}%
      \begin{minipage}[b]{\linewidth-\chapter@mark@width}
      #2\hbadness=10000
      \end{minipage}}%
  }{% without first optional argument:
    \chapter[#2]{%
      \gsetlength{\chapter@mark@width}{\widthof{\chapter@mark}}%
      \begin{minipage}[b]{\linewidth-\chapter@mark@width}
      #2\hbadness=10000
      \end{minipage}}%
  }
  %
  \let\chapterformat\chapterformat@original%
  \let\chapterheadendvskip\chapterheadendvskip@original%
  \endgroup
}
\makeatother

\begin{document}

\fancychapter{This}
\blindtext

\section{Foo}
\blindtext

\section{Bar}
\blindtext

\chapter{That}
\blindtext

\section{Foo}
\blindtext

\section{Bar}
\blindtext

\end{document}

我的问题是,在命令周围使用作用域似乎不是一个好主意\chapter。同时我范围,因为似乎没有办法保存并在以后恢复 KOMA 选项或 KOMA 字体的当前值,如上所述这里。我很乐意听取任何关于如何为某些章节标题正确设置不同格式的建议。

答案1

以下是一个建议,其中编号的章节自动获得不同的布局:

\documentclass{scrreprt}
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\usepackage{calc}
\usepackage{xcolor}
%\usepackage{xparse}% not needed for this suggestion
\usepackage{graphicx}
\usepackage{blindtext}
%\KOMAoption{parskip}{false}% default

\definecolor{chapmark}{RGB}{148,148,148}
\newkomafont{chapternumber}{%
  \usekomafont{chapterprefix}%
  \normalfont\fontfamily{LinuxBiolinumT-TLF}\selectfont%
  \color{chapmark}%
}
\newcommand*\numberscale{5}

\renewcommand*\chapterformat{%
  \scalebox{\numberscale}{\usekomafont{chapternumber}\thechapter}\quad%
}

\let\originalchapterlinesformat\chapterlinesformat
\renewcommand*\chapterlinesformat[3]{%
  \Ifstr{#1}{chapter}
    {%
      \IfArgIsEmpty{#2}
        {\originalchapterlinesformat{#1}{#2}{#3}}
        {%
          #2%
          \parbox[t]{\textwidth-\widthof{#2}}{%
            \parbox[b]{\linewidth}{#3}%
              \par\vskip-\baselineskip%
              \makebox[0pt][l]{\normalsize\raisebox{-1.2\baselineskip}[0pt][0pt]{%
                \hspace{-1ex}%
                \rule[0pt]{\linewidth+1ex}{1pt}}}\par%
              \vskip\baselineskip%
          }%
        }%
    }
    {\originalchapterlinesformat{#1}{#2}{#3}}%
}

\begin{document}
\chapter{This}
\blindtext
\section{Foo}
\blindtext
\section{Bar}
\blindtext

\chapter{That}
\blindtext
\section{Foo}
\blindtext
\section{Bar}
\blindtext

\tableofcontents
\end{document}

结果:

在此处输入图片描述


如果只有一些编号章节应该采用特殊格式(您真的想这样做吗?):

\documentclass[chapterprefix=true]{scrreprt}
%\providecommand*\Ifstr{\ifstr}% needed up to and including KOMA-Script version 3.27, see https://komascript.de/faq_deprecatedif
\usepackage{calc}
\usepackage{xcolor}
%\usepackage{xparse}% not needed for this suggestion
\usepackage{graphicx}
\usepackage{blindtext}
%\KOMAoption{parskip}{false}% default

\definecolor{chapmark}{RGB}{148,148,148}
\newkomafont{chapternumber}{%
  \usekomafont{chapterprefix}%
  \normalfont\fontfamily{LinuxBiolinumT-TLF}\selectfont%
  \color{chapmark}%
}
\newcommand*\numberscale{5}
\newif\ifdecoratedchapter
\newif\ifdefaultchapterprefix
\makeatletter
\newcommand\usedecoratedchapters{%
  \decoratedchaptertrue%
  \if@chapterprefix\defaultchapterprefixtrue\else\defaultchapterprefixfalse\fi%
    \KOMAoption{chapterprefix}{false}%
}
\newcommand\usedefaultchapters{%
  \ifdecoratedchapter%
    \ifdefaultchapterprefix\@chapterprefixtrue\else\@chapterprefixfalse\fi%
  \fi%
  \decoratedchapterfalse%
}
\makeatletter
\let\originalchapterformat\chapterformat
\renewcommand*\chapterformat{%
  \ifdecoratedchapter%
    \scalebox{\numberscale}{\usekomafont{chapternumber}\thechapter}\quad%
  \else%
    \originalchapterformat%
  \fi%
}

\let\originalchapterlinesformat\chapterlinesformat
\renewcommand*\chapterlinesformat[3]{%
  \Ifstr{#1}{chapter}
    {%
      \IfArgIsEmpty{#2}
        {\originalchapterlinesformat{#1}{#2}{#3}}
        {%
          \ifdecoratedchapter%
            #2%
            \parbox[t]{\textwidth-\widthof{#2}}{%
              \parbox[b]{\linewidth}{#3}%
                \par\vskip-\baselineskip%
                \makebox[0pt][l]{\normalsize\raisebox{-1.2\baselineskip}[0pt][0pt]{%
                  \hspace{-1ex}%
                  \rule[0pt]{\linewidth+1ex}{1pt}}}\par%
                \vskip\baselineskip%
            }%
          \else%
            \originalchapterlinesformat{#1}{#2}{#3}%
          \fi%
        }%
    }
    {\originalchapterlinesformat{#1}{#2}{#3}}%
}

\begin{document}
\usedecoratedchapters
\chapter{This}
\usedefaultchapters
\blindtext
\section{Foo}
\blindtext
\section{Bar}
\blindtext

\chapter{That}
\blindtext
\section{Foo}
\blindtext
\section{Bar}
\blindtext

\tableofcontents
\end{document}

结果:

在此处输入图片描述

警告:\if@chapterprefix是内部命令。因此将来可能会发生变化。

相关内容