3

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

How do I center the titles?

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

And here is 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}

enter image description here

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

4

You can simply add \centering in your changed code:

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

with the result:

resulting pdf

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}

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

with the result:

better resulting

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

| improve this answer | |
  • 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
  • 1
    @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
3

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

Result:

screenshot

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

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

Result:

screenshot

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.