附件

附件

我尝试了解我定义的一个非常简单的环境中发生了什么。请注意,我很难解释这个问题。我尽力了,但可能不太清楚。

请注意,您可以在此处找到完整的代码以及结果关联

\begin我定义了一个环境。我想在标签和中包含的内容之上添加 10pt 的垂直空间\end

我定义如下:

\newenvironment{sectionContent1}{
  { \vspace{10pt} }
}%

https://fr.sharelatex.com/learn/Environments#Defining_simple_environments:在 \newcommand 之后的括号之间,您必须写上环境的名称,如示例中的方框所示。下面是两对括号。第一对括号内设置了您的新环境将执行的操作内的文本,然后在第二对括号内声明新环境在文本之后将做什么。

以下是完整的代码(另请参阅此永久链接):

\documentclass{class}

\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt}
\setlength{\fboxsep}{0pt}

% Test 1 and 2
% Please note the blank line just above the block "{ \vspace{10pt} }"
\newenvironment{sectionContent1}{

  { \vspace{10pt} }
}%

% Test 3
% Please note the absence of a blank line just above the block "{ \vspace{10pt} }"
\newenvironment{sectionContent2}{
  { \vspace{10pt} }
}%

% Test 4
% The presence of a blanck line between the upper box and the block "{ \vspace{10pt} }" is very important.
% How to insert this blanck line in the block that contains the LaTeX code that is inserted before the text
% when the environment is used ? (https://fr.sharelatex.com/learn/Environments#Defining_a_new_environment)

\newenvironment{sectionContent3}{
  {
     \vspace{10pt} }
}%

\begin{document}

    % Test 1
    % There is no blank line before "\begin{sectionContent1}".
    % The result is not what I expect it to be. The vertical space between the bottom 
    % of the upper box and the text within the section is too high (it should be 10pt).

    \framebox[\textwidth][c]{Before the section} 
    \begin{sectionContent1}
       \textbf{Test 1}: in the section. The vertical space between the bottom of the upper box and the text
       within the section is too high (it should be 10pt).
    \end{sectionContent1}
    \framebox[\textwidth][c]{After the section}

    % Test 2
    % There is a blank line before "\begin{sectionContent1}".
    % The result is what I expect it to be.

    \framebox[\textwidth][c]{Before the section}

    \begin{sectionContent1}
       \textbf{Test 2}: in the section. The result is what I expect it to be.
    \end{sectionContent1}
    \framebox[\textwidth][c]{After the section}

    % Test 3
    % There is a blank line before "\begin{sectionContent2}".
    % The result is what I expect it to be.

    \framebox[\textwidth][c]{Before the section}

    \begin{sectionContent2}
       \textbf{Test 3}: in the section. The result is what I expect it to be.
    \end{sectionContent2}
    \framebox[\textwidth][c]{After the section}

    % Test 4
    % I tried to put the blank line in the environment definition.
    % But it does not work.

    \framebox[\textwidth][c]{Before the section}
    \begin{sectionContent3}
       \textbf{Test 4}: in the section.
    \end{sectionContent3}
    \framebox[\textwidth][c]{After the section}

\end{document}

请查看附件中的“class.cls”文件。您也可以查看此文件永久链接

生成的 PDF 文件可以在以下位置找到:此永久链接

从这些测试中,我得出结论,我无法在环境定义的顶部或环境定义中出现的(第一个)块内插入空白行。即:

% A blank line before the first block
\newenvironment{sectionContent1}{

  { \vspace{10pt} }
}%

或者

% A blank line within the first block.
\newenvironment{sectionContent3}{
  {
     \vspace{10pt} }
}%

最重要的似乎是有一个空白行使用前环境。 那是:

\framebox[\textwidth][c]{Before the section}

\begin{sectionContent1}
   \textbf{Test 2}: in the section. 
\end{sectionContent1}
\framebox[\textwidth][c]{After the section}

或者

\framebox[\textwidth][c]{Before the section}

\begin{sectionContent2}
   \textbf{Test 3}: in the section.
\end{sectionContent2}
\framebox[\textwidth][c]{After the section}

似乎出现的任何空白行之内环境的定义(无论是在块之前还是在块内)都被忽略。

我可以接受它……但它并不令人满意。事实上,它意味着(环境的)用户知道他必须在\begin启动环境的标签前插入一个空行。我更希望环境的(正确)使用与标签前是否存在空行无关\begin

您能解释一下紧接着出现的空行发生了什么吗\newenvironment{sectionContent}

您能解释一下块内出现的空行发生了什么吗{ ... \vspace{10pt} }

附件

文件 ”类.cls

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{class}[1995/10/30 Standard LaTeX minimal class]

% This class "article" defines many important things.
% See https://tex.stackexchange.com/questions/440462/hline-does-not-produce-anything/440468#440468
\LoadClass{article}

% [geometry] This package provides a flexible and easy interface to page dimensions.
\usepackage[a4paper,
            % We set the dimensions of the left and right margins.
            left=1cm,
            right=1cm,
            % We set the dimensions of the top and bottom margins.
            top=1cm,
            bottom=1cm]{geometry}

% We (must) redefine the font size named "normalsize" (this is mandatory).
% Please note that LaTeX defines other names for font sizes (for example: tiny, small...).
% See https://www.sharelatex.com/learn/Font_sizes,_families,_and_styles#Reference_guide
% WARNING: Make sure to redefine the command "\normalsize" before loading the package "fontspec".
\renewcommand{\normalsize}{\fontsize{10pt}{12pt}\selectfont}

答案1

您的代码存在几个问题。

  • \newenvironment期望参数,但你只提供了两个。

    \newenvironment{name}{begin-code}{end-code}
    

    定义后面偶然出现了空行;这对应于一个\par标记,被解释为\newenvironment结束代码。如果删除空行,则会出现错误,因为下一个空行\newenvironment会被当作结束代码。这个错误的\par标记可能会影响空格环境。

  • 您必须小心定义中的空格。一些空格会被消耗(根据固定规则),但一些空格会保留下来,并会导致 TeX 进入水平模式。请注意,\vspace只有当 TeX 处于垂直模式时,该命令才会立即生效;否则空格会被延迟,直到 TeX 再次处于垂直模式。更清晰的定义如下。

    \newenvironment{sectionContent}{% The comment sign ensures that there is no extra space
      % The next \par ensures that we are in vertical mode
      \par% Here the comment sign is not really needed as TeX gobbles the space after an ordinary macro name anyway
      \vspace{10pt}% As it happens, \vspace gobbles the space following it, but in general you can't be sure what a macro does; better safe than sorry.
    }{% This is the code for the end of the environment. Obligatory!
      \par% This \par ensures that the text following the env starts a new paragraph
    }
    
  • 小心使用括号 ( {, })。它们会影响命令的范围,但也可能引入额外的空格。TeX 代码

    word{ { \vspace{10pt} } }word
    

    将导致两个单词之间的空格(后面的空格\vspace{10pt}被吞掉,但其他的空格保留)。

  • 请注意, 产生的空格\vspace将在页面的开头或结尾消失,这可能是有道理的,因为没有任何东西可以将文本与文本分开。如果您无论如何都想有空格,请使用\vspace*

相关内容