# How can I customize marginal notes options to avoid exceeding page borders when switching the marginal note location?

I am using \marginnote command to insert notes to the left and right of the main text. The default location of the marginal note goes to the larger margin but can be switched with \reversemarginpar. However, when the switching happens, LaTeX, and even marginnote package, does not automatically handle the differences in left and right margins, which causes the margin notes on the smaller margin to exceed the page border.

Here are two images that illustrate what I am talking about:

Note that I took a screenshot for the whole width of the page so I did not crop the image. The question is: are there preamble macros (or new commands) that help automatically handle the differences in margins when switching margin note to the side with smaller margin? Also, the justification of the margin note text should change accordingly, that is, if the justification of the margin note text is originally left, it should be switched automatically to right when using \reversemarginpar command, and vice versa.

Here is my MWE:

\documentclass[11pt, a4paper]{book}

\usepackage{marginnote}

\begin{document}

\paragraph{Page with larger right margin}
This paper proposes an improved single-diode modeling approach for PV modules suitable for a broad range of the PV technologies available today, including modules on tandem cell structures. After establishing the model (which has an overall of seven parameters), the paper devises a methodology to estimate its parameters using Standard Test Conditions (STC) data, Nominal Operating Cell Temperature (NOCT) data, \marginnote[]{Right marginal notes work well} and temperature coefficients values as provided in most manufacturers' datasheets. Simulation results and their comparison with a previous work show a very accurate prediction of critical points in the current-voltage characteristics curve. The precise prediction happens for both STC and NOCT conditions and the error in predicting maximum power point lies within $1\%$ limit, and the error in its corresponding voltage and current is almost always within $2\%$ limit. Further, for both maximum power point\reversemarginpar \marginnote[But left marginal notes do not and may sometimes exceed page margins]{}[-1in] and open-circuit voltage, the statistical variance around manufacturer measurements due to temperature changes is demonstrated to be low for five various module technologies.

\newpage

\paragraph{Page with larger left margin}
This paper proposes an improved single-diode modeling approach for PV modules suitable for a broad range of the PV technologies available today, including modules on tandem cell structures. After establishing the model (which has an overall of seven parameters), the paper devises a methodology to estimate its parameters using Standard Test Conditions (STC) data, Nominal Operating Cell Temperature (NOCT) data, \reversemarginpar \marginnote[]{Right marginal notes do not work well and may sometimes exceed page margins} and temperature coefficients values as provided in most manufacturers' datasheets. Simulation results and their comparison with a previous work show a very accurate prediction of critical points in the current-voltage characteristics curve. The precise prediction happens for both STC and NOCT conditions and the error in predicting maximum power point lies within $1\%$ limit, and the error in its corresponding voltage and current is almost always within $2\%$ limit. Further, for both maximum power point\normalmarginpar\marginnote[But left marginal notes work well]{}[-1in] and open-circuit voltage, the statistical variance around manufacturer measurements due to temperature changes is demonstrated to be low for five various module technologies.

\end{document}

• So you want a different \marginparwidth for the left and right margins? – John Kormylo Mar 18 '18 at 2:07
• My ultimate goal is to get the margin notes to be within the border of the page even when switching from the side whose margin is large to the side whose margin is small. So if having different \marginparwidth for each side can do that then yes. – Al-Motasem Aldaoudeyeh Mar 18 '18 at 2:13
• Or you could just make \marginparwidth small enough to fit on both sides, which depends on the page layout. – John Kormylo Mar 18 '18 at 16:18

I added a \marginparsep gap at the page edge too.

\documentclass[11pt, a4paper]{book}
\dimen0=\dimexpr 1in+\oddsidemargin-2\marginparsep\relax
\ifdim\marginparwidth>\dimen0 \marginparwidth=\dimen0\fi
\dimen0=\dimexpr \paperwidth-\textwidth-1in-\oddsidemargin-2\marginparsep\relax
\ifdim\marginparwidth>\dimen0 \marparginwidth=\dimen0\fi
\dimen0=\dimexpr 1in+\evensidemargin-2\marginparsep\relax
\ifdim\marginparwidth>\dimen0 \marparginwidth=\dimen0\fi
\dimen0=\dimexpr \paperwidth-\textwidth-1in-\evensidemargin-2\marginparsep\relax
\ifdim\marginparwidth>\dimen0 \marginparwidth=\dimen0\fi

\usepackage{marginnote}

\begin{document}

\paragraph{Page with larger right margin}
This paper proposes an improved single-diode modeling approach for PV modules suitable for a broad range of the PV technologies available today, including modules on tandem cell structures. After establishing the model (which has an overall of seven parameters), the paper devises a methodology to estimate its parameters using Standard Test Conditions (STC) data, Nominal Operating Cell Temperature (NOCT) data, \marginnote[]{Right marginal notes work well} and temperature coefficients values as provided in most manufacturers' datasheets. Simulation results and their comparison with a previous work show a very accurate prediction of critical points in the current-voltage characteristics curve. The precise prediction happens for both STC and NOCT conditions and the error in predicting maximum power point lies within $1\%$ limit, and the error in its corresponding voltage and current is almost always within $2\%$ limit. Further, for both maximum power point\reversemarginpar \marginnote[But left marginal notes do not and may sometimes exceed page margins]{}[-1in] and open-circuit voltage, the statistical variance around manufacturer measurements due to temperature changes is demonstrated to be low for five various module technologies.

\newpage

\paragraph{Page with larger left margin}
This paper proposes an improved single-diode modeling approach for PV modules suitable for a broad range of the PV technologies available today, including modules on tandem cell structures. After establishing the model (which has an overall of seven parameters), the paper devises a methodology to estimate its parameters using Standard Test Conditions (STC) data, Nominal Operating Cell Temperature (NOCT) data, \reversemarginpar \marginnote[]{Right marginal notes do not work well and may sometimes exceed page margins} and temperature coefficients values as provided in most manufacturers' datasheets. Simulation results and their comparison with a previous work show a very accurate prediction of critical points in the current-voltage characteristics curve. The precise prediction happens for both STC and NOCT conditions and the error in predicting maximum power point lies within $1\%$ limit, and the error in its corresponding voltage and current is almost always within $2\%$ limit. Further, for both maximum power point\normalmarginpar\marginnote[But left marginal notes work well]{}[-1in] and open-circuit voltage, the statistical variance around manufacturer measurements due to temperature changes is demonstrated to be low for five various module technologies.

\end{document}

• These macros work well, despite the fact that I don't know what they mean. However, they are not compatible with geometry package. So far, I have never seen a package specifically for customizing margin notes. In particular, the package needs to provide an option to select margin with respect to main text and with respect to page borders. – Al-Motasem Aldaoudeyeh Mar 18 '18 at 16:46