KOMA-Scriptで見出しを中央揃えにする方法

KOMA-Scriptで見出しを中央揃えにする方法

この質問の続きとして: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}

結果:

結果のPDF

しかし、正直に言うと、なぜ 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

下線は 1 行の見出しにのみ適用できます。そのため、\@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}

結果:

スクリーンショット

関連情報