# How to center headings at KOMA-Script

As a continue to this question: customize subsubsection with koma script

How do I center the titles?

\renewcommand{\sectionlinesformat}[4]{%
%\@tempswafalse
\ifstr{#1}{section}{%
}
{\ifstr{#1}{subsection}{%
}
{\@hangfrom{\hskip#2#3}{#4}}}%


And here is MWE:

\documentclass{scrartcl}

\makeatletter
\renewcommand{\sectionlinesformat}[4]{%
%\@tempswafalse
\ifstr{#1}{section}{%
}
{\ifstr{#1}{subsection}{%
}
{\@hangfrom{\hskip#2#3}{#4}}}%
}\makeatother

\begin{document}

\part{ABC}

\section{abc}

\subsection{def}
\end{document}


I want to center the section title and the subsection title.

You can simply add \centering in your changed code:

\documentclass{scrartcl}

\makeatletter
\renewcommand{\sectionlinesformat}[4]{%
%\@tempswafalse
\ifstr{#1}{section}{%
}
{\ifstr{#1}{subsection}{%
}
{\@hangfrom{\hskip#2#3}{#4}}}%
}\makeatother

\begin{document}

\part{ABC}

\section{abc}

\subsection{def}
\end{document}


with the result:

But to be honest why do you change the internal format of KOMA-Script? That is not a very good idea. Better is the following (and please omit underlining in headings, use instead bold or italic):

\documentclass{scrartcl}

\begin{document}

\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}} % <========================

\end{document}


with the result:

In this way the centering is working for long section headings too, in the first variant it can not work!

• Very nice....and thank you very much for your consideration on tags. – Sebastiano Mar 15 '19 at 12:28
• For your question: some time the document can much prettier when the titles are centered :-) BTW - If I want underlined text, this is possible? – heblyx Mar 15 '19 at 12:47
• BTW, about your second example - I want the adforn symbols will be part of the titles, i.e. - that every title they will be automatically, like the MWE that I posted - is this possible? – heblyx Mar 15 '19 at 12:52
• @heblyx well underlining is a problem in LaTeX, but see answer tex.stackexchange.com/a/9552/16550 for that problem. – Mensch Mar 15 '19 at 12:55
• Thank you!! I'll check it later :-) – heblyx Mar 15 '19 at 12:59

You can redefine \raggedsection to change the alignment of all headings set by \sectionlinesformat.

Underlining is only possible for oneline headings. So you can remove \@hangfrom for them.

\documentclass{scrartcl}
\usepackage{fontspec}
\renewcommand\raggedsection{\centering}% center headings like \section, \subsection etc.

\let\originalsectionlinesformat\sectionlinesformat
\renewcommand{\sectionlinesformat}[4]{%
{\originalsectionlinesformat{#1}{#2}{#3}{#4}}}}%

\usepackage{blindtext}% only for dummy text
\begin{document}
\section{abc}
\blindtext
\subsection{def}
\blindtext
\end{document}


Result:

If there are also longer headings, remove the (ugly) underline.

\documentclass{scrartcl}
\usepackage{fontspec}
\renewcommand\raggedsection{\centering}

\let\originalsectionlinesformat\sectionlinesformat
\renewcommand{\sectionlinesformat}[4]{%
\originalsectionlinesformat{#1}{#2}{#3}{#4%