如何让 \titleformat 不适用于附录?

如何让 \titleformat 不适用于附录?

我有一个简单的 LaTeX 代码。

我只想对附录部分使用特定的部分格式。但是,它与 有冲突\titleformat{\section}

如果我运行以下代码,附录:出现在appendix章节标题中。

\documentclass[12pt]{article}  %It can be article / report / and book 
\usepackage{amsmath}


\usepackage{titlesec}

%\titleformat{\section}    
%       {\fontsize{14}{10}\bfseries}{\thesection}{1em}{}

\titleformat{\subsection}    
       {\fontsize{12}{5}\bfseries}{\thesubsection}{1em}{}
\titleformat{\subsubsection}    
       {\fontsize{12}{5}\bfseries}{\thesubsubsection}{1em}{}

%% Set up some preparatory code -- activated fully after '\appendix'
%% (see 'The LaTeX Companion,' 2nd. ed., pp. 26f. for more details)
\makeatletter
\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
   {\csname the#1\endcsname\quad}%      default
   {\csname #1@cntformat\endcsname}%    enable individual control
}
\makeatother


\begin{document}

\section{Counting words} 
\appendix
\numberwithin{equation}{section}
\makeatletter 
% "activate" the preparatory code, but for section-level headers only
\newcommand{\section@cntformat}{Appendix \thesection:\ }
\makeatother

\section{Counting words without repetitions} 

\begin{equation} a^2+b^2=c^2 \end{equation}

\subsection{afdf}
\end{document}

但如果我让以下部分代码工作,附录没有出现。

%\titleformat{\section}    
%       {\fontsize{14}{10}\bfseries}{\thesection}{1em}{}

有什么方法可以控制整体字体大小并Appendix同时显示?

答案1

这里的问题是,当你使用 titlesec 的命令时,你会覆盖从 LaTeX 配套程序中应用的解决方案。正如你所拥有的

\titleformat{\section}
 {\fontsize{14}{10}\bfseries}{\thesection}{1em}{}

将重新定义计数器的打印,从\@seccntformat直接使用\thesection

您需要做的就是将其更改为

\makeatletter
\titleformat{\section}
 {\fontsize{14}{10}\bfseries}{\@seccntformat{section}}{1em}{}
\makeatother

你又恢复生意了

答案2

您只需使用包中\IfAppendix的命令即可。顺便说一句,在 中,第二个参数(用于基线跳过)应该比第一个参数(字体大小)多 20-25% 左右:apptools\titleformat\fontsize

\documentclass[12pt]{article} %It can be article / report / and book \usepackage{amsmath}
\usepackage{amsmath}
\usepackage{apptools}
\usepackage{titlesec}

\titleformat{\section}
 {\fontsize{14}{10}\bfseries}{\IfAppendix{\appendixname}{} \thesection}{1em}{}
%
\titleformat{\subsection}
{\fontsize{12}{15}\bfseries}{\thesubsection}{1em}{}
\titleformat{\subsubsection}
{\fontsize{12}{15}\bfseries}{\thesubsubsection}{1em}{}

\begin{document}

\section{Counting words}

\appendix \numberwithin{equation}{section}

\section{Counting words without repetitions}

\begin{equation} a^2+b^2=c^2 \end{equation}

\subsection{afdf}

\end{document} 

在此处输入图片描述

相关内容