恢复由 titlesec 修改的 \section 样式

恢复由 titlesec 修改的 \section 样式

\section用包修改样式后titlesec,想用\let和恢复到原来的样式\renewcommand,但是失败了,为什么会这样,如何实现?

梅威瑟:

\documentclass{article}
\usepackage{titlesec,xcolor}

\begin{document}
\section{Original Section Style}
\let\oldsection\section %store the original macro \section
\oldsection{Original Section Style stored}
\titleformat{\section}{\huge\color{red}}{\Roman{section}}{1em}{}
\section{New Section Style}
\let\section\oldsection % recovery of \section
\renewcommand{\thesection}{\arabic{section}}% I tried by another way
\section{I want to restore the original section style, but fails}
\end{document}

在此处输入图片描述

答案1

在手册的第 9.2 节中titlesec,您会发现默认样式\section

\titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}{}

因此你可以前进和后退:

\documentclass{article}
\usepackage{titlesec,xcolor}

\newcommand{\originalsections}{%
  \titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}{}%
}
\newcommand{\modifiedsections}{%
  \titleformat{\section}{\huge\color{red}}{\Roman{section}}{1em}{}%
}

\originalsections

\begin{document}

\section{Original Section Style A}
\section{Original Section Style B}

\modifiedsections

\section{New Section Style A}
\section{New Section Style B}

\originalsections

\section{Original Section Style C}

\end{document}

在此处输入图片描述

答案2

等的旧定义必须通过语句\section来表达\let

titlesec变化\section等,即

\let\oldsection\section
\usepackage{titlesec}

在此处输入图片描述

修改后的代码如下:

\documentclass{article}

\let\oldsection\section %store the original macro \section

\usepackage{titlesec}
\usepackage{xcolor}


\begin{document}

\section{Original Section Style}
\oldsection{Original Section Style stored}
\titleformat{\section}{\huge\color{red}}{\Roman{section}}{1em}{}
\section{New Section Style}
\let\section\oldsection % recovery of \section
\renewcommand{\thesection}{\arabic{section}}% I tried by another way
\section{I want to restore the original section style, but fails}
\end{document}

相关内容