# ifthispageodd with scrlayer-notecolumn crashes LaTeX: Any workarounds for creating margin notes?

I'm currently working on a book project in LaTeX and LyX and found a weird bug that seems to crash LaTeX. I have no idea how to work around that crash, so I thought I'd ask for help here.

In a nutshell: I'm using the KOMA-script class scrbook to create a document of size A4 and would like to set up margin notes alongside the main text. I read a lot about different procedures for that and decided to try the scrlayer-notecolumn package to avoid the disadvantages of the other methods.

For greater clarity, I would like to be able to indent all regular paragraphs, but leave chapter and section headings unindented. I already managed to set that up (but I'm open for suggestions, in case this should be done differently). Here's a MWE for the class file:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{MyBook}[2016/10/01 My Book Class]

% General setup
\RequirePackage{blindtext}
\RequirePackage{calc}
\RequirePackage{color}
\definecolor{orange}{rgb}{1.0, 0.5, 0.0}
\newcommand\pagedividers{14}

% Page Layout
a4paper,
twoside,
titlepage,
fontsize = 9pt,
parskip = full,
index = totoc,
listof = totoc,
bibliography = totoc,
numbers = noenddot,
appendixprefix = true,
captions = nooneline
]{scrbook}

\RequirePackage{geometry}
\geometry{
reversemarginpar,
inner  = 21cm /  \pagedividers,
outer  = 21cm / \pagedividers * 2,
top    = 29.7cm / \pagedividers,
bottom = 29.7cm / \pagedividers,
heightrounded
}

% Use scrlayer-scrpage, so that package notecolumn works
\RequirePackage[
plainfootsepline,
]{scrlayer-scrpage}

% Set up the note column
\RequirePackage{scrlayer-notecolumn}
\RequirePackage{scrextend}
\newlength{\completemargin}
\setlength{\completemargin}{4.125cm}
\newlength{\notescolwidth}
\setlength{\notescolwidth}{\completemargin - \marginparsep}

\DeclareNewNoteColumn[
marginpar,
position = \ifthispageodd{\oddsidemargin + 1in}{\evensidemargin + 1in},
width = \notescolwidth,
font = \raggedright\footnotesize\color{orange}
]{notes}

% Indent the paragraphs, lists, equations and subsections
\RequirePackage{enumitem}
\setlength{\leftskip}{\completemargin}
\setlist[itemize]{leftmargin = \completemargin}
\setlist[enumerate]{leftmargin = \completemargin}
\setlist[description]{leftmargin = \completemargin + 1cm, labelindent = \completemargin}
\RequirePackage[fleqn]{amsmath}
\setlength{\mathindent}{\completemargin + 1cm}


And here's a sample file for testing the layout:

\documentclass[english]{MyBook}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}
\usepackage{babel}
\begin{document}

\Blindtext[2]

\makenote[notes]{Here's a note for the new note column. Just for testing purposes.}\Blindtext[4]

\makenote[notes]{Here's a note for the new note column. Just for testing purposes.}\Blindtext[3]
\end{document}


This, of course, give me an asymmetrical layout. And here's where my problem starts:

The note column (notes in the above example) needs to be to the left of the main text, both on the odd and on the even pages. As the outer and inner margins are different, I need a way to check, if I'm on an odd or even page, when setting the position of the note column. I do this - as suggested - by using \ifthispageodd from package scrextend.

This, however, crashes LaTeX (or pdflatex, when called from LyX).

I'm really not sure how to work around that. If I remove \ifthispageodd and use, e.g., \oddsidemargin instead, the pages look like that:

So, is there any other way for checking odd or even pages? Or is there a command that I can use for reading out the actual left margin of the current page (instead of \oddsidemargin in the above MWE). Or should I use a different procedure for setting up the note column? Any help would be much appreciated.

Oh, by the way: If anybody has an idea how to align the baseline of the notes in the note column with the main text, I'd be very happy, as well.

\ifthispageodd uses a mechanism that is very similar to setting and referecing a label. This could be the reason why it crashes. But when a layer page style (which is used for the note column) is intiallized, the correct page number is known. So you can use

  position = \ifodd\value{page}
\dimexpr 1in+\oddsidemargin \relax
\else
\dimexpr 1in+\evensidemargin \relax
\fi


Note that \dimexpr and \relax are not needed when package calc was loaded.

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{MyBook}[2016/10/01 My Book Class]

% General setup
\RequirePackage{blindtext}% only for dummy text
\RequirePackage{calc}
\RequirePackage{color}
\definecolor{orange}{rgb}{1.0, 0.5, 0.0}
\newcommand\pagedividers{14}

% Page Layout
a4paper,
twoside,
titlepage,
fontsize = 9pt,
parskip = full,
index = totoc,
listof = totoc,
bibliography = totoc,
numbers = noenddot,
appendixprefix = true,
captions = nooneline
]{scrbook}

\RequirePackage{geometry}
\geometry{
reversemarginpar,
inner  = \paperwidth  / \pagedividers,
outer  = \paperwidth  / \pagedividers * 2,
top    = \paperheight / \pagedividers,
bottom = \paperheight / \pagedividers,
heightrounded
}

% Use scrlayer-scrpage, so that package notecolumn works
\RequirePackage[
plainfootsepline,
]{scrlayer-scrpage}

% Set up the note column
\RequirePackage{scrlayer-notecolumn}
\newlength{\completemargin}
\setlength{\completemargin}{4.125cm}
\newlength{\notescolwidth}
\setlength{\notescolwidth}{\completemargin - \marginparsep}

\DeclareNewNoteColumn[
marginpar,
position = \ifodd \value{page} 1in+\oddsidemargin \else 1in+\evensidemargin \fi,
width = \notescolwidth,
font = \raggedright\footnotesize\color{orange}
]{notes}

% Indent the paragraphs, lists, equations and subsections
\RequirePackage{enumitem}
\setlength{\leftskip}{\completemargin}
\setlist[itemize]{leftmargin = \completemargin}
\setlist[enumerate]{leftmargin = \completemargin}
\setlist[description]{leftmargin = \completemargin + 1cm, labelindent = \completemargin}
\RedeclareSectionCommands[
indent=\completemargin
]{subsubsection,paragraph,subparagraph}
\RequirePackage[fleqn]{amsmath}
\setlength{\mathindent}{\completemargin + 1cm}


Note that I have used \RedeclareSectionCommands to change the indent of subsubsection etc.

\documentclass[english]{MyBook}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}
\usepackage{babel}

\begin{document}

• Oh, thanks, that's a quite an elaborate idea. I kinda expected the problem to have a simpler solution and wouldn't have thought that this would necessitate setting up layers and two different note columns. Why isn't the same procedure necessary when positioning the note using, e.g., \marginpar (which renders the note on different positions depending on whether it's a right or a left page)? By the way: Does \ifthispageodd also crash your LaTeX (i.e. it never stops running, making it necessary to force-kill the process) or is that a bug in my installation? – Marcus C. Oct 11 '16 at 16:00
• You are absolutly right: there is no need to define an additional layer. You can simple use \ifodd \value{page} ... \else ...\fi. So I have changed my answer. – esdd Oct 11 '16 at 16:43