![Komascript 部分标题位于两列文本布局上](https://linux22.com/image/456518/Komascript%20%E9%83%A8%E5%88%86%E6%A0%87%E9%A2%98%E4%BD%8D%E4%BA%8E%E4%B8%A4%E5%88%97%E6%96%87%E6%9C%AC%E5%B8%83%E5%B1%80%E4%B8%8A.png)
是否可以让 \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{…}
。如果这不重要,你也可以将 的样式更改\section
为chapter
:
\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
。