[Tex/LaTex] Making marginnote to appear either side of twocolumn, twoside document

double-sidedmarginnotemarginpartwo-column

I have the following MWE in which the \marginnote need to appear at correct margin. For example the margin notes one, two, three and four need to appear on left margin of first page. In the second page the margin notes thirteen and fourteen should appear on right side.

The command \reversemarginpar corrects this, but I need to reset on next column. I am not able to use the twocolumn document class option as I am using \twocolumn[] to set the title part of the document.

I will not be able to use other class files. Is there an easy way to put the margin notes to the respective margins.


MWE

\documentclass[twoside]{article}

\RequirePackage{lipsum}
\RequirePackage[margin=2cm]{geometry}
\RequirePackage{marginnote}

\begin{document}
\sloppy

\twocolumn[{%
\hrulefill\medskip\par
\lipsum[1]
\hrulefill
\vspace*{2pc}
}]


\section{Two}\label{s2}
\marginnote{One}\lipsum[1]
\marginnote{Two}\lipsum[2] 
\marginnote{Three}\lipsum[3]
\marginnote{Four}\lipsum[4]
\marginnote{Five}\lipsum[5]
\marginnote{Six}\lipsum[6]
\marginnote{Seven}\lipsum[7]

\section{Three}\label{s3}
\marginnote{Eight}\lipsum[1]
\marginnote{Nine}\lipsum[2] 
\marginnote{Ten}\lipsum[3]
\marginnote{Eleven}\lipsum[4]
\marginnote{Twelve}\lipsum[5]
\marginnote{Thirteen}\lipsum[6]
\marginnote{Fourteen}\lipsum[7]

\end{document}

There are certain post available here with \marginnote which is not exactly what I am looking for.

Best Answer

Add the following lines in your preamble:

\makeatletter
\let\oldmarginnote\marginnote
\renewcommand*{\marginnote}[1]{%
   \begingroup%
   \ifodd\value{page}
     \if@firstcolumn\reversemarginpar\fi
   \else
     \if@firstcolumn\else\reversemarginpar\fi
   \fi
   \oldmarginnote{#1}%
   \endgroup%
}
\makeatother

MWE:

\documentclass[twoside]{article}

\usepackage{lipsum}
\usepackage[margin=2cm]{geometry}
\usepackage{marginnote}

\makeatletter
\let\oldmarginnote\marginnote
\renewcommand*{\marginnote}[1]{%
   \begingroup%
   \ifodd\value{page}
     \if@firstcolumn\reversemarginpar\fi
   \else
     \if@firstcolumn\else\reversemarginpar\fi
   \fi
   \oldmarginnote{#1}%
   \endgroup%
}
\makeatother


\begin{document}

\sloppy

\twocolumn[{%
\hrulefill\medskip\par
\lipsum[1]
\hrulefill
\vspace*{2pc}
}]


\section{Two}\label{s2}
\marginnote{One}\lipsum[1]
\marginnote{Two}\lipsum[2]
\marginnote{Three}\lipsum[3]
\marginnote{Four}\lipsum[4]
\marginnote{Five}\lipsum[5]
\marginnote{Six}\lipsum[6]
\marginnote{Seven}\lipsum[7]


\section{Three}\label{s3}
\marginnote{Eight}\lipsum[1]
\marginnote{Nine}\lipsum[2]
\marginnote{Ten}\lipsum[3]
\marginnote{Eleven}\lipsum[4]
\marginnote{Twelve}\lipsum[5]
\marginnote{Thirteen}\lipsum[6]
\marginnote{Fourteen}\lipsum[7]

\end{document} 

Output:

enter image description here


To avoid having paragraphs at the beginning of a page with an orphan margin note in the previous page, you can load the needspace package and add \needspace{\baselineskip} to the re-definition of \marginnote

\makeatletter
\let\oldmarginnote\marginnote
\renewcommand*{\marginnote}[1]{%
   \begingroup%
   \ifodd\value{page}
     \if@firstcolumn\reversemarginpar\fi
   \else
     \if@firstcolumn\else\reversemarginpar\fi
   \fi
   \needspace{\baselineskip}\oldmarginnote{#1}%
   \endgroup%
}
\makeatother

MWE (try removing \needspace{\baselineskip} to see the difference)

\documentclass[twoside]{article}

\usepackage{lipsum}
\usepackage[margin=2cm]{geometry}
\usepackage{marginnote}
\usepackage{needspace}

\makeatletter
\let\oldmarginnote\marginnote
\renewcommand*{\marginnote}[1]{%
   \begingroup%
   \ifodd\value{page}
     \if@firstcolumn\reversemarginpar\fi
   \else
     \if@firstcolumn\else\reversemarginpar\fi
   \fi
   \needspace{\baselineskip}\oldmarginnote{#1}%
   \endgroup%
}
\makeatother


\begin{document}

\sloppy

\twocolumn[{%
\hrulefill\medskip\par
\lipsum[1]
\hrulefill
\vspace*{2pc}
}]


\section{Two}\label{s2}
\marginnote{One}\lipsum[1]
\marginnote{Two}\lipsum[1]
\marginnote{Three}\lipsum[1]
\marginnote{Four}\lipsum[1]
\marginnote{Five}\lipsum[2]
\marginnote{Six}\lipsum[2]
Some text\par
Some text\par
Some text\par
\marginnote{Seven}\lipsum[1]
\end{document} 

Output

enter image description here

Related Question