使用带参数的环境导致了一个未定义的空格

发布于 2021-12-02 21:25:36

我想实现一个类似于定理类的环境,它带有一个可选的参数,下面是我的测试代码

\documentclass{ctexart}
\usepackage{xifthen}

\newcounter{numdef}[subsection]
\setcounter{numdef}{0}
\renewcommand\thenumdef{\arabic{numdef}}

\newenvironment{ntdef}[1][]{
    \vspace*{.2em}
    \newcommand\makecomment{%
        \ifthenelse{\isempty{#1}}
        {}% if the option is empty
        {#1}
    }
    \newcommand\makedef{\stepcounter{numdef}
        \textit{定义}\ \thesubsection.\thenumdef:
    }
    \noindent\makedef
}{\par\vspace{.2em}}

\begin{document}
    \section{A}
    \subsection{B}
    \noindent\textit{定义}\ 1.1.0\textit{(定义一)}:这是测试定义。\par
    \begin{ntdef}[定义一]
        这是第一条定义
    \end{ntdef}

    \begin{ntdef}
        这是第二条定义
    \end{ntdef}
\end{document}

然后我得到了下面的结果

QQ图片20211202212213.png

第一行是一个大致的理想结果。我用 xifthen 宏包判断环境所给参数是否为空,如果不为空则输出括号内容(为了演示方便,我没有加上这段代码),但是当我给了一个非空值之后,这段话结尾就会莫名出现一个短的水平空白(红线部分),这是为什么?

查看更多

关注者
0
被浏览
758
1 个回答
㡣
2021-12-02
这家伙很懒,什么也没写!

经过测试,并不是 xifthen 宏包的问题。只要为环境设置非空值,就会出现这样的空白。

问题已经解决,和 xifthen 红包无关。是由于

\begin{ntdef}[定义一]
        这是第一条定义
    \end{ntdef}

使用环境时[参数]后面的换行被视作空格导致,两个解决方法,

\begin{ntdef}[定义一]%
        这是第一条定义
    \end{ntdef}

或在环境的前定义末尾加上 \ignorespaces

\newenvironment{ntdef}[1][]{
    \vspace*{.2em}
    \newcommand\makecomment{%
        \ifthenelse{\isempty{#1}}
        {}% if the option is empty
        {1}%
    }
    \newcommand\makedef{\stepcounter{numdef}
        \textit{定义}\ \thesubsection.\thenumdef:
    }
    \noindent\makedef \ignorespaces
}{\par\vspace{.2em}}

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览