继续这个问题:使用 koma 脚本自定义子部分
如何使标题居中?
\renewcommand{\sectionlinesformat}[4]{%
%\@tempswafalse
\ifstr{#1}{section}{%
\mbox{\@hangfrom{\underline{{#3}{#4}\adforn{12}}}}
}
{\ifstr{#1}{subsection}{%
\mbox{\@hangfrom{\underline{{#3}{#4}\adforn{24}}}}%
}
{\@hangfrom{\hskip#2#3}{#4}}}%
以下是 MWE:
\documentclass{scrartcl}
\usepackage{fontspec,adforn}
\makeatletter
\renewcommand{\sectionlinesformat}[4]{%
%\@tempswafalse
\ifstr{#1}{section}{%
\mbox{\@hangfrom{\underline{{#3}{#4}\adforn{12}}}}
}
{\ifstr{#1}{subsection}{%
\mbox{\@hangfrom{\underline{{#3}{#4}\adforn{24}}}}%
}
{\@hangfrom{\hskip#2#3}{#4}}}%
}\makeatother
\begin{document}
\part{ABC}
\section{abc}
\subsection{def}
\end{document}
我想将章节标题和小节标题置于中央。
答案1
您只需添加\centering
更改后的代码即可:
\documentclass{scrartcl}
\usepackage{fontspec,adforn}
\makeatletter
\renewcommand{\sectionlinesformat}[4]{%
%\@tempswafalse
\ifstr{#1}{section}{%
\centering\mbox{\@hangfrom{\underline{{#3}{#4}\adforn{12}}}}
}
{\ifstr{#1}{subsection}{%
\centering\mbox{\@hangfrom{\underline{{#3}{#4}\adforn{24}}}}%
}
{\@hangfrom{\hskip#2#3}{#4}}}%
}\makeatother
\begin{document}
\part{ABC}
\section{abc}
\subsection{def}
\end{document}
结果如下:
但说实话,你为什么要改变 KOMA-Script 的内部格式?这不是一个好主意。更好的做法是以下(请省略标题中的下划线,改用粗体或斜体):
\documentclass{scrartcl}
\usepackage{fontspec,adforn}
\begin{document}
\addtokomafont{section}{\centering} % <==============================
\addtokomafont{subsection}{\centering} % <==============================
\part{ABC}
\section{abc test test test test test test test test test test test test
test test test test test test test test test test test test test test
test test test test test test \adforn{12}} % <========================
\subsection{def \adforn{24}} % <========================================
\end{document}
结果如下:
通过这种方式,居中功能对于长节标题也有效,但在第一个变体中它无法工作!
答案2
您可以重新定义\raggedsection
来更改 设置的所有标题的对齐方式\sectionlinesformat
。
下划线仅适用于单行标题。因此,您可以将\@hangfrom
其删除。
\documentclass{scrartcl}
\usepackage{fontspec}
\usepackage{adforn}
\renewcommand\raggedsection{\centering}% center headings like \section, \subsection etc.
\let\originalsectionlinesformat\sectionlinesformat
\renewcommand{\sectionlinesformat}[4]{%
\ifstr{#1}{section}{\hskip#2\underline{#3#4\adforn{12}}}%
{\ifstr{#1}{subsection}{\hskip#2\underline{#3#4\adforn{24}}}
{\originalsectionlinesformat{#1}{#2}{#3}{#4}}}}%
\usepackage{blindtext}% only for dummy text
\begin{document}
\section{abc}
\blindtext
\subsection{def}
\blindtext
\end{document}
结果:
如果还有较长的标题,请删除(难看的)下划线。
\documentclass{scrartcl}
\usepackage{fontspec}
\usepackage{adforn}
\renewcommand\raggedsection{\centering}
\let\originalsectionlinesformat\sectionlinesformat
\renewcommand{\sectionlinesformat}[4]{%
\originalsectionlinesformat{#1}{#2}{#3}{#4%
\ifstr{#1}{section}{\adforn{12}}
{\ifstr{#1}{subsection}{\adforn{24}}{}}%
}%
}
\usepackage{blindtext}% only for dummy text
\begin{document}
\section{abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc}
\blindtext
\subsection{def}
\blindtext
\end{document}
结果: