koma スクリプトでセクションをカスタマイズする

koma スクリプトでセクションをカスタマイズする

何かお手伝いいただけないでしょうか。scrbookクラス ドキュメントのセクション タイトルをカスタマイズしたいのですが、次のような内容です。 ここに画像の説明を入力してください

ボックス内のセクション番号、その後に中央揃えの線、セクション名、そしてテキスト幅を埋める垂直方向の中央揃えの線。

これまで、このStackExchangeで解決策を探してきましたが、成功しませんでした。Koma-Script マニュアルそして、同様のことを実行する 2 つのコマンドを見つけました: \sectionformatと です\sectionlinesformat。しかし、正直なところ、LaTeX に関する私の知識は非常に限られているため、どちらをいつ使用すればよいのか理解できません。

このフォーラムで見つかった同様の解決策を見ると、私は次のように対処しています。

\documentclass[openright]{scrbook}
\usepackage{geometry}
\geometry{a4paper,twoside, margin=3cm}
\usepackage{blindtext}% only dummy text
\usepackage{xcolor}

\renewcommand*{\sectionformat}{%
    \usekomafont{section}
    \makebox[0pt][r]{\fboxrule=1.5pt\fcolorbox{black}{white!0}{\color{black}\thesection}\rule[.7ex]{10pt}{1.5pt}}
 }

\begin{document}
    \chapter{Introduction}
    \section{Section 1}
    \Blindtext[2]
    \section{Section 2}
    \Blindtext[2]
    \section{Section 3}
    \Blindtext[2]
    \section{Section 4}
    \Blindtext[2]
\end{document}

そこから私は次のことを得ました: ここに画像の説明を入力してください

しかし、テキスト幅を埋めるために、水平 - 垂直のテキスト高さを揃えた線を追加する方法が見つかりませんでした。

私が試したもう一つのオプションは以下です:

%%%%% Simple + underline
\renewcommand{\sectionlinesformat}[4]{%
\Ifstr{#1}{section}{%
    \parbox[c]{\linewidth}{%
      \raggedsection{\hskip #2{\color{black}#3}}{#4}\par%
      \kern-.75\ht\strutbox\rule{\linewidth}{1pt}%
    }%
  }{%
    {\hskip #2#3}{#4}}% 
}

ご覧のとおり、ここでは\sectionlinesformatの代わりに が使用されています\sectionformatが、その理由はわかりません。その結果、次のようになります。

ここに画像の説明を入力してください

私が見つけた3番目のオプションは、\makeatletterと を使用することです\makeatother

\makeatletter
\renewcommand\@seccntformat[1]{%
  \makebox[0pt][l]{\rule[-\dimexpr\fboxsep+2pt\relax]{\columnwidth}{1.5pt}}%
  \colorbox{blue}{%
    \rule[-2pt]{0pt}{0pt}%
    \color{white}\csname the#1\endcsname
  }%
  \quad
}
\makeatother

そして私はこう得ます:

ここに画像の説明を入力してください

望ましい結果を得るにはどうすればよいか、何かアドバイスはありますか?

また、セクション番号とそのボックスをテキストに揃えるか、余白に配置するかを選択できると便利です。

答え1

このようなもの?

\documentclass[openright]{scrbook}
\usepackage{geometry}
\geometry{a4paper,twoside, margin=3cm}
\usepackage{blindtext}% only dummy text
\usepackage{xcolor}
\usepackage{xhfill}

\renewcommand*{\sectionformat}{%
    \usekomafont{section}%
    \fboxrule=1pt\fcolorbox{blue}{white!0}{\color{black}\thesection}%
    \color{blue}\rule[.7ex]{10pt}{1pt}\>%
}
\makeatletter
\renewcommand*{\sectionlinesformat}[4]{%
    \@hangfrom{\hskip #2#3}{\color{blue}#4}\>%
    \xrfill[.7ex]{1pt}[blue]%
}
\makeatother

\begin{document}
    \chapter{Introduction}
    \section{Section 1}
    \Blindtext[2]
    \section{Section 2}
    \Blindtext[2]
    \section{Section 3}
    \Blindtext[2]
    \section{Section 4}
    \Blindtext[2]
\end{document}

答え2

@Vincent の回答への追加としてのみ:

セクションのみ(サブセクションとサブサブセクションではない)で異なる形式を使用する場合:

\documentclass[twoside,open=right]{scrbook}
\usepackage{geometry}
\geometry{margin=3cm}
\usepackage{blindtext}% only for dummy text
\usepackage{xcolor}
\usepackage{xhfill}

\renewcommand*{\sectionformat}{%
  \fboxrule=1.5pt\fcolorbox{.}{white}{\color{black}\thesection}%
  \rule[.7ex]{10pt}{1pt}\;%
}

\NewCommandCopy{\originalsectionlinesformat}{\sectionlinesformat}
\renewcommand*{\sectionlinesformat}[4]{%
  \originalsectionlinesformat{#1}{#2}{#3}{#4\Ifstr{#1}{section}{\;\xrfill[.6ex]{1.5pt}[.]}{}}%
}

\addtokomafont{section}{\color{blue!50!black}}

\begin{document}
\chapter{Introduction}
\section{Section 1}
\subsection{Subsection}
\subsubsection{Subsubsection}
\Blindtext[2]
\end{document}

ここに画像の説明を入力してください

サブセクションとサブサブセクションで同じ形式を使用しながら異なる色を使用する場合:

\documentclass[twoside,open=right]{scrbook}
\usepackage{geometry}
\geometry{margin=3cm}
\usepackage{blindtext}% only for dummy text
\usepackage{xcolor}
\usepackage{xhfill}

\renewcommand*{\sectionformat}{%
  \fboxrule=1.5pt\fcolorbox{.}{white}{\normalcolor\thesection}%
  \rule[.7ex]{10pt}{1pt}\;%
}
\renewcommand*{\subsectionformat}{%
  \fboxrule=1.5pt\fcolorbox{.}{white}{\normalcolor\thesubsection}%
  \rule[.7ex]{10pt}{1pt}\;%
}
\renewcommand*{\subsectionformat}{%
  \fboxrule=1.5pt\fcolorbox{.}{white}{\normalcolor\thesubsubsection}%
  \rule[.7ex]{10pt}{1pt}\;%
}

\NewCommandCopy{\originalsectionlinesformat}{\sectionlinesformat}
\renewcommand*{\sectionlinesformat}[4]{%
  \originalsectionlinesformat{#1}{#2}{#3}{#4\;\xrfill[.6ex]{1.5pt}[.]}%
}

\addtokomafont{section}{\color{blue!50!black}}
\addtokomafont{subsection}{\color{green!50!black}}
\addtokomafont{subsubsection}{\color{red!50!black}}

\begin{document}
\chapter{Introduction}
\section{Section 1}
\subsection{Subsection}
\subsubsection{Subsubsection}
\Blindtext[2]
\end{document}

ここに画像の説明を入力してください

関連情報