The package titlesec
provides \titleformat
command with possible even/odd specification.
The package has many options and a full answer cannot be provided without your example of the desired output ... For the usage see the titlesec
documentation.
So just a short example (put into your preamble):
\usepackage{titlesec}
\titleformat{name=\section,page=even}{\filleft\Large\scshape}{\thesection}{.5em}{}
\titleformat{name=\section,page=odd}{\filright\Large\scshape}{\thesection}{.5em}{}
Notice that this requires two pases to stabilize.
EDIT: I've updated the solution below to use the wrapfig environment, which simplified at least some parts of the code. This solved all the spacing issues I had before. I also changed the definition of the command to only require that it is put before each paragraph, and added functionality for putting long notes on new lines. The only issues remaining is that the solution still does not work well with page breaks, and also that it is quite cumbersome to but the same command before each paragraph, even if it is less cumbersome than the syntax of my previous solution.
I've now made a new solution (with the help of David Carlise) which I think is a bit closer to what you intended than my last attempt. It still has some problems though:
- Some unwanted spaces sometimes appear between paragraphs
- Notes are never put on separate lines
- No sidebreaks are allowed inside paragraphs
The result now look as in the image below:
![example result](https://i.stack.imgur.com/zbXNC.png)
And the source code.. :)
\documentclass[10pt]{article}
% Packages
\usepackage[newcommands]{ragged2e}
\usepackage{newfile}
\usepackage{color}
\usepackage{xparse}
\usepackage{graphics}
\usepackage[dvipsnames]{xcolor}
\usepackage{hyperref}
\usepackage{wrapfig}
\usepackage{ifthen}
% Settings
\colorlet{Pbardefault}{yellow!70} % Default color for the colored bar left to the notes
\colorlet{Plightdefault}{yellow!10}% Default color for background of the note
\def\Pmin{0.5} % ~minimal amount main text
\def\Pmax{0.95} % ~maximal amount main text / \Psf
\def\Psf{1.14} %
\newcounter{localsidenotecounter}
\newlength{\currentparskip}
\setlength{\currentparskip}{\parskip}
% The formatting of the note
\newdimen\Pparsep
\Pparsep=0.2em
\newdimen\Pcolorwidth
\Pcolorwidth=2mm
% The paragraph command
\makeatletter
\def\sidenotepi#1\par%
{
%
\immediate\openout\tempfile=sidenotes.tex %
%
\twopara%
{
#1 \immediate\closeout\tempfile
}
{\input{sidenotes.tex}}
}
\makeatother
% A command for finding the optimal text widths
\makeatletter
\long\def\twopara#1#2{%
\newdimen\columnsep
\columnsep=\dimexpr \Pparsep + \Pcolorwidth
\def\@elt##1{\global\value{##1}\the\value{##1}\relax}%
\edef\TY@ckpt{\cl@@ckpt}%
\@tempdima\dimexpr\textwidth-\columnsep\relax
{\hbadness\@M\raggedright\hsize.5\@tempdima \@tempdima\hsize
\setbox\z@\vbox{{#1\endgraf}}%
\setbox\tw@\vbox{{#2\endgraf}}%
\Gscale@div\tmp{\ht\z@}{\dimexpr\ht\z@+\ht\tw@\relax}%
\global\let\xtmp\tmp}%
\dimen@ \xtmp\@tempdima
\ifdim\dimen@<\Pmin\@tempdima \dimen@\Pmin\@tempdima\fi
\ifdim\dimen@>\Pmax\@tempdima \dimen@\Pmax\@tempdima\fi
\dimen@ii\dimexpr\@tempdima-\dimen@\relax
\TY@ckpt
\ifdim\dimen@ii<20pt
#1
\else
\ifdim \Psf\dimen@ii>\dimexpr\textwidth-\Pmin\textwidth
#1
\let\Pnotebox\Pnotebox@
\dimen@ii = \dimexpr \textwidth-4mm
\setlength{\intextsep}{2pt}%
\begin{figure}[h]
#2
\end{figure}
\else
\let\Pnotebox\Pnotebox@
\dimen@ii\dimexpr\Psf\dimen@ii
\setlength{\intextsep}{0pt}%
\begin{wrapfigure}{o}[-4mm]{\dimen@ii}
#2
\end{wrapfigure}
#1
\fi
\fi
\let\Pnotebox\Pnotebox@@
}
\makeatother
% The unformatted note text
\makeatletter
\long\def\Pnotebox@@#1#2#3{ %barcolor, backcolor, text
#3}
\makeatother
\makeatletter
\long\def\Pnotebox#1#2#3{ %barcolor, backcolor, text
#3}
\makeatother
% The formatting of the notes
\makeatletter
\newdimen\Pnoteheight
\long\def\Pnotebox@#1#2#3{ %barcolor, backcolor, text
\settoheight{\Pnoteheight}{\parbox{\dimen@ii}{#3}}%
\begin{minipage}{\dimexpr\dimen@ii+\Pcolorwidth}
\textcolor{#1}{\rule[\dimexpr-\Pnoteheight+1mm]{\Pcolorwidth}{\dimexpr 2\Pnoteheight}}%
\colorbox{#2}{\parbox{\dimen@ii}{#3}}
\end{minipage}%
}
\makeatother
% Commands for making the sidenote
\newwrite\tempfile
\makeatletter
\DeclareDocumentCommand \sidenote {m O{Pbardefault} O{Plightdefault}} {%
\stepcounter{localsidenotecounter}%
\hyperlink{sidenote\arabic{localsidenotecounter}}{${}^\textrm{\footnotesize\arabic{localsidenotecounter}}$}%
\immediate\write\tempfile%
{%
\noexpand\Pnotebox{#2}{#3}{%
\noexpand\footnotesize \noexpand{\noexpand\hypertarget{sidenote\arabic{localsidenotecounter}}{${}^{\arabic{localsidenotecounter}}$} \unexpanded{#1 }}%
}}}
\makeatother
\begin{document}
\sidenotepi
But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and 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. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue\sidenote{Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure} 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 because occasionally circumstances occur in which toil and pain can procure him some great pleasure
\sidenotepi
But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and 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. 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 because occasionally circumstances occur in which toil and pain can procure him some great pleasure.
\sidenotepi
But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and 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. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue\sidenote{Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure} 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 because occasionally circumstances occur in which toil and pain can procure him some great pleasure.\sidenote{Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure.}[blue][blue!5]
\end{document}
Best Answer
I assume (but you don;t say) that you are using
\marginnote
from themarginnote
package. That like a standard\marginpar
does not pick up the current alignment. The following shows the note is justified as normal whether it is used in a justified paragraph, or a flushleft or centred one.sectsty
is doing somethig odd, can't be bothered to debug that so just re-assert the normalisation of font and alignment that marginpar/marginnote do anyway but that got broken somewhere. This is ragged left on one side and raggedright in the other, as is normal for marginnote.