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.

밑줄은 한 줄 제목에만 가능합니다. 그래서 당신은 \@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}

결과:

스크린샷

관련 정보