章見出しのレイアウトを一時的に変更する

章見出しのレイアウトを一時的に変更する

文書内のいくつかの「特別な」章のレイアウトを変更したいのですが、その後、以前使用したレイアウトにリセットします。

\documentclass{report}
\usepackage{titlesec}

\begin{document}

\chapter{Normal Chapter}
\section{Normal Section}
\subsection{Normal Subsection}

% ask for current lay out:

\titlespacing*{\chapter}{0pt}{-40pt}{10pt}
\titleformat{\chapter}{\centering\Large\bf}{}{0pt}{}{}
\titleformat{\section}{\large\bf}{}{0pt}{}
\titleformat{\subsection}{\normalsize\it}{}{0pt}{}

\chapter{Special Chapter}
\section{Special Section}
\subsection{Special Subsection}

% reset to initial lay out

\chapter{Normal Chapter Again}
\section{Normal Section Again}
\subsection{Normal Subsection Again}

\end{document}

現在の設定を保存し、その後リセットに使用する変数を定義するにはどうすればよいでしょうか?

答え1

グループ化して範囲を制限することができます。

\documentclass{report}
\usepackage{titlesec}

\begin{document}

\chapter{Normal Chapter}
\section{Normal Section}
\subsection{Normal Subsection}

% ask for current lay out:

\begingroup
\titlespacing*{\chapter}{0pt}{-40pt}{10pt}
\titleformat{\chapter}{\centering\Large\bf}{}{0pt}{}{}
\titleformat{\section}{\large\bf}{}{0pt}{}
\titleformat{\subsection}{\normalsize\it}{}{0pt}{}

\chapter{Special Chapter}
\section{Special Section}
\subsection{Special Subsection}
\endgroup
% reset to initial lay out

\chapter{Normal Chapter Again}
\section{Normal Section Again}
\subsection{Normal Subsection Again}

\end{document}

新しい設定を保存するための新しいコマンドを定義し、以前と同じようにグループ内でこのコマンドを使用することもできます。

\documentclass{report}
\usepackage{titlesec}

\newcommand\speciallayout{
  \titlespacing*{\chapter}{0pt}{-40pt}{10pt}
  \titleformat{\chapter}{\centering\Large\bf}{}{0pt}{}{}
  \titleformat{\section}{\large\bf}{}{0pt}{}
  \titleformat{\subsection}{\normalsize\it}{}{0pt}{}
}

\begin{document}

\chapter{Normal Chapter}
\section{Normal Section}
\subsection{Normal Subsection}

% ask for current lay out:

\begingroup
\speciallayout
\chapter{Special Chapter}
\section{Special Section}
\subsection{Special Subsection}
\endgroup
% reset to initial lay out

\chapter{Normal Chapter Again}
\section{Normal Section Again}
\subsection{Normal Subsection Again}

\end{document}

標準レイアウトに同様のコマンドを定義すると、明示的にグループ化しなくても、2 つのコマンドを使用してスタイルを切り替えることができるようになります。

\documentclass{report}
\usepackage{titlesec}

\newcommand\speciallayout{
  \titlespacing*{\chapter}{0pt}{-40pt}{10pt}
  \titleformat{\chapter}{\centering\Large\bf}{}{0pt}{}{}
  \titleformat{\section}{\large\bf}{}{0pt}{}
  \titleformat{\subsection}{\normalsize\it}{}{0pt}{}
}
\newcommand\normallayout{
\titlespacing*{\chapter}{0pt}{50pt}{40pt}
\titleformat{\chapter}[display]
  {\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{20pt}{\Huge}
\titleformat{\section}
  {\normalfont\Large\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}
  {\normalfont\large\bfseries}{\thesubsection}{1em}{}
}

\begin{document}

\chapter{Normal Chapter}
\section{Normal Section}
\subsection{Normal Subsection}

\speciallayout
\chapter{Special Chapter}
\section{Special Section}
\subsection{Special Subsection}

\normallayout
\chapter{Normal Chapter Again}
\section{Normal Section Again}
\subsection{Normal Subsection Again}

\end{document}

関連情報