#StackBounty: #marginpar #scrbook How to assign tagged margin notes to opposite margins of a scrbook?

Bounty: 50

Consider the following piece of LaTeX code, saved in a file named test.tex. This code was copied almost verbatim from campa’s answer to a previous post of mine in which I requested to add optional tags to margin notes in order to be able to selectively hide some of the notes, but not all of them, by specifying a tag. The difference is that I used the scrbook document class, and added a title.

documentclass{scrbook}

title{A book}

makeatletter

newcommand*{newmarginstuff}[1]{%
   expandafter@ifdefinablecsname @my@marg@#1endcsname
      {expandafterletcsname @my@marg@#1endcsname=iftrue}%fi
}

newcommand*{includemarginstuff}[1]{%
   @bsphackexpandafterletcsname @my@marg@#1endcsname=iftrue@esphack
} %fi

newcommand*{excludemarginstuff}[1]{%
   @bsphackexpandafterletcsname @my@marg@#1endcsname=iffalse@esphack
} %fi

newcommand*{marginstuff}[2][]{%
   ifrelaxdetokenize{#1}relax
      expandafter@firstoftwo
   else
      expandafter@secondoftwo
   fi
   {marginpar{#2}}%
   {csname @my@marg@#1endcsnamemarginpar{#2}fi}%
}

makeatother

newmarginstuff{foo}
newmarginstuff{baz}

begin{document}
maketitle

parskip2ex

Text text text text text text text
marginstuff[foo]{1st foo}
text text text text text text text text text
text text text text text text text text text

Text text text text text text text
marginstuff{generic}
text text text text text text text text text
text text text text text text text text text

excludemarginstuff{foo}
Text text text text text text text
marginstuff[foo]{2nd foo}
text text text text text text text text text
text text text text text text text text text

Text text text text text text text
marginstuff[baz]{1st baz}
text text text text text text text text text
text text text text text text text text text

end{document}

The resulting pdf is (I use LuaLaTeX):

Tagged marginpars inside a scrbook

I’d like the margin notes carrying the tag baz to appear on the opposite margin to those margin notes carrying the tag foo. How can this be accomplished?

The tags of the margin notes determine which margin the note will occupy

Note that in the scrbook document class, the margin notes carrying the tag foo appear automatically on alternating sides of consecutive pages, and I don’t want to change this behavior.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.