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}
wolf_math
  • 357

4 Answers4

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

gusbrs
  • 13,740
  • It should be ragged on both sides, isn't? – Sigur Feb 26 '18 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 Feb 26 '18 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 Feb 26 '18 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 Feb 26 '18 at 21:52
  • Well, let's wait the OP comments. But in my opinion makes more sense to have symmetric outputs, so ragged right/left. – Sigur Feb 26 '18 at 21:57
  • @Sigur, I did agree with you it would be more elegant! :) (I'll edit, you are right it is a "bonus" even if not asked for). – gusbrs Feb 26 '18 at 21:59
  • @Sigur, our symmetry yearnings are satisfied now. – gusbrs Feb 26 '18 at 22:10
  • 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 Feb 26 '18 at 22:14
  • This is great, but @Mr Tsjolder has provided a much simpler solution below. – Nat Kuhn May 08 '21 at 20:38
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}%
}
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

Ben Stern
  • 121
  • 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 Feb 25 '18 at 21:57
  • Revising, those are fair criticisms. – Ben Stern Feb 26 '18 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 Feb 26 '18 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 Feb 26 '18 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

Wazabi
  • 11