LaTeX \leftskip and \rightskip (including a simple introduction to LaTeX glue, or, rubber length)

Jan. 16, 2025

\leftskip and \rightskip

TeX primitive commands \leftskip and \rightskip are used to set1:

  • \rightskip: flexible spacing (glue) inserted to the right of each line in a paragraph.
  • \leftskip: flexible spacing (glue) inserted to the left of each line in a paragraph.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
\documentclass{article}
\usepackage[paperheight=16cm,paperwidth=12cm,textwidth=8cm]{geometry}
\usepackage[x11names]{xcolor}
\pagecolor{AntiqueWhite1}

\setlength{\lineskip}{3.5pt}
\setlength{\lineskiplimit}{2pt}
\setlength{\parindent}{20pt}
\setlength{\baselineskip}{12pt}

\newcommand{\mytextB}{When \TeX{} typesets a paragraph it treats each individual character as a two-dimensional ``box'' with a specific width, height and depth. \TeX{} uses those box (character) widths to calculate  linebreaks: the best (optimal) points at which to end one line and start another. Each typeset line of text is, in effect, just a ``bigger box'', producing a typeset paragraph which is a series of lines (bigger boxes) stacked vertically, separated by some white space. Words in the paragraph lines are also separated by an amount of white space which varies from line-to-line, as calculated by \TeX{} to achieve the ``optimal'' linebreaks.\par}

\begin{document}
\vspace*{\fill}
	\mytextB
	
	\vspace{5em}
	The value of \verb|\leftskip| is \the\leftskip.\\
	The value of \verb|\rightskip| is \the\rightskip.
\vspace*{\fill}

\newpage
\vspace*{\fill}
	\leftskip 10pt
	\rightskip 0pt plus 1fill
	
	\mytextB
	\vspace{5em}
	The value of \verb|\leftskip| is \the\leftskip.\\
	The value of \verb|\rightskip| is \the\rightskip.
	
\vspace*{\fill}

\newpage
\vspace*{\fill}
	\leftskip 0pt plus 1fill
	\rightskip 10pt

	\mytextB
	
	\vspace{5em}
	The value of \verb|\leftskip| is \the\leftskip.\\
	The value of \verb|\rightskip| is \the\rightskip.
\vspace*{\fill}

\newpage
\vspace*{\fill}
	\leftskip 0pt plus 1fill
	\rightskip 0pt plus 1fill
	
	\mytextB
	
	\vspace{5em}
	The value of \verb|\leftskip| is \the\leftskip.\\
	The value of \verb|\rightskip| is \the\rightskip.
\vspace*{\fill}
\end{document}

Above four examples respectively show the case of:

(1) The default value of \leftskip and \rightskip

(2) Making \leftskip fixed and \rightskip flexible1

1
2
\leftskip 10pt
\rightskip 0pt plus 1fill

fig1

Note: This image and following analogous ones are all from reference1.

(3) Making \leftskip flexible and \rightskip fixed

1
2
\leftskip 0pt plus 1fill
\rightskip 10pt

fig2

(4) Making both \leftskip and \rightskip flexible

1
2
\leftskip 0pt plus 1fill
\rightskip 0pt plus 1fill

fig3

In this case, both \leftskip and \rightskip are specified as 0pt plus 1fill, which makes them equally infinitely flexible. If we want set them as infinitely flexible but not equally, we can use commands like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
\documentclass{article}
\usepackage[paperheight=16cm,paperwidth=12cm,textwidth=8cm]{geometry}
\usepackage[x11names]{xcolor}
\pagecolor{AntiqueWhite1}

\setlength{\lineskip}{3.5pt}
\setlength{\lineskiplimit}{2pt}
\setlength{\parindent}{20pt}
\setlength{\baselineskip}{12pt}

\newcommand{\mytextB}{When \TeX{} typesets a paragraph it treats each individual character as a two-dimensional ``box'' with a specific width, height and depth. \TeX{} uses those box (character) widths to calculate  linebreaks: the best (optimal) points at which to end one line and start another. Each typeset line of text is, in effect, just a ``bigger box'', producing a typeset paragraph which is a series of lines (bigger boxes) stacked vertically, separated by some white space. Words in the paragraph lines are also separated by an amount of white space which varies from line-to-line, as calculated by \TeX{} to achieve the ``optimal'' linebreaks.\par}

\begin{document}
\vspace*{\fill}
	\mytextB
	
	\vspace{5em}
	The value of \verb|\leftskip| is \the\leftskip.\\
	The value of \verb|\rightskip| is \the\rightskip.
\vspace*{\fill}

\newpage
\vspace*{\fill}
\begingroup
	\leftskip 0pt plus 1fill
	\rightskip 0pt plus 1fill
	
	\mytextB
	
	\vspace{5em}
	The value of \verb|\leftskip| is \the\leftskip.\\
	The value of \verb|\rightskip| is \the\rightskip.
\endgroup
\vspace*{\fill}

\newpage
\vspace*{\fill}
\begingroup
	\leftskip 0pt plus 3fill
	\rightskip 0pt plus 1fill
	
	\mytextB
	
	\vspace{5em}
	The value of \verb|\leftskip| is \the\leftskip.\\
	The value of \verb|\rightskip| is \the\rightskip.
\endgroup
\vspace*{\fill}

\newpage
\vspace*{\fill}
\begingroup
	\leftskip 0pt plus 100fill
	\rightskip 0pt plus 1fill
	
	\mytextB
	
	\vspace{5em}
	The value of \verb|\leftskip| is \the\leftskip.\\
	The value of \verb|\rightskip| is \the\rightskip.
\endgroup
\vspace*{\fill}

\end{document}

Note: By the way, in this example, the code is put in a group created by \begingroup and \endgroup to make settings functional locally.


LaTeX glue (rubber length)

Actually, \leftskip and \rightskip defined like 0pt plus 1fill etc. are called LaTeX glue (or, rubber length2), a kind of flexible white space which can be stretched or shrunk according to the amount of space required1. A complete definition for a glue incorporates a fixed part, a stretchable part and a shrinkable part3:

1
<fixed part> plus <stretchable part> minus <shrinkable part>

Besides \leftskip and \rightskip, there are also some other lengths that can be defined as glue, such as line spacing (green parts in Fig. 1, Fig. 2, and Fig. 3), paragraph spacing4, the white space after words and punctuation marks3, and so forth.


References