7

I'm currently using the book class and I'm using marginpar to cite sources. I would like the marginpar on right pages to be aligned left and the marginpar on left pages to be aligned right. Is there any way to do this?

Note I cannot use marginnote due to a package incompatibility.

enter image description here

\documentclass[twoside]{book}
\usepackage[paperheight=5in,paperwidth=6in,top=.7in,bottom=.7in, inner=1in, outer=2in, marginparsep=.1in, headsep=16pt]{geometry}

\usepackage{lipsum}

\begin{document}

\marginpar{This is the outside margin. I would like it to be aligned left (as it is).}\lipsum[1] 

\clearpage

\marginpar{This is also the outside margin. I would like it to be aligned right}\lipsum[2] 

\end{document}

4 Answers 4

6

This automates Ben Stern's answer using the KOMA Script test \ifthispageodd (through scrextend given the use of the book class) and defining an \alignedmarginpar which aligns right on left pages.

\documentclass[twoside]{book}
\usepackage[paperheight=5in,paperwidth=6in,top=.7in,bottom=.7in, inner=1in,
 outer=2in, marginparsep=.1in, headsep=16pt]{geometry}

\usepackage{scrextend}
\usepackage{lipsum}

\newcommand{\alignedmarginpar}[1]{%
    \ifthispageodd{%
        \marginpar{\raggedright\small #1}}{%
        \marginpar{\raggedleft\small #1}}%
    }

\begin{document}

\alignedmarginpar{This is the outside margin. I would like it to be aligned left (as it is).}\lipsum[1]

\clearpage

\alignedmarginpar{This is also the outside margin. I would like it to be aligned right.}\lipsum[2]

\end{document}

enter image description here enter image description here

9
  • It should be ragged on both sides, isn't?
    – Sigur
    Commented Feb 26, 2018 at 21:38
  • @Sigur, that's not what I understood from the OP. The text of the two marginpars suggests the alternation of the alignment is the desired result. If your reading is correct, the last comment I left for BenStern was undeserved.
    – gusbrs
    Commented Feb 26, 2018 at 21:43
  • Ragged, but to different directions. OP wrote would like the marginpar on right pages to be aligned left and the marginpar on left pages to be aligned right. So, the result should be one opposite to the other. So use \raggedright.
    – Sigur
    Commented Feb 26, 2018 at 21:45
  • @Sigur Aligned doesn't mean ragged necessarily. The OP states on the even page note "I would like it to be aligned left (as it is)". Indeed there is no way to justify the text and align it right. I'm inclined to agree with you that \raggedright on the other side would be more elegant. Given these considerations, do you think this is the proper reading of the OP? (if so, I'll gladly edit).
    – gusbrs
    Commented Feb 26, 2018 at 21:52
  • 1
    I'd use a small font, since the width is not too big, just to see how pretty it is: \raggedright\small #1. Also, it is more visible the ragged in this case.
    – Sigur
    Commented Feb 26, 2018 at 22:14
6

There is an optional argument to \marginpar that allows to specify the left-side text. This allows to create the \alignedmarginpar solution from gusbrs' answer without the need for additional packages:

\newcommand{\alignedmarginpar}[1]{%
    \marginpar[\raggedleft #1]{\raggedright #1}%
}
3
  • This is brilliant, and should be the accepted answer. (I tried it, it works!)
    – Nat Kuhn
    Commented May 8, 2021 at 20:37
  • What if sidenotes uses the \marginpar command? How can I tell it to use the new command?
    – Atcold
    Commented Feb 21, 2022 at 17:12
  • 1
    @Atcold If you want to overwrite the \marginpar command, you could use \renewcommand instead (see e.g. this question). Commented Feb 28, 2022 at 8:12
2

Put a \raggedleft right before your \marginpar text.

\documentclass[twoside]{book}
\usepackage[paperheight=5in,paperwidth=6in,top=.7in,bottom=.7in, inner=1in,
  outer=2in, marginparsep=.1in, headsep=16pt]{geometry}

\usepackage{lipsum}

\begin{document}

\marginpar{This is the outside margin. I would like it to be aligned left (as it
  is).}\lipsum[1]

\clearpage

\marginpar{\raggedleft This is also the outside margin. I would like it to be
  aligned right}\lipsum[2]

\end{document}

demo of latex output

4
  • 1
    Welcome to TeX.SX! This does indeed achieve the result for this particular marginpar, but it requires that you know which side the marginpar will be beforehand and is all too manual in this respect. And somewhat error prone. Still, a welcome +1.
    – gusbrs
    Commented Feb 25, 2018 at 21:57
  • Revising, those are fair criticisms.
    – Ben Stern
    Commented Feb 26, 2018 at 21:32
  • 1
    I appreciate your well intended revision. But now, you have all marginpars raggedleft. On both even and odd pages, which explicitly is not what the original post asked for. Your original answer was partial, but went in the intended direction. The revision now leaves your answer as not solving the problem asked for. Have you taken a look at the answer I left automating your initial proposal?
    – gusbrs
    Commented Feb 26, 2018 at 21:39
  • 1
    My mistake, I somehow confused myself. Reverting. Thank you. I was trying to figure out a solution that didn't need another package, but I think you're right that this needs more automation.
    – Ben Stern
    Commented Feb 26, 2018 at 22:33
1

To complement @gusbrs and @egreg's excellent answers, should you want to replicate the behavior of \marginpar[inner margin text]{outer margin text} which allows to typeset a different text depending on the margin in use (for twosided documents), you could also go with the following definition using the xparse package,

\documentclass[twoside]{book}
\usepackage[paperheight=5in,paperwidth=6in,top=.7in,bottom=.7in, inner=1in, outer=2in, marginparsep=.1in, headsep=16pt]{geometry}
\usepackage{lipsum}
\usepackage{xparse}

\NewDocumentCommand{\raggedmarginpar}{ O{#2} m }{%
    \marginpar[\raggedleft#1]{\raggedright#2}%
}

\begin{document}

\raggedmarginpar{This is the outside margin. I would like it to be aligned left (as it is).}\lipsum[1]

\clearpage

\raggedmarginpar{This is also the outside margin. I would like it to be aligned right}\lipsum[2]

\clearpage

\raggedmarginpar[Points towards main text.~$\Rightarrow$]{$\Leftarrow$~Points towards main text.}\lipsum[3]

\clearpage

\raggedmarginpar[Points towards main text.~$\Rightarrow$]{$\Leftarrow$~Points towards main text.}\lipsum[4]

\end{document}

Third page Fourth page

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .