
我想修改文件中一些「特殊」章節的佈局,但之後重置為我之前使用的佈局,即
\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}