由于调用\appendix
会将section
数字格式从arabic
改为Alpha
,我构造了一对宏,名为\appendixon
和 ,\appendixoff
其功能是使数字格式在这两个宏内不断变化。任何section
数字在 之后都会恢复到其原始格式\appendixoff
。但它们并没有像我预期的那样工作。虽然可以再次重新定义部分数字格式,但我不想重复。有没有更好的方法?
附言:
当格式section label
变为非阿拉伯数字时,Roman
例如附录中的章节标签也变为罗马数字,如何防止这种情况发生?
\documentclass{article}
\usepackage{titlesec}
\newcommand{\appendixon}[1][]{%
\bgroup\parindent0em\appendix{\LARGE\bfseries Appendix}
}
\newcommand{\appendixoff}{\egroup}
% Why do `\bgroup` and `egroup` here do not function?
\begin{document}
\section{Pre appendix}
\appendixon
\section{section of appendix}
\appendixoff
\section{Post appendix}%Here, the section title format fails to go back to its original defination.
\titleformat{\section}{\normalfont\Large\bfseries}{\Roman{section}}{1em}{}
\section{Post titleformat}
\subsection{subsection post titleformat}
\end{document}
答案1
我建议您制作另一个宏来保存章节编号,例如:
\makeatletter
\usepackage{etoolbox}
\newcounter{savedsection}% for remembering the last chapter number
\preto\appendix{\setcounter{savedsection}{\arabic{section}}}% remembering!
\newcommand\resumechapters{% the \appendix command with some tweaks
\setcounter{section}{\arabic{savedsection}}% restore chapter number
\gdef\@chapapp{\sectionname}% reset chapter name
\gdef\thesection{\@arabic\c@section}% make chapter numbers arabic
}
\makeatother
此宏可帮助您跳过或恢复章节编号,而无需重新启动。
MWE 是:
\documentclass{article}
\makeatletter
\usepackage{etoolbox}
\newcounter{savedsection}% for remembering the last chapter number
\preto\appendix{\setcounter{savedsection}{\arabic{section}}}% remembering!
\newcommand\resumechapters{% the \appendix command with some tweaks
\setcounter{section}{\arabic{savedsection}}% restore chapter number
\gdef\@chapapp{\sectionname}% reset chapter name
\gdef\thesection{\@arabic\c@section}% make chapter numbers arabic
}
\makeatother
% \let\cleardoublepage\relax% compressed output of MWE
\newcommand{\appendixon}[1][]{%
\bgroup\parindent0em\appendix{\LARGE\bfseries Appendix}
}
\newcommand{\appendixoff}{\egroup}
% Why do `\bgroup` and `egroup` here do not function?
\begin{document}
\section{Pre appendix}
\section{Pre appendix, section 1}
\appendixon
\section{section of appendix}
\section{section 2 of appendix}
\appendixoff
\resumechapters
\section{Post appendix}
\section{Post appendix, section 2}
\end{document}
更新
要使部分格式采用罗马格式,请添加以下块:
%%%%%
% You need \usepackage{apptools} for this method
\titleformat{\section}{\normalfont\Large\bfseries}{\IfAppendix{\Alph{section}}{\Roman{section}}}{1em}{}
%%%%%
梅威瑟:
\documentclass{article}
\usepackage{titlesec}
\usepackage{apptools}
\makeatletter
\usepackage{etoolbox}
\newcounter{savedsection}% for remembering the last chapter number
\preto\appendix{\setcounter{savedsection}{\arabic{section}}}% remembering!
\newcommand\resumechapters{% the \appendix command with some tweaks
\setcounter{section}{\arabic{savedsection}}% restore chapter number
\gdef\@chapapp{\sectionname}% reset chapter name
\gdef\thesection{\@arabic\c@section}% make chapter numbers arabic
}
\makeatother
%%%%%
% You need \usepackage{apptools} for this method
\titleformat{\section}{\normalfont\Large\bfseries}{\IfAppendix{\Alph{section}}{\Roman{section}}}{1em}{}
%%%%%
% \let\cleardoublepage\relax% compressed output of MWE
\newcommand{\appendixon}[1][]{%
\bgroup\parindent0em\appendix{\LARGE\bfseries Appendix}
}
\newcommand{\appendixoff}{\egroup}
% Why do `\bgroup` and `egroup` here do not function?
\begin{document}
\section{Pre appendix}
\section{Pre appendix, section 1}
\appendixon
\section{section of appendix}
\section{section 2 of appendix}
\appendixoff
\resumechapters
\section{Post appendix}
\section{Post appendix, section 2}
\end{document}
答案2
我想这就是你想要实现的:
\documentclass{article}
\makeatletter
\newenvironment{myappendix}{\par
\newcounter{tempsec}%
\setcounter{tempsec}{\thesection}%
\setcounter{section}{0}%
\setcounter{subsection}{0}%
\gdef\thesection{\@Alph\c@section}
%\clearpage % You could add \clearpage here to let the appendix start at new page
{\parindent0em\LARGE\bfseries Appendix}
}
{
\setcounter{section}{\thetempsec}
\gdef\thesection{\@arabic\c@section}
}
\makeatother
\begin{document}
\section{Pre appendix}
\subsection{Pre appendix subsection}
\begin{myappendix}
\section{Section of Appendix}
\subsection{Appendix subsection}
\end{myappendix}
\section{Post appendix}
\subsection{Post appendix subsection}
\end{document}