MHB Using an environment leaves a line by default.

  • Thread starter Thread starter caffeinemachine
  • Start date Start date
  • Tags Tags
    Line
AI Thread Summary
In LaTeX, to control spacing around text and achieve specific formatting, users can utilize commands like \hspace and \hfill. When trying to place text on the same line with controlled spacing, one approach is to use the \hfill command after the first line, allowing the second line to be positioned as desired. If the goal is to have the second line centered relative to the entire line, the command \rlap can be employed to overlay text without adding extra space. Alternatively, if the second line should be centered only in the remaining space after the first line, simply using \hfill after the first line suffices. For cases where a new line is needed without additional spacing, \hspace*{\fill} can be used effectively. These methods provide flexibility in formatting text within LaTeX documents.
caffeinemachine
Gold Member
MHB
Messages
799
Reaction score
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
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?
 
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....This is the centered line........|
This is the next line...............|
.............                      ...|
.............                      ...|
.............                      ...|
This is the first line....            ...This is the centered line.......|
This is the next line...............|
(the bar denotes where the right margin starts).
 
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....This is the centered line........|
This is the next line...............|
.............                      ...|
.............                      ...|
.............                      ...|
This is the first line....            ...This is the centered line.......|
This is the next line...............|
(the bar denotes where the right margin starts).

Most Helpful. Thank you!
 
Back
Top