Komascript 部分标题位于两列文本布局上

Komascript 部分标题位于两列文本布局上

是否可以让 \section 标题打破两列流?两列布局在文档类中定义很简单:

\documentclass[a4paper,11pt,twocolumn]{scrbook}

在我的文档中,封装的工作方式如下:

\twocolumn[\section{My section header}]

但我想通过 KOMAscript 定义样式来实现,例如:

\addtokomafont{section}{\newpage\color{white}}

或根据 scrguien.pdf 2021-11-09 第 499 页

\makeatletter
  \renewcommand{\sectionlinesformat}[4]{%
    \@tempswafalse
      \Ifstr{#1}{section}{%
        \color{white}
        \newpage
        \hspace*{#2}%
        \colorbox{black}{%
          \parbox{\dimexpr\linewidth-2\fboxrule-2\fboxsep-#2}{%
            \raggedsection
            \@hangfrom{#3}{#4}%
          }%
        }%
      }{%
        \@hangfrom{\hskip #2#3}{#4}%
      }%
  }
\makeatother   

修改\linewidth\textwidth效果不佳。但这可能是正确的方法。

我的 MNWE 示例:

\documentclass[a4paper,11pt,twocolumn]{scrbook}
\usepackage{blindtext}
\usepackage{lipsum}
\usepackage{xcolor}    

%------Section-------------------------------------------
% \addtokomafont{section}{\newpage\color{white}}
% scrguien.pdf 2021-11-09 page 499
\makeatletter
  \renewcommand{\sectionlinesformat}[4]{%
    \@tempswafalse
      \Ifstr{#1}{section}{%
        \color{white}
        \newpage
        \hspace*{#2}%
        \colorbox{black}{%
          \parbox{\dimexpr\linewidth-2\fboxrule-2\fboxsep-#2}{%
            \raggedsection
            \@hangfrom{#3}{#4}%
          }%
        }%
      }{%
        \@hangfrom{\hskip #2#3}{#4}%
      }%
  }
\makeatother   

\begin{document}
\chapter{My first junk chapter over two column}
\section{I want to put there long header}
\lipsum[1-4]
\twocolumn[\section{I want to put there long header - twocolumn}]
\lipsum[1-4]
\blinddocument
\end{document}

答案1

您可以添加\twocolumn\sectionlineformats

\documentclass[a4paper,11pt,twocolumn]{scrbook}
\usepackage{blindtext}
\usepackage{lipsum}
\usepackage{xcolor}    

%------Section-------------------------------------------
\makeatletter
  \renewcommand{\sectionlinesformat}[4]{%
    \@tempswafalse
    \Ifstr{#1}{section}{%
      \twocolumn[%
        \color{white}
        \hspace*{#2}%
        \colorbox{black}{%
          \parbox{\dimexpr\linewidth-2\fboxrule-2\fboxsep-#2}{%
            \raggedsection
            \@hangfrom{#3}{#4}%
          }%
        }%
      ]%
    }{%
      \@hangfrom{\hskip #2#3}{#4}%
    }%
  }
\makeatother   

\begin{document}
\chapter{My first junk chapter over two column}
\section{I want to put there long header}
\lipsum[1-4]
\section{I want to put there long header - twocolumn}
\lipsum[1-4]
\blinddocument
\end{document}

但是因为\twocolumn 总是开始一个新页面,它也会开始一个新页面在 和 之间\chapter{…}紧接着的\section{…}。如果这不重要,你也可以将 的样式更改\sectionchapter

\documentclass[a4paper,11pt,twocolumn]{scrbook}
\usepackage{blindtext}
\usepackage{lipsum}
\usepackage{xcolor}    

\RedeclareSectionCommand[style=chapter]{section}
\makeatletter
  \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   

\begin{document}
\chapter{My first junk chapter over two column}
\section{I want to put there long header}
\lipsum[1-4]
\section{I want to put there long header - twocolumn}
\lipsum[1-4]
\blinddocument
\end{document}

如果您不想在\chapter和之间有分页符\section,您可以尝试一个技巧:

\documentclass[a4paper,11pt,twocolumn]{scrbook}
\usepackage{blindtext}
\usepackage{lipsum}
\usepackage{xcolor}    

\RedeclareSectionCommand[style=chapter]{section}
\makeatletter
  \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   

\begin{document}
\setchapterpreamble{\section{I want to put there long header}}
\chapter{My first junk chapter over two column}
\lipsum[1-4]
\section{I want to put there long header - twocolumn}
\lipsum[1-4]
\blinddocument
\end{document}

如果你不想用 开始新页面\section,你需要使用类似multicol

相关内容