有没有办法隐藏没有内容的部分?我想隐藏内容为空的部分而不修改部分计数器。这是为了制作一个简短版本的课程,只显示重要的结果,而不会破坏完整的课程结构。
\documentclass[12pt]{article}
\begin{document}
\section{With content}
Bla, bla, ...
\section{No content}
\section{With content}
Bla, bla, ...
\section{With content}
Bla, bla, ...
\section{No content}
\section{With content}
Bla, bla, ...
\end{document}
答案1
如果我理解正确,那么 David Carlisle 的答案的这个细微变化也允许带星号的变体和在非可选参数之前的可选参数。但我不提供任何形式的担保/保证。也许我忽略了一些东西。如果是这样,请提示一下。
\documentclass[12pt]{article}
\makeatletter
%%-----------------------------------------------------------------------------
%% Check whether argument is empty:
%%.............................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is empty>}%
%% {<Tokens to be delivered in case that argument
%% which is to be checked is not empty>}%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
%%.............................................................................
\newcommand\UD@CheckWhetherNull[1]{%
\romannumeral0\expandafter\@secondoftwo\string{\expandafter
\@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
\@secondoftwo\string}\expandafter\@firstoftwo\expandafter{\expandafter
\@secondoftwo\string}\expandafter\expandafter\@firstoftwo{ }{}%
\@secondoftwo}{\expandafter\expandafter\@firstoftwo{ }{}\@firstoftwo}%
}
%%-----------------------------------------------------------------------------
\newcommand\exchange[2]{#2#1}%
\@ifdefinable\zzsection{\let\zzsection\section}%
% !!!! It is assumed that \section has the following syntax:
% !!!! \section<optional star><optional argument in square brackets><non-optional argument>
\DeclareRobustCommand\section{%
\kernel@ifnextchar*{\@firstoftwo{\mysection{*}}}{\mysection{}}%
}%
\newcommand\mysection[1]{%
\kernel@ifnextchar[{\mysection@opt{#1}}{\mysection@noopt{#1}}%
}%
\@ifdefinable\mysection@opt{%
\long\def\mysection@opt#1[#2]#3{\mychecknextsection{#1}{#1[{#2}]{#3}}}%
}%
\@ifdefinable\mysection@noopt{%
\long\def\mysection@noopt#1#2{\mychecknextsection{#1}{#1{#2}}}%
}%
\newcommand\mychecknextsection[2]{%
% #1 star or emptiness; #2 arguments of the section-command.
\kernel@ifnextchar{\section}%
{% use token-register \toks@ for avoiding expansion of the things that get typed out.
\expandafter\exchange\expandafter{%
\expandafter\toks@\expandafter{\the\toks@}%
}{%
\toks@{zap \section#2}%
\typeout{\the\toks@}%
}%
\UD@CheckWhetherNull{#1}{\refstepcounter{section}}{}%
}%
{\kernel@ifnextchar{\par}{\zzparsection\section#2}{\zzsection#2}}%
}%
\@ifdefinable\zzparsection{\long\def\zzparsection#1\par{#1}}%
\makeatother
\begin{document}
\section{With content}
Bla, bla, ...
\section*{No content}
\section{No content}
\section*{Some content}
Bla
\section*{No content}
% E.g., this one would have broken things in the version before the edit:
\section[No \textit{content} in contents]{No content}
\section*{No content}
\section{With content}
Bla, bla, ...
\section{With content}
Bla, bla, ...
\section{No content}
\section{With content}
Bla, bla, ...
\end{document}
当然还有
\section{No content}
\relax
\section{Content}
或者
\begingroup
% whatsoever
\section{No content}
\endgroup
\section{Content}
这两个部分都将被视为具有内容的部分。
答案2
不是完全强大但是...
此版本不支持*
表格或可选参数。
\documentclass[12pt]{article}
\makeatletter
\let\zzsection\section
\def\section#1{%
\@ifnextchar{\section}%
{\typeout{zap #1}\refstepcounter{section}}%
{\@ifnextchar{\par}{\zzparsection{#1}}{\zzsection{#1}}}%
}
\def\zzparsection#1\par{\section{#1}}
\makeatother
\begin{document}
\section{With content}
Bla, bla, ...
\section{No content}
\section{With content}
Bla, bla, ...
\section{With content}
Bla, bla, ...
\section{No content}
\section{With content}
Bla, bla, ...
\end{document}