1つのセクションのみをカスタマイズする

1つのセクションのみをカスタマイズする

すでにこの質問をしましたが、回答が得られていません。解決策を見つけようとしましたが、まだ問題があります。

すべてのセクションが 1 つのスタイルで表示され、1 つのセクションのみが異なるスタイルで表示されるドキュメントを作成したいのですが、そのために 2 つのスタイルのセクションを実現しましたが、問題は、それを同じドキュメントに配置することができないことです。

異なるスタイルで表示されるセクションのコードは次のとおりです (セクション 2)。

\documentclass{book}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\definecolor{myBlue}{HTML}{0088FF}
\begin{document}
    \chapter{CHAP 1}
    \section{Section one}
    \titleformat{\section}[hang]{\Large\bfseries\sffamily\fontfamily{pag}\selectfont}%
    {\rlap{\color{myBlue}\rule[-6pt]{\textwidth}{1.2pt}}\colorbox{myBlue}{%
            \raisebox{0pt}[13pt][3pt]{ \makebox[70pt]{% height, width
                    \fontfamily{pag}\selectfont\color{white}{\thesection}}
    }}}%
    {15pt}%
    { \color{myBlue}#1
        %
    }
    \section{Section two}
    \lipsum[2]
    \lipsum[1]
    \titleformat{\section}[block]
    {\normalfont\large\bfseries}
    {\thesection}
    {1em}{#1}
    {}
    \section{Section three}
    \section{Section four}
\end{document} 

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

セクション 1、3、4 では次のコードを使用します:

\renewcommand{\section}{\@startsection{section}{1}{\z@}
{-12pt \@plus -1ex \@minus -.4ex}
{2ex \@plus.2ex }
{\normalfont\fontsize{14pt}{16pt}\fontfamily{pag}\bfseries\color{myBlue}}}

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

最終結果は次の写真のようになると思います:

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

答え1

セクション タイトルの書式設定方法を変更するために使用するコマンド (名前付き\setupnormalsectionsおよび\setupspecialsectionshere)を作成し\titleformat、ドキュメントの途中でこれらのコマンドを呼び出すことができます。

マクロが展開されたときに##挿入されるようにしたい場合は、マクロ定義で を使用する必要があることを忘れないでください。##1 そのままの 5 番目の必須引数にあります\titleformat。2 つの がない#場合、マクロ置換テキストが となっている箇所では#1、マクロを展開すると、これが#1マクロの最初の引数に置き換えられます。まず、これはここでは望ましくありません。次に、ここで問題となっているマクロは\setupnormalsectionsと であり\setupspecialsections、これらは引数を取りません。

\documentclass{book}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}
\definecolor{myBlue}{HTML}{0088FF}

\makeatletter

\newcommand*{\setupnormalsections}{%
  \titleformat{\section}[block]
    {\normalfont\fontsize{14pt}{16pt}\fontfamily{pag}\bfseries\color{myBlue}}
    {\thesection}
    {1em}{##1}
    {}%
}

\newcommand*{\setupspecialsections}{%
  \titleformat{\section}[hang]
    {\Large\bfseries\sffamily\fontfamily{pag}\selectfont}%
    {\rlap{\color{myBlue}\rule[-6pt]{\textwidth}{1.2pt}}\colorbox{myBlue}{%
      \raisebox{0pt}[13pt][3pt]{\makebox[70pt]{% height, width
          \fontfamily{pag}\selectfont\color{white}{\thesection}}%
      }}}%
    {15pt}%
    {\color{myBlue}##1}%
}

\makeatother

\setupnormalsections

\begin{document}

    \chapter{CHAP 1}
    \section{Section one}

    \setupspecialsections
    \section{Section two}

    \lipsum[2]
    \lipsum[1]

    \setupnormalsections
    \section{Section three}
    \section{Section four}

\end{document}

スクリーンショット

関連情報