• Create BookmarkCreate Bookmark
• Create Note or TagCreate Note or Tag
• PrintPrint

### 3.5. Lines and columns

In the last part of this chapter we present a few packages that help in manipulating the text stream in its entirety. The first package deals with attaching line numbers to paragraphs, supporting automatic references to them. This can be useful in critical editions and other scholarly works.

The second package deals with the problem of presenting two text streams side by side—for example, some original and its translation. We will show how both packages can be combined in standard cases.

The third package deals with layouts having multiple columns. It allows switching between different numbers of columns on the same page and supports balancing textual data. Standard LaTeX already offers the possibility of typesetting text in one- or two-column mode, but one- and two-column output cannot be mixed on the same page.

We conclude by introducing a package that allows you to mark the modifications in your source with vertical bars in the margin.

#### 3.5.1. lineno—Numbering lines of text

In certain applications it is useful or even necessary to number the lines of paragraphs to be able to refer to them. As TeX optimizes the line breaking over the whole paragraph, it is ill equipped to provide such a facility, since technically line breaking happens at a very late stage during the processing, just before the final pages are constructed. At that point macro processing, which could add the right line number or handle automatic references, has already taken place. Hence, the only way to achieve line numbering is by deconstructing the completed page line by line in the “output routine” (i.e., the part of LaTeX, that normally breaks the paragraph galley into pages and adds running headers and footers) and attaching the appropriate line numbers at that stage.

This approach was taken by Stephan Böttcher in his lineno package. Although one would expect such an undertaking to work only in a restricted environment, his package is surprisingly robust and works seamlessly with many other packages—even those that modify the LaTeX output routine, such as ftnright, multicol, and wrapfig. It also supports layouts produced with the twocolumn option of the standard LaTeX classes.

Loading the lineno package has no direct effect: to activate line numbering, a \linenumbers command must be specified in the preamble or at some point in the document. The command \nolinenumbers deactivates line numbering again. Line numbering works on a per-paragraph basis. Thus, when LaTeX sees the end of a paragraph, it checks whether line numbering is currently requested and, if so, attaches numbers to all lines of that paragraph. It is therefore best to put these commands between paragraphs rather than within them.

The \linenumbers command can take an optional argument that denotes the number to use for the first line. If used without such an argument, it continues from where it stopped numbering previously. You can also use a star form, which is a shorthand for \linenumbers[1].

3-5-1

Numbering boxed paragraphs

Rather than starting or stopping line numbering with the above commands, you can use the environment linenumbers to define the region that should get line numbers. This environment will automatically issue a \par command at the end to terminate the current paragraph. If line numbers are needed only for short passages, the environment form (or one of the special environments numquote and numquotation described later) is preferable.

As the production of line numbers involves the output routine, numbering will take place only for paragraphs being built and put on the “main vertical list” but not for those built inside boxes (e.g., not inside a \marginpar or within the body of a float). However, the package offers some limited support for numbering lines in such places via the \internallinenumbers command. Restrictions are that the baselines within such paragraphs need to be a fixed distance apart (otherwise, the numbers will not get positioned correctly) and that you may have to end such paragraphs with explicit \par commands. The \internallinenumbers command accepts a star and an optional argument just as \linenumbers does. However, the starred form not only ensures that line numbering is (re)started with 1, but also that the line numbers do not affect line numbering in the main vertical list; compare the results in the two \marginpars below.

3-5-2

Handling display math

The line numbers in the second \marginpar continue the numbering on the main vertical list (the last line of the preceding paragraph was 5) and the third paragraph then continues with line number 9. Such \marginpar commands are processed before the paragraph containing them is broken into lines, which explains the ordering of the numbers.

As lineno needs \par to attach line numbers when the output routine is invoked, a TeXnical problem arises when certain display math constructs are used: the partial paragraph above such a display is broken into lines by TeX without issuing a \par. As a consequence, without further help such a partial paragraph will not get any line numbers attached. The package’s solution, as illustrated in the next example, is to offer the environment linenomath, which, if it surrounds such a display, will take care of the line numbering problem. It also has a starred form that also numbers the display lines.

3-5-3

Cross-references to line numbers

If there are many such displays the need for surrounding each of them with a linenomath environment is cumbersome. For this reason the package offers the option displaymath, which redefines the basic LaTeX math display environments so that they internally use linenomath environments. The option mathlines will make linenomath behave like its starred form so that the displayed mathematical formulas get line numbers as well.

3-5-4

To reference line numbers put a \linelabel into the line and then refer to it via \ref or \pageref, just as with other references defined using \label. The exception is that \linelabel can only be used on the main vertical list and should only be used within paragraphs that actually carry numbers. If it is used elsewhere, you get either a bogus reference (if the current line does not have a line number) or an error message (in places where \linelabel is not allowed).

3-5-5

Labeling only some lines

It is also possible to refer to a line that carries no \linelabel, by using the \lineref command with an optional argument specifying the offset. This ability can be useful if you need to refer to a line that cannot be easily labeled, such as a math display, or if you wish to refer to a sequence of lines, as in the previous example.

There are several ways to customize the visual appearance of line numbers. Specifying the option modulo means that line numbers will only appear on some lines (default is every fifth). This effect can also be achieved by using the command \modulolinenumbers. Calling this command with an optional argument attaches numbers to lines that are multiples of the specified number (in particular, a value of 1 corresponds to normal numbering). Neither command nor option initiates line numbering mode, for that a \linenumbers command is still necessary.

3-5-6

The font for line numbers is controlled by the hook \linenumberfont. Its default definition is to use tiny sans serif digits. The numbers are put flush right in a box of width \linenumberwidth. This box is separated from the line by the value stored in \linenumbersep. To set the number flush left you have to dig deeper, but even for this case you will find hooks like \makeLineNumberRight in the package. Although changing the settings in the middle of a document is usually not a good idea, it was done in the next example for demonstration purposes.

3-5-7

For special applications the package offers two environments that provide line numbers automatically: numquote and numquotation. They are like their LaTeX cousins quote and quotation, except that their lines are numbered. They accept an optional argument denoting the line number with which to start (if the argument is omitted, they restart with 1) and they have starred forms that will suppress reseting the line numbers.

The main difference from their LaTeX counterparts (when used together with the \linenumbers command) is the positioning of the numbers, which are indented inward. Thus, their intended use is for cases when only the quoted text should receive line numbers that can be referenced separately.

3-5-8

Using the machinery provided by the package material, it is fairly easy to develop your own environments that attach special items to each line. The main macro to customize is \makeLineNumber, which gets executed inside a box of zero width at the left edge of each line (when line numbering mode is turned on). The net effect of your code should take up no space, so it is best to operate with \llap or \rlap. Apart from that you can use basically anything. You should only remember that the material is processed and attached after the paragraph has been broken into lines and normal macro-processing has finished, so, you should not expect it to interact with data in mid-paragraph. You can produce the current line number with the \LineNumber command, which will supply the number or nothing, depending on whether line numbering mode is on.

The following example shows the definition and use of two new environments that (albeit somewhat crudely, as they do not care about setting fonts and the like) demonstrate some of the possibilities. Note that even though the second environment does not print any line numbers, the lines are internally counted, so that line numbering resumes afterwards with the correct value.

3-5-9

The appearance and behavior of the line numbers can be further controlled by a set of options or, alternatively, by a set of commands equivalent to the options (see the package documentation for details on the command forms). With the options left (default) and right, you specify in which margin the line numbers should appear. Using the option switch or switch*, you get them in the outer and inner margins, respectively.

At least two LaTeX runs of the document are required before the line numbers will appear in the appropriate place. Unfortunately, there is no warning about the need to rerun the document, so you have to watch out for this issue yourself.

\verb is allowed

You can also request that numbers restart on each page by specifying the option pagewise. This option needs to come last.

#### 3.5.2. parallel—Two text streams aligned

Sometimes it is necessary to typeset something in parallel columns, such as when presenting some text and its translation. Parallel in this context means that at certain synchronization points the two text streams are vertically (re)aligned. This type of layout is normally not supported by LaTeX (which by default only works with a single text stream), but it can be achieved by using Matthias Eckermann’s parallel package.

This package provides the Parallel environment, which surrounds the material to be typeset in parallel. It takes two mandatory arguments: the widths of the left and right columns. Their sum should be less than \textwidth; otherwise, the text in the two columns will touch or even overlap. To ease usage, one or both arguments can be left empty, in which case the appropriate width for the column(s) will be calculated automatically, using the current value of \ParallelUserMidSkip as the column separation. To mark up the left and the right text streams, you use \ParallelLText and \ParallelRText, respectively. Although both commands expect the text as an argument, it is nevertheless possible to use \verb or a verbatim environment inside, as the following example shows.

3-5-10

To align certain lines of text you split the two text streams at appropriate points by using pairs of \ParallelLText and \ParallelRText commands and separating each pair with \ParallelPar. If you forget one of the \ParallelPar commands, some of your text will get lost without warning. Moreover, as its name suggests, the \ParallelPar command introduces a paragraph break, so that alignment is possible only at paragraph boundaries. Additional paragraph breaks inside the argument of a \Parallel..Text command are also possible but in that case no alignment is attempted.

In the next example, displaying a few “direct” translations of computer jargon into German (taken from [54] with kind permission by Eichborn Verlag), we define a shorthand command \LR to make it easier to input the text. If such a shorthand is used, \verb can no longer be used in the argument. Thus, if you need \verb, use the package commands directly. We also use the lineno package since line numbers can be useful when talking about a text and its translation.

3-5-11

As you can see, it is possible to adjust paragraph parameters within the scope of the Parallel environment. The negative \parindent cancels the positive \leftskip so that each paragraph starts flush left but following lines are indented by \leftskip (and both must be changed after calling \raggedright, as the latter also sets these registers).

The Parallel environment works by aligning line by line, which has a surprising consequence when one block contains unusually large objects, such as a display. Thus, the method is suitable only for normal text lines.

3-5-12

Footnotes in parallel text

Footnotes within the parallel text are not placed at the bottom of the current page, but rather are typeset directly after the end of the current Parallel environment and separated from it by the result of executing \ParallelAtEnd, which is a command defined to do nothing. You can, however, redefine it to place something between footnotes and preceding text. If the redefinition should apply only to a single Parallel environment, place it within the scope of the environment.

The presentation of the footnotes is controlled by four package options: OldStyleNums sets footnote numbers using old-style numerals, RaiseNums generates raised footnote numbers, and ItalicNums produces italic numbers. If none of these options is given, then Arabic numerals at the baseline position are used. The options affect only the numbers in front of the footnote text; the markers within the parallel text are always raised Arabic numerals. The fourth option, SeparatedFootnotes, can be combined with one of the three other options and indicates that footnotes in each column should be independently numbered. The numbers from the right column are then postfixed with \ParallelDot, which by default produces a centered dot. In the next example its definition is slightly modified so that the dot itself does not take up any space.

3-5-13

The Parallel environment can sport an optional argument before the mandatory ones, whose value can be c (make two columns—the default), v (separate columns with a vertical rule as shown in the previous example), or p (put left text on left-hand pages and right text on right-hand pages). If the “page” variant is chosen it is possible that you get empty pages. For example, if you are on a verso page the environment has to skip to the next recto page in order to display the texts on facing pages.

#### 3.5.3. multicol—A flexible way to handle multiple columns

With standard LaTeX it is possible to produce documents with one or two columns (using the class option twocolumn). However, it is impossible to produce only parts of a page in two-column format as the commands \twocolumn and \onecolumn always start a fresh page. Additionally, the columns are never balanced, which sometimes results in a slightly weird distribution of the material.

The multicol package1 by Frank Mittelbach solves these problems by defining an environment, multicols, with the following properties:

1 Although the multicol package is distributed under LPPL (LaTeX Project Public License) [111], for historical reasons its copyright contains an additional “moral obligation” clause that asks commercial users to consider paying a license fee to the author or the LaTeX3 fund for their use of the package. For details see the head of the package file itself.

• Support is provided for 2–10 columns, which can run for several pages.

• When the environment ends, the columns on the last page are balanced so that they are all of nearly equal length.

• The environment can be used inside other environments, such as figure or minipage, where it will produce a box containing the text distributed into the requested number of columns. Thus, you no longer need to hand-format your layout in such cases.

• Between individual columns, vertical rules of user-defined widths can be inserted.

• The formatting can be customized globally or for individual environments.

Normally, you can start the environment simply by specifying the number of desired columns. By default paragraphs will be justified, but with narrow measures—as in the examples—they would be better set unjustified as we show later on.

3-5-14

You may be interested in prefixing the multicolumn text with a bit of single-column material. This can be achieved by using the optional preface argument. LaTeX will then try to keep the text from this argument and the start of the multi-column text on the same page.

3-5-15

The multicols environment starts a new page if there is not enough free space left on the current page. The amount of free space is controlled by a global parameter. However, when using the optional argument the default setting for this parameter may be too small. In this case you can either change the global default (see below) or adjust the value for the current environment by using a second optional skip argument as follows:

This would start a new page if less than 7cm free vertical space was available.

Preventing balancing

The multicols environment balances the columns on the last page (it was originally developed for exactly this purpose). If this effect is not desired you can use the multicols* variant instead. Of course, this environment works only in the main vertical galley, since inside a box one has to balance the columns to determine a column height.

The multicols environment recognizes several formatting parameters. Their meanings are described in the following sections. The default values can be found in Table 3.8 (dimensions) and Table 3.9 (counters). If not stated otherwise, all changes to the parameters have to be placed before the start of the environment to which they should apply.

Table 3.8. Length parameters used by multicols

Table 3.9. Counters used by multicols

The required free space

The multicols environment first checks whether the amount of free space left on the page is at least equal to \premulticols or to the value of the second optional argument, when specified. If the requested space is not available, a \newpage is issued. A new page is also started at the end of the environment if the remaining space is less than \postmulticols. Before and after the environment, a vertical space of length \multicolsep is placed.

Column width and separation

The column width inside the multicols environment will automatically be calculated based on the number of requested columns and the current value of \linewidth. It will then be stored in \columnwidth. Between columns a space of \columnsep is left.

Between any two columns, a rule of width \columnseprule is placed. If this parameter is set to 0pt (the default), the rule is suppressed. If you choose a rule width larger than the column separation, the rule will overprint the column text.

3-5-16
##### Column formatting

By default (the \flushcolumns setting), the multicols environment tries to typeset all columns with the same length by stretching the available vertical space inside the columns. If you specify \raggedcolumns the surplus space will instead be placed at the bottom of each column.

Paragraphs are formatted using the default parameter settings (as described in Sections 3.1.11 and 3.1.12) with the exception of \pretolerance and \tolerance, for which the current values of \multicolpretolerance and \multicoltolerance are used, respectively. The defaults are -1 and 9999, so that the paragraph-breaking trial without hyphenation is skipped and relatively bad paragraphs are allowed (accounting for the fact that the columns are typically very narrow). If the columns are wide enough, you might wish to change these defaults to something more restrictive, such as

Note the somewhat uncommon assignment form: \multicoltolerance is an internal TeX counter and is controlled in exactly the same way as \tolerance.

##### Balancing control1

1 Very bad for reading but too good to fix: this problem of a break-stack with “the” four times in a row will not be detected by TeX’s paragraph algorithm—only a complete paragraph rewrite would resolve it.

At the end of the multicols environment, remaining text will be balanced to
produce columns of roughly equal length. If you wish to place more text in the
left columns you can advance the counter unbalance. This counter determines
the number of additional lines in the columns in comparison to the number that
the balancing routine has calculated. It will automatically be restored to zero after
the environment has finished. To demonstrate the effect, the next example uses
the text from Example 3-5-16 on the facing page but requests one extra line.

3-5-17

Column balancing is further controlled by the two counters columnbadness and finalcolumnbadness. Whenever LaTeX is constructing boxes (such as a column) it will compute a badness value expressing the quality of the box—that is, the amount of excess white space. A zero value is optimal, and a value of 10000 is infinitely bad in LaTeX’s eyes.2 While balancing, the algorithm compares the badness of possible solutions and, if any column except the last one has a badness higher than columnbadness, the solution is ignored. When the algorithm finally finds a solution, it looks at the badness in the last column. If it is larger than finalcolumnbadness, it will typeset this column with the excess space placed at the bottom, allowing it to come out short.

2 For an overfull box the badness value is set to 100000 by TeX, to mark this special case.

##### Collecting material

To be able to properly balance columns the multicols environment needs to collect enough material to fill the remaining part of the page. Only then does it cut the collected material into individual columns. It tries to do so by assuming that not more than the equivalent of one line of text per column vanishes into the margin due to breaking at vertical spaces. In some situations this assumption is incorrect and it becomes necessary to collect more or less material. In such a case you can adjust the default setting for the counter collectmore. Changing this counter by one means collecting material for one more (or less) \baselineskip.

There are, in fact, reasons why you may want to reduce that collection. If your document contains many footnotes and a lot of surplus material is collected, there is a higher chance that the unused part will contain footnotes, which could come out on the wrong page. The smallest sensible value for the counter is the negative number of columns used. With this value multicols will collect exactly the right amount of material to fill all columns as long as no space gets lost at a column break. However, if spaces are discarded in this set up, they will show up as empty space in the last column.

##### Tracing the algorithm

You can trace the behavior of the multicol package by loading it with one of the following options. The default, errorshow, displays only real errors. With infoshow, multicol becomes more talkative and you will get basic processing information such as

which is the calculated column width.

With balancingshow, you get additional information on the various trials made by multicols when determining the optimal column height for balancing, including the resulting badness of the columns, reasons why a trial was rejected, and so on.

Using markshow will additionally show which marks for the running header or footer are generated on each page. Instead of using the options you can (temporarily) set the counter tracingmulticols to a positive value (higher values give more tracing information).

##### Manually breaking columns

Sometimes it is necessary to overrule the column-breaking algorithm. We have already seen how the unbalance counter is used to influence the balancing phase. But on some occasions one wishes to explicitly end a column after a certain line. In standard LaTeX this can be achieved with a \pagebreak command, but this approach does not work within a multicols environment because it will end the collection phase of multicols and thus end all columns on the page. As an alternative the command \columnbreak is provided. If used within a paragraph it marks the end of the current line as the desired breakpoint. If used between paragraphs it forces the next paragraph into the next column (or page) as shown in the following example. If \flushcolumns is in force, the material in the column is vertically stretched (if possible) to fill the full column height. If this effect is not desired one can prepend a \vfill command to fill the bottom of the column with white space.

3-5-18
##### Floats and footnotes in multicol

Floats (e.g., figures and tables) are only partially supported within multicols. You can use starred forms of the float environments, thereby requesting floats that span all columns. Column floats and \marginpars, however, are not supported.

Supported printer drivers

Footnotes are typeset (full width) on the bottom of the page, and not under individual columns (a concession to the fact that varying column widths are supported on a single page).

Under certain circumstances a footnote reference and its text may fall on subsequent pages. If this is a possibility, multicols produces a warning. In that case, you should check the page in question. If the footnote reference and footnote text really are on different pages, you will have to resolve the problem locally by issuing a \pagebreak command in a strategic place. The reason for this behavior is that multicols has to look ahead to assemble material and may not be able to use all material gathered later on. The amount of looking ahead is controlled by the collectmore counter.

#### 3.5.4. changebar—Adding revision bars to documents

When a document is being developed it is sometimes necessary to (visually) indicate the changes in the text. A customary way of doing that is by adding bars in the margin, known as “changebars”. Support for this functionality is offered by the changebar package, originally developed by Michael Fine and Neil Winton, and now supported by Johannes Braams. This package works with most PostScript drivers, but in particular dvips, which is the default driver when the package is loaded. Other drivers can be selected by using the package option mechanism. Supported options are dvitoln03, dvitops, dvips, emtex, textures, and vtex.

When you add text to your document and want to signal this fact, you should surround it with the changebar environment. Doing so ensures that LaTeX will warn you when you forget to mark the end of a change. This environment can be (properly) nested within other environments. However, if your changes start within one LaTeX environment and end inside another, the environment form cannot be used as this would result in improperly nested environments. Therefore, the package also provides the commands \cbstart and \cbend. These should be used with care, because there is no check that they are properly balanced. Spaces after them might get ignored.

Changing the width

If you want to give a single bar a different width you may use the optional argument and specify the width as a normal LaTeX length.

Text that has been removed can be indicated by inserting the \cbdelete command. Again, the width of the bar can be changed.

3-5-19

When your document has reached the final stage you can remove the effect of using the changebar package by inserting the command \nochangebars in the preamble of the document.

##### Customizations

If you want to change the width of all changebars you can do so by changing the value of \changebarwidth via the command \setlength. The same can be done for the deletion bars by changing the value of \deletebarwidth.

Positioning changebars

By default, the changebars will show up in the “inner margin”, but this can be changed by using one of the following options: outerbars, innerbars, leftbars, or rightbars.

The distance between the text and the bars is controlled by \changebarsep. It can can be changed only in the preamble of the document.

Coloring changebars

The color of the changebars can be changed by the user as well. By default, the option grey is selected so the changebars are grey (grey level 65%). The drivers dvitoln03 and emtex are exceptions that will produce black changebars.

The “blackness” of the bars can be controlled with the help of the LaTeX counter changebargrey. A command like \setcounter{changebargrey}{85} changes that value. The value of the counter is a percentage, where 0 yields black bars, and 100 yields white bars.

Tracing the algorithm

The option color makes it possible to use colored changebars. It internally loads dvipsnames, so you can use a name when selecting a color.

The color to use when printing changebars is selected with the command \cbcolor, which accepts the same arguments as the \color command from the color package [57, pp.317–326].

3-5-20

You can trace the behavior of the changebar package by loading it with one of the following options. The default, traceoff, displays the normal information LaTeX always shows. The option traceon informs you about the beginning and end points of changebars being defined. The additional option tracestacks adds information about the usage of the internal stacks.

• Create BookmarkCreate Bookmark
• Create Note or TagCreate Note or Tag
• PrintPrint