You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用subfiles包时,如何让子文件单独编译不显示subsection编号?

基于subfiles包的实现方案

利用subfiles包自带的\ifSubfilesClassLoaded条件判断命令,就能优雅实现需求:主文件编译时保留子文件中subsection的编号,单独编译子文件时隐藏编号。

主文件(main.tex)无需修改

\documentclass{article}

\usepackage{subfiles}

\begin{document}

\subsection{Section title}

\subfile{sub.tex}

\subsection{Section 3 title}

\end{document}

修改子文件(sub.tex)

在导言区添加条件判断,单独编译时调整章节编号深度:

\documentclass[main.tex]{subfiles}

% 单独编译子文件时,仅对section及以上层级编号,subsection不编号
\ifSubfilesClassLoaded
  \setcounter{secnumdepth}{1}
\fi

\begin{document}

\subsection{Section 2 title} 

这里是子文件的具体内容。

\end{document}

原理说明

  • \ifSubfilesClassLoaded是subfiles包内置的条件命令:当单独编译子文件时(此时子文件以subfiles类作为主文档),执行\setcounter{secnumdepth}{1},将编号深度设为1(仅section层级会显示编号,subsection及以下不显示)。
  • 当主文件调用\subfile{sub.tex}时,子文件会继承主文档的编号设置(article类默认secnumdepth=3,即subsection会显示编号),条件判断的false分支无需额外操作。

如果需要更精准地控制单个subsection的显示(而非全局调整编号深度),也可以对每个subsection做条件判断:

\ifSubfilesClassLoaded
  \subsection*{Section 2 title}
\else
  \subsection{Section 2 title}
\fi

这种方式适合仅需隐藏特定subsection编号的场景,但全局调整编号深度的方案更简洁优雅。

内容的提问来源于stack exchange,提问作者Russell Urquhart C

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 17:05:20