ページの外側のセクション番号

ページの外側のセクション番号

私の質問に続いてセクション見出しのカスタマイズセクション番号のボックスを常にページの外側に配置したいと思います (twoside オプションを使用しています)。つまり、奇数ページの場合、セクション番号のボックスは右側に、偶数ページの場合、セクション番号のボックスは左側に配置されます。

偶数ページの場合:

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

奇数ページの場合:

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

ここで問題が起こります:

  1. スニペットの目的の場所にセクション番号を取得するために、コードを手動で変更しました。しかし、もちろん、これは大きなドキュメントには適していません。これを自動的に行うための支援はありますか?
  2. 奇数ページでは、偶数ページのようにボックスをテキスト領域の外側に配置することができませんでした。

もう一度言いますが、どんなご指導でも大歓迎です。

これが私の MWE です:

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

\renewcommand*{\sectionformat}{%
    \usekomafont{section}%
    \makebox[0pt][r]{%
    \fboxrule=1.5pt\fcolorbox{blue}{white!0}{\color{black}\thesection}\color{blue}\rule[.6ex]{10pt}{1.5pt}
    }%
}
\makeatletter
\renewcommand*{\sectionlinesformat}[4]{%
    \@hangfrom{\hskip #2#3}{\color{blue}#4}$\;$%
    \xrfill[.6ex]{1.5pt}[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}

ありがとう!

答え1

KOMA スクリプト コマンドを使用できます\Ifthispageodd:

\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}%
}
\renewcommand*{\subsectionformat}{%
  \fboxrule=1.5pt\fcolorbox{.}{white}{\normalcolor\thesubsection}%
}

\makeatletter
\renewcommand*{\sectionlinesformat}[4]{%
  \Ifthispageodd
    {\rule[.6ex]{10pt}{1.5pt}\;#4\;\xrfill[.6ex]{1.5pt}[.]\hspace{-1pt}\usekomafont{#1}\makebox[0pt][l]{\rule[.6ex]{11pt}{1.5pt}#3}}
    {\makebox[0pt][r]{#3\rule[.6ex]{10pt}{1.5pt}\;}#4\;\xrfill[.6ex]{1.5pt}[.]}%
}
\makeatother

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

\begin{document}
\chapter{Introduction}
\section{Section 1}
\subsection{Subsection}
\Blindtext[2]
%\vspace{4\baselineskip}
\section{Section 2}
\Blindtext[1]
%\clearpage
\section{Section 3}
\Blindtext[2]
\section{Section 4, very long section title that requires more than one line}
\Blindtext[2]
\end{document}

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

警告:\Ifthispageoddには制限があります。コマンドが 1 ページで実行されても、コンテンツが次のページに移動されると、エラーが発生します。\vspace{4\baselineskip}問題を確認するには、行のコメントを解除してください。そのため、終了したらドキュメントを制御し、たとえばを使用して問題を修正する必要があります\clearpage


追加コメント: 質問のコードでは、セクション、サブセクション、サブサブセクションの見出しに同じ形式が使用されています。この動作と、セクションとサブセクションの見出しに異なる色を使用する方法を示すために、サブセクションを追加しました。

関連情報