我观察到我自制的环境存在不良行为。以下代码有时会在环境头后创建空格,有时则不会。
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{ifthen}
\newenvironment{Theorem}[1][]{%
\medskip%
\par%
\noindent%
\textbf{Theorem.}%
\ifthenelse{ \equal{#1}{} }{}{ (#1)}%
\it%
}%
{%
\smallskip\par%
}
\newenvironment{Theorem2}{%
\medskip%
\par%
\noindent%
\textbf{Theorem.}%
\it%
}%
{%
\smallskip\par%
}
\begin{document}
\begin{Theorem}
That is a cool Theorem
\end{Theorem}
\begin{Theorem}[of some famous guy]
That is a cool Theorem
\end{Theorem}
\begin{Theorem2}
another cool theorem
\end{Theorem2}
\begin{Theorem2}%
another cool theorem
\end{Theorem2}
\end{document}
结果是:
有没有办法强制 LaTeX 忽略环境定义中的所有正常空格,这样我只能通过添加空格\space
?
我可以在环境定义中添加一些内容,使其忽略环境调用后直接出现的所有空格和 par,以便
\begin{Theorem2}
some text
\end{Theorem2}
产生的结果与
\begin{Theorem2}%
some text
\end{Theorem2}
?
答案1
\ignorespaces
是的!在第一个参数的末尾使用\newenvironment
\documentclass{article}
\newenvironment{Theorem2}{%
\medskip%
\par%
\noindent%
\textbf{Theorem.}%
\itshape\ignorespaces%
}%
{%
\smallskip\par%
}
\begin{document}
\begin{Theorem2}
some text
\end{Theorem2}
\end{document}
\ignorespaces
是一个忽略下一个空格直到需要输出文本的原语。例如,当您执行此操作时,将生成\newenvironment{thm}{}{}
一个宏\thm
,而该宏又是该\begin{thm}
命令执行的最后一个宏。
答案2
是的,有:使用xparse
和利用在空间范围内\ExplSyntaxOn
被忽略的空间;可以用来~
插入一个真正的空间。
\documentclass[a4paper]{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentEnvironment{Theorem}{o}
{
\par\addvspace{\medskipamount}
\noindent
\normalfont
\textbf{Theorem.~}
\IfValueT{#1}{(#1)~}
\itshape\ignorespaces
}
{
\par\addvspace{\smallskipamount}
}
\ExplSyntaxOff
\begin{document}
Some text before the theorem to see the vertical spacing
in context.
\begin{Theorem}
That is a cool Theorem
\end{Theorem}
Some text after and before the theorem to see the vertical spacing
in context.
\begin{Theorem}[of some famous guy]
That is a cool Theorem
\end{Theorem}
Some text after the theorem to see the vertical spacing
in context.
\end{document}
我做了一些调整:\medskip
和\smallskip
在这种情况下是错误的。但是,\smallskipamount
如图所示,肯定太小了。\addvspace
垂直空间不会累积;使用您的代码,两个连续的定理将被一个小跳跃加上一个中等跳跃分开。
另一方面,\newtheorem*
fromamsthm
要简单得多。
\documentclass[a4paper]{article}
\usepackage{amsthm}
\newtheorem*{Theorem}{Theorem}
\begin{document}
Some text before the theorem to see the vertical spacing
in context.
\begin{Theorem}
That is a cool Theorem
\end{Theorem}
Some text after and before the theorem to see the vertical spacing
in context.
\begin{Theorem}[of some famous guy]
That is a cool Theorem
\end{Theorem}
Some text after the theorem to see the vertical spacing
in context.
\end{document}