Komascript 部分重新声明并定义页眉

Komascript 部分重新声明并定义页眉

在我的 MNWE 中,我尝试更改两列部分的样式记事本文档。同时,我希望在第一行文本为节标题的页面上也显示页眉。

在我的代码中,我丢失了页眉的设置,因为部分的行为方式与章节相同。如果页面仅包含段落,它就会按我想要的方式工作。

我尝试使用\AddtoDoHook该技术,但没有成功(因此在 MNWE 中进行了评论)。

MNWE:

% https://tex.stackexchange.com/questions/662860/komascript-section-header-over-two-column-text-layout
\documentclass[a4paper,11pt,twocolumn, chapterprefix=on]{scrbook}
\usepackage{blindtext}
\usepackage{lipsum}
\usepackage{xcolor} 
\usepackage[headsepline, automark]{scrlayer-scrpage}   
\usepackage{currfile}

\pagestyle{scrheadings}  
\setkomafont{pageheadfoot}{\normalfont\normalcolor\itshape\small}
\setkomafont{pagenumber}{\normalfont\bfseries}
\chead{\currfilename}                              
% https://tex.stackexchange.com/questions/114570/koma-script-scrpage2-footer-height 
\ihead{\pagemark}                         % scrguien page 255
\ofoot[]{}                                % remove pagenumber from outer foot
\cfoot[]{}                                % remove pagenumber from centre foot

% \AddtoDoHook appends persistent code to the hook named name.
% \@gobble: https://tex.stackexchange.com/questions/85796/why-does-gobble-take-one-argument
\RedeclareSectionCommand[style=chapter]{section}
\makeatletter
\AddtoDoHook{heading/begingroup/section}{\KOMAoptions{chapterprefix=false}\@gobble}
% \AddtoDoHook{heading/begingroup/section}{\pagestyle{scrheadings}\@gobble}
% \AddtoDoHook{heading/begingroup/section}{\ihead{\pagemark}  \@gobble}
  % \chapterlinesformat{level}{number}{text}
  \renewcommand{\chapterlinesformat}[3]{%
    \@tempswafalse
    % \Ifstr{string 1}{string 2}{then code}{else code}  page 346 scrguide-en 3.38
    \Ifstr{#1}{section}{%
        \color{white}
        \colorbox{black}{%
          \parbox{\dimexpr\linewidth-2\fboxrule-2\fboxsep}{%
            \raggedsection
            \@hangfrom{#2}{#3}%
          }%
        }%
      ]%
    }{%
      \@hangfrom{#2}{#3}%
    }%
  }
\makeatother  

答案1

如果在键的可选参数中使用style=chapter或 ,则提供宏。键设置带有标题的页面的页面样式。默认情况下,键的值为。宏用于存储键 的值。style=part\RedeclareSectionCommandpagestyle\<name>pagestylepagestylepagestyleplain\<name>pagestylepagestyle

因此你可以使用

\RedeclareSectionCommand[style=chapter,pagestyle=scrheadings]{section}

或者

\RedeclareSectionCommand[style=chapter]{section}
\renewcommand*{\sectionpagestyle}{scrheadings}

以获得期望的结果。

第一个例子:

% https://tex.stackexchange.com/questions/662860/komascript-section-header-over-two-column-text-layout
\documentclass[a4paper,11pt,twocolumn, chapterprefix=on]{scrbook}
\usepackage{blindtext}
\usepackage{lipsum}
\usepackage{xcolor} 
\usepackage[headsepline, automark]{scrlayer-scrpage}% <- sets page style scrheadings automatically
\usepackage{currfile}

\setkomafont{pageheadfoot}{\normalfont\normalcolor\itshape\small}
\setkomafont{pagenumber}{\normalfont\bfseries}
\chead{\currfilename}
\ihead{\pagemark}
\ofoot*{}
\cfoot*{}

% \AddtoDoHook appends persistent code to the hook named name.
% \@gobble: https://tex.stackexchange.com/questions/85796/why-does-gobble-take-one-argument
\RedeclareSectionCommand[style=chapter,pagestyle=scrheadings]{section}% <- changed
\makeatletter
\AddtoDoHook{heading/begingroup/section}{\KOMAoptions{chapterprefix=false}\@gobble}
  \renewcommand{\chapterlinesformat}[3]{%
    \@tempswafalse
    \Ifstr{#1}{section}{%
        \color{white}
        \colorbox{black}{%
          \parbox{\dimexpr\linewidth-2\fboxrule-2\fboxsep}{%
            \raggedsection
            \@hangfrom{#2}{#3}%
          }%
        }%
      ]%
    }{%
      \@hangfrom{#2}{#3}%
    }%
  }
\makeatother 
\usepackage{blindtext}
\begin{document}
\blinddocument
\end{document}

第二个例子:

% https://tex.stackexchange.com/questions/662860/komascript-section-header-over-two-column-text-layout
\documentclass[a4paper,11pt,twocolumn, chapterprefix=on]{scrbook}
\usepackage{blindtext}
\usepackage{lipsum}
\usepackage{xcolor} 
\usepackage[headsepline, automark]{scrlayer-scrpage}% <- sets page style scrheadings automatically
\usepackage{currfile}

\setkomafont{pageheadfoot}{\normalfont\normalcolor\itshape\small}
\setkomafont{pagenumber}{\normalfont\bfseries}
\chead{\currfilename}
\ihead{\pagemark}
\ofoot*{}
\cfoot*{}

% \AddtoDoHook appends persistent code to the hook named name.
% \@gobble: https://tex.stackexchange.com/questions/85796/why-does-gobble-take-one-argument
\RedeclareSectionCommand[style=chapter]{section}
\renewcommand*{\sectionpagestyle}{scrheadings}% <- added
\makeatletter
\AddtoDoHook{heading/begingroup/section}{\KOMAoptions{chapterprefix=false}\@gobble}
  \renewcommand{\chapterlinesformat}[3]{%
    \@tempswafalse
    \Ifstr{#1}{section}{%
        \color{white}
        \colorbox{black}{%
          \parbox{\dimexpr\linewidth-2\fboxrule-2\fboxsep}{%
            \raggedsection
            \@hangfrom{#2}{#3}%
          }%
        }%
      ]%
    }{%
      \@hangfrom{#2}{#3}%
    }%
  }
\makeatother 
\usepackage{blindtext}
\begin{document}
\blinddocument
\end{document}

答案2

解决方案很简单。

\RedeclareSectionCommand[style=chapter]{section}

使用修改

\RedeclareSectionCommand[style=chapter,pagestyle=scrheadings]{section}

正如评论中所建议的那样。

相关内容