作為這個問題的延續:使用 koma 腳本自訂 subsubsection
如何讓標題居中?
\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}
結果: