跳到内容

Introduction

This article explains how to create margin notes, a popular alternative to footnotes as a device for adding supplementary information to support the main text of a document. We also have a help page on the todonotes package which provides alternative solutions to those presented in this article.

The \marginpar command

Without loading any additional packages you can use the built-in LaTeX \marginpar command to add margin notes to your document. The general form of the \marginpar command is

\marginpar[left text]{right text}

where left text will be used if the margin note appears on the left of a document page and right text if appears on the right. To be more specific, marginal notes are normally put on the “outside” of the page—although the meaning of “outside” depends on the document class you are using and whether you have specified the twoside package option:

  • book class (\documentclass{book}) documents are two-sided by default (design)
  • report class (\documentclass{report}) and article class (\documentclass{article}) documents are, by default, single-sided. To make them two-sided you have to specify the twoside option:
    • \documentclass[twoside]{report}, or
    • \documentclass[twoside]{article}

For single-sided documents, excluding right-to-left languages, such as Arabic, the “outside” is always the right-hand edge of the text and for two-sided documents it is the left-hand edge of the text for left-hand pages and the right-hand edge of the text for right-hand pages.

Reversing placement of margin notes

The command \reversemarginpar reverses the side on which margin notes are typeset; its effect can be undone by the command \normalmarginpar. As stated in the LaTeX source code documentation “These commands have no effect for two-column output.”

Commands affecting margin note typesetting

Typesetting of \marginpar notes makes use of several dimension commands which store values that can be changed, if required:

  • \marginparwidth: determines the width of margin notes and thus the length of lines typeset in the margin note.
  • \marginparsep: sets the gap (distance) between margin notes and the text of your document.
  • \marginparpush: defines the minimum separation (vertical) distance between \marginpar notes.

Changing the typesetting of margin notes: ragged right and ragged left

By default, the text of a margin note is typeset is a box which produces typeset lines of width \marginparwidth. This can result in unsightly gaps between typeset words as the TeX engine tries to find the “best” line breaks. You can use the commands \raggedright and \raggedleft to alter the typeset style of the text.

Example

Here is an example which uses several \marginpar commands to demonstrate some of the features listed above. The example also loads the geometry package to create a small page size and the hyperref package which provides the \url command used to credit the source of the text (in the footnote).

\documentclass[twoside]{article} % Note: uses twoside option
\usepackage[a4paper, marginparwidth=75pt, total={10cm, 10cm}]{geometry} % To create a small page
\usepackage{hyperref} % To use the \url command (in the footnote)
\usepackage{marginnote}
\begin{document}
\section{Lorem Ipsum}
\footnote{Source text: Wikipedia (\url{https://en.wikipedia.org/wiki/Lorem_ipsum})}But I must explain to you how all this mistaken idea of reprobating pleasure and extolling pain arose. To do so, I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. \marginpar[Note 1: text for left-hand side text]{Note 1: text for right-hand side of pages, it is set justified.} No one rejects, dislikes or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but occasionally circumstances occur in which toil and pain can procure him some great pleasure.  \marginpar[Note 2: text for left-hand side text]{\raggedright Note 2: text for right-hand side of pages, it is not justified, but uses \texttt{\string\raggedright}.} To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure? [33] On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammeled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. \marginpar[\raggedleft Note 3: text for left-hand side of pages, it is not justified, but uses \texttt{\string\raggedleft}]{Note 3: text for left-hand side of pages}But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted.  The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains.
\end{document}

 Open this \marginpar example in Overleaf

This example produces the following (two pages) of output:

Page 1:

Example of the \marginpar command

Page 2:

Example of the \marginpar command

The mparhack package: fixing the wrong margin

Internally, LaTeX processes margin notes as a type of “floating” component of your document—similar to the way LaTeX handles floating figures and tables. Due to being "floats", margin notes produced by \marginpar can occasionally appear in the wrong margin. The mparhack package was written to fix this problem so it may be worth using it if you encounter that.

An alternative to \marginpar: the marginnote package

The marginnote package offers a versatile alternative to the \marginpar command. marginnote implements margin notes using a non-float mechanism, which is different to how \marginpar works: marginnote resolves some problems but, as its documentation notes, it can introduce others.

Example

The following example demonstrates the \marginnote command provided by the marginnote package:

\documentclass{article}
\usepackage[a5paper, total={3in, 6in}]{geometry} % to create a small page
\usepackage{hyperref} % To use the \url command (in the footnote)
\usepackage{marginnote}
\begin{document}
\section{Lorem Ipsum}
\footnote{Source text: Wikipedia (\url{https://en.wikipedia.org/wiki/Lorem_ipsum})}But I must explain to you how all this mistaken idea of reprobating pleasure and extolling pain arose. To do so, I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. \marginnote{This is a margin note shifted 2cm, \textit{down} the page, relative to the line in which it is typeset.}[2cm] No one rejects, dislikes or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but occasionally circumstances occur in which toil and pain can procure him some great pleasure. \reversemarginpar\marginnote{This is another margin note but shifted 2cm \textit{up} the page, relative to the line in which it is typeset. It is also in the left-hand margin.}[-2cm] To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure? [33] On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammeled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains.
\end{document}

 Open this example in Overleaf

This example produces the following output:

Using the marginnote package

The above example also loads the geometry package to create a small page size and the hyperref package which provides the \url command used to credit the source of the text (in the footnote). After importing the marginnote package by writing \usepackage{marginnote} you can use the \marginnote command as shown by the two instances in the example text:

  • \marginnote{This is a margin note shifted 2cm, \textit{down} the page, relative to the line in which it is typeset.}[2cm]
  • \marginnote{This is another margin note but shifted 2cm \textit{up} the page, relative to the line in which it is typeset. It is also in the left-hand margin.}[-2cm]

In these examples we use a second parameter inside brackets [ ] which determines vertical alignment relative to the line where the command is used. The first \margingnote command uses [2cm] to shift the margin note 2cm down the page, the second one uses [-2cm] to shift the margin note 2cm up the page.

The second \marginnote command operates after a \reversemarginpar was used, causing it to be placed in the left-hand margin of this single-sided document:

\reversemarginpar\marginnote{This is another margin note but shifted 2cm \textit{up} the page, relative to the line in which it is typeset. It is also in the left-hand margin.}[-2cm]

Further reading

For more information see

Overleaf guides

LaTeX Basics

Mathematics

Figures and tables

References and Citations

Languages

Document structure

Formatting

Fonts

Presentations

Commands

Field specific

Class files

Advanced TeX/LaTeX