Using an environment leaves a line by default.

  • MHB
  • Thread starter caffeinemachine
  • Start date
  • Tags
    Line
In summary, the conversation discusses different ways to control spacing in LaTeX and how to create a centered line without the extra spacing above and below that the center environment provides. The summary also includes examples of using plain TeX spacing commands to achieve the desired result.
  • #1
caffeinemachine
Gold Member
MHB
816
15
Suppose in LaTeX I write:

This the first line. begin{center}This is the centered line. end{center}.
What I get is:

This is the first line

This is the centered line.​
But suppose I want this:


This is the first line.~~~~~~~~~~~~~~~~This is the centered line.

(Here tilde is white space. Some how simply using spacebar wasn't working.)

What is the way to do it? My problem is that when I open any environment a fixed amount of space is always left before and after the contents of the contents of the environment. How can I control the amount of space left?
 
Physics news on Phys.org
  • #2
Well, \hspace is one way. You could write

Code:
This is the first line. $\hspace{3in}$ This is the centered line.

producing

This is the first line. $\hspace{3in}$ This is the centered line.

Then you could play around with the spacing command. It's a rather bone-headed way to do it, but also quick and dirty. It almost looks like you're wanting a two-column environment. Is that the case?
 
  • #3
caffeinemachine said:
Suppose in LaTeX I write:

This the first line. begin{center}This is the centered line. end{center}.
What I get is:

This is the first line

This is the centered line.​
But suppose I want this:


This is the first line.~~~~~~~~~~~~~~~~This is the centered line.

(Here tilde is white space. Some how simply using spacebar wasn't working.)

What is the way to do it? My problem is that when I open any environment a fixed amount of space is always left before and after the contents of the contents of the environment. How can I control the amount of space left?
One way to do this would be to go back to basics, and use the plain TeX spacing commands instead of the LaTeX center environment.

It's not clear to me whether you want "This is the centered line" to have a line to itself, but without the extra spacing above and below that the center environment provides, or whether you actually want "This is the centered line" to be on the same line as "This is the first line."

If it is to have a line to itself, that is easily done as follows:
Code:
This is the first line. \\
\hspace*{\fill} This is the centered line. \hspace*{\fill} \\
This is the next line.
That will give:

This is the first line.
This is the centered line.​
This is the next line.

But if you want both things to be on the same line, there is then a further question. Do you want "This is the centered line" to be centred on the whole line, or do you want it to be centred in the part of the line to the right of "This is the first line"? In the first case, you need this:
Code:
\leavevmode
\rlap{This is the first line.} \hfill This is the centered line. \hspace*{\fill} \\
This is the next line.

In the second case, you need this:
Code:
This is the first line. \hfill This is the centered line. \hspace*{\fill} \\
This is the next line.

The outcomes from those will look something like this:
Code:
This is the first line.[COLOR=beige]...[/COLOR]This is the centered line.[COLOR=beige].......[/COLOR]|
This is the next line.[COLOR=beige]..............[/COLOR]|
[COLOR=beige].............                      ...[/COLOR]|
[COLOR=beige].............                      ...[/COLOR]|
[COLOR=beige].............                      ...[/COLOR]|
This is the first line.[COLOR=beige]...            ...[/COLOR]This is the centered line.[COLOR=beige]......[/COLOR]|
This is the next line.[COLOR=beige]..............[/COLOR]|
(the bar denotes where the right margin starts).
 
  • #4
Opalg said:
One way to do this would be to go back to basics, and use the plain TeX spacing commands instead of the LaTeX center environment.

It's not clear to me whether you want "This is the centered line" to have a line to itself, but without the extra spacing above and below that the center environment provides, or whether you actually want "This is the centered line" to be on the same line as "This is the first line."

If it is to have a line to itself, that is easily done as follows:
Code:
This is the first line. \\
\hspace*{\fill} This is the centered line. \hspace*{\fill} \\
This is the next line.
That will give:

This is the first line.
This is the centered line.​
This is the next line.

But if you want both things to be on the same line, there is then a further question. Do you want "This is the centered line" to be centred on the whole line, or do you want it to be centred in the part of the line to the right of "This is the first line"? In the first case, you need this:
Code:
\leavevmode
\rlap{This is the first line.} \hfill This is the centered line. \hspace*{\fill} \\
This is the next line.

In the second case, you need this:
Code:
This is the first line. \hfill This is the centered line. \hspace*{\fill} \\
This is the next line.

The outcomes from those will look something like this:
Code:
This is the first line.[COLOR=beige]...[/COLOR]This is the centered line.[COLOR=beige].......[/COLOR]|
This is the next line.[COLOR=beige]..............[/COLOR]|
[COLOR=beige].............                      ...[/COLOR]|
[COLOR=beige].............                      ...[/COLOR]|
[COLOR=beige].............                      ...[/COLOR]|
This is the first line.[COLOR=beige]...            ...[/COLOR]This is the centered line.[COLOR=beige]......[/COLOR]|
This is the next line.[COLOR=beige]..............[/COLOR]|
(the bar denotes where the right margin starts).
Most Helpful. Thank you!
 
  • #5


The amount of space left before and after the contents of an environment is determined by the default settings of that environment. To control the amount of space left, you can use the \vspace command before and after the contents of the environment. For example, to add 1em of space before and after the centered line, you can use the following code:

This is the first line. \vspace{1em}

\begin{center}
This is the centered line.
\end{center}

\vspace{1em}

Alternatively, you can also use the \hspace command to add horizontal space, if you want to adjust the spacing between the two lines. For example, to add 1em of horizontal space between the two lines, you can use the following code:

This is the first line. \hspace{1em}

\begin{center}
This is the centered line.
\end{center}

\hspace{1em}

Overall, the key is to use the appropriate commands (\vspace or \hspace) with the desired amount of space to control the spacing between lines within an environment.
 

FAQ: Using an environment leaves a line by default.

What does it mean to use an environment and leave a line by default?

Using an environment in programming refers to creating a specific environment or context for executing code. Leaving a line by default means that when the environment is not specified, the code will default to using the standard or default environment.

How do I determine which environment is used by default?

The default environment can vary depending on the programming language and specific coding environment being used. In some cases, it may be specified in the code or documentation. Otherwise, you may need to consult with a more experienced programmer or do some research to determine the default environment.

Can I change the default environment?

In most cases, the default environment is set by the programming language or coding environment and cannot be changed. However, some languages or coding environments may allow for customization of the default environment or the ability to set a different default environment.

What are the advantages of using an environment and leaving a line by default?

Using an environment allows for greater control and flexibility in executing code, as different environments may have different settings or variables. Leaving a line by default also ensures that the code will run even if a specific environment is not specified, providing a safety net for the code.

Are there any potential drawbacks to using an environment and leaving a line by default?

One potential drawback is that the code may not run as intended if the default environment is not suitable for the specific task or if the default environment is changed without proper consideration. Additionally, using multiple environments in a code can sometimes lead to confusion or errors.

Similar threads

Replies
2
Views
1K
Replies
11
Views
5K
Replies
12
Views
1K
Replies
7
Views
3K
Replies
2
Views
10K
Replies
4
Views
2K
Replies
7
Views
2K
Back
Top