1

I try to understand what's going on with a very simple environment I defined. Please note that I have difficulties to explain the problem. I tries to do my best, but it may not be clear.

Please note that you can find the complete code, along with the result, on this link

I define an environment. And I want a vertical space of 10pt to be added on top of what's included within the tags \begin and \end.

I define something like:

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

See https://fr.sharelatex.com/learn/Environments#Defining_simple_environments: Right after the \newcommand, in between braces, you must write the name of the environment, boxed in the example. Below that are two pairs of braces. Inside the first pair of braces is set what your new environment will do before the text within, then inside the second pair of braces declare what your new environment will do after the text.

Here is the complete code (see also this permanent link):

\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}

Look at the annexe for the file "class.cls". You can also look at this permanent link.

The resulting PDF file can be found on this permanent link.

From these tests I conclude that I can't insert a blank line at the top of the environment definition or within the (first) block that appears in the environment definition. That is:

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

  { \vspace{10pt} }
}%

or

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

The important thing seems to be the presence of a blank line before the use of the environment. That is:

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

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

or

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

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

It seems that any blank line that appears within the definition of the environment (whether before the block or within the block) is just ignored.

I could go with it... but it is not satisfying. Indeed, it implies that the user (of the environment) knows that he has to insert a blank line before the tag \begin that starts the environment. I would prefer the (correct) use of the environment to be independent of the presence of a blank line before the tag \begin.

Could you explain what happens to the blank lines that appear right after \newenvironment{sectionContent} ?

Could you explain what happens to the blank lines that appear within the block { ... \vspace{10pt} } ?

ANNEXE

The file "class.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

There are several problems with your code.

  • \newenvironment expects three arguments, but you provide only two.

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

    Accidentally you have empty lines following the definitions; this corresponds to a \par token, which is interpreted by \newenvironment as the end-code. If you remove the empty line, you will get an error, since then the next \newenvironment is swallowed as end-code. This erroneous \par token may influence the space after the environment.

  • You have to be careful with spaces in the definition. Some spaces are consumed (according to fixed rules), but some spaces remain and will cause TeX to go into horizontal mode. Note that the \vspace command is only effective immediately if TeX is in vertical mode; otherwise the space is delayed until TeX is in vertical mode again. A cleaner definition is the following one.

    \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
    }
    
  • Be careful with braces ({, }). They affect the scope of commands, but may also introduce additional spaces. The TeX code

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

    will result in three spaces between the two words (the space after \vspace{10pt} is swallowed, but the others remain).

  • Note that the space produced by \vspace will disappear at the beginning or the end of the page, which probably makes sense since there is nothing to separate the text from. If you want to have the space in any case, use \vspace*.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.