我怎样才能删除文档中的子节标题(在本例中是 PDF 文件),但仍保留目录中的标题?
代码:
\includepdf[pages=1,pagecommand=\subsection{Header1} \thispagestyle{empty}]{xx.pdf}
\includepdf[pages=2-,pagecommand=]{xx.pdf}
答案1
如果包含的 pdf 需要此功能,那么您可以使用 addtotoc={⟨page number ⟩,⟨section ⟩,⟨level ⟩,⟨heading ⟩,⟨label ⟩}
选项\includepdf
。
addtotoc={1,subsection,2,<heading>,<label>}
例子:
\documentclass{article}
\usepackage{pdfpages}
\begin{document}
\tableofcontents
\section{A section}
See subsection~\ref{subsec:H1} on page \pageref{subsec:H1}.
\includepdf[pages=-,addtotoc={1,subsection,2,Header1,subsec:H1}]{dummy}
\end{document}
结果:
答案2
不要使用\subsection{Header1}
,而是使用
\refstepcounter{subsection}
\addcontentsline{toc}{subsection}
{%
\ifnum2>\value{secnumdepth}\else\protect\numberline{\thesubsection}\fi Header1%
}
完成 MWE:
\documentclass[]{article}
\begin{document}
\tableofcontents
\section{something}
\refstepcounter{subsection}
\addcontentsline{toc}{subsection}
{%
\ifnum2>\value{secnumdepth}\else\protect\numberline{\thesubsection}\fi Header1%
}
\end{document}
结果:
如果需要多次执行此操作,也可以创建一个宏,该宏接受两个参数,即分段级别和标题,并创建目录条目而不排版标题。以下内容应适用于标准类。
\usepackage{pdftexcmds}
\makeatletter
\def\@getsecnum@fin#1\else#2\fi{\fi#1}
\def\@getsecnum@none\fi{\fi1000}
\newcommand*\@getsecnum@a[2]
{%
\fi\ifnum\pdf@strcmp{#1}{#2}=0 \@getsecnum@fin
}
\newcommand*\@getsecnum[1]
{%
\ifnum\pdf@strcmp{#1}{part}=0 \@getsecnum@fin{-1}%
\else\@getsecnum@a{#1}{chapter}{0}%
\else\@getsecnum@a{#1}{section}{1}%
\else\@getsecnum@a{#1}{subsection}{2}%
\else\@getsecnum@a{#1}{subsubsection}{3}%
\else\@getsecnum@a{#1}{paragraph}{4}%
\else\@getsecnum@a{#1}{subparagraph}{5}%
\else\@getsecnum@none
\fi
}
\newcommand\TOConlySection@[3]
{%
\ifnum#1>\c@secnumdepth
\else
\refstepcounter{#2}%
\fi
\addcontentsline{toc}{#2}
{%
\ifnum#1>\c@secnumdepth
\else
\ifnum#1=-1
\thepart\hspace{1em}%
\else
\protect\numberline{\csname the#2\endcsname}%
\fi
\fi
#3%
}%
}
\newcommand\TOConlySection[2]
{%
\expandafter\TOConlySection@\expandafter
{\romannumeral-`0\@getsecnum{#1}}{#1}{#2}%
}
用法是
\TOConlySection{part}{part heading}
\TOConlySection{section}{section heading}
\TOConlySection{subsection}{subsection heading}