答案1
您忘记了 定义中的“结束部分” nub
,而应该是\ignorespacesafterend
。使用您显示的代码,\makeatother
将改为 。
\documentclass{book}
\usepackage{amsmath}
\makeatletter
\newenvironment{nub}[1][\@nil]{%
\def\tmp{#1}%
\ifx\tmp\@nnil
\subsection{}
\else
\subsection{#1.}
\fi}{\ignorespacesafterend}
\makeatother
\begin{document}
\setcounter{chapter}{6}\setcounter{section}{4}\setcounter{subsection}{2}
\begin{nub}
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
\begin{equation*}
F(x)=G(x)
\end{equation*}
\end{nub}
\begin{nub}[Lemma]
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
\end{nub}
\end{document}
更简单的定义xparse
:
\documentclass{book}
\usepackage{amsmath}
\usepackage{xparse}
\NewDocumentEnvironment{nub}{o}
{\subsection{\IfValueT{#1}{#1.}}}
{\ignorespacesafterend}
我将如何实现它
\documentclass{book}
\usepackage{amsmath}
\usepackage{amsthm,xparse}
\newtheoremstyle{nub}
{\topsep} % ABOVESPACE
{\topsep} % BELOWSPACE
{\normalfont} % BODYFONT
{0pt} % INDENT (empty value is the same as 0pt)
{\bfseries} % HEADFONT
{} % HEADPUNCT
{5pt plus 1pt minus 1pt} % HEADSPACE
{(\thmnumber{#2})\thmname{#1}} % CUSTOM-HEAD-SPEC
\theoremstyle{nub}
\newtheorem{nubinner}[subsection]{\nubname}
\newcommand{\nubname}{}
\NewDocumentEnvironment{nub}{o}
{\IfValueT{#1}{\renewcommand{\nubname}{ #1.}}\begin{nubinner}}
{\end{nubinner}}
\begin{document}
\setcounter{chapter}{6}\setcounter{section}{4}\setcounter{subsection}{2}
\begin{nub}
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
\begin{equation*}
F(x)=G(x)
\end{equation*}
\end{nub}
\begin{nub}[Lemma]
text text text text text text text text text text
text text text text text text text text text text
text text text text text text text text text text
\end{nub}
\end{document}