临时修改章节标题的布局

临时修改章节标题的布局

我想修改文档中某些“特殊”章节的布局,但之后重置为之前使用的布局,即

\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}

为标准布局定义类似的命令,您现在可以使用两个命令切换样式,而无需明确分组:

\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}

相关内容