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