summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/marginnote/marginnote.dtx
blob: 42328574023e56eac51f26287279c4f8fb209dea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
% \CheckSum{757}
% \iffalse meta-comment
%
% marginnote.dtx
%
%% Copyright (c) Markus Kohm  2005-2010
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%% http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2003/12/01 or later.
%% 
%% This work has the LPPL maintenance status "maintained".
%%
%% The author of the initial version of these work was 
%% Markus Kohm <komascript@gmx.info>
%% 
%% This work consists of the files marginnote.dtx, marginnote.ins,
%% and REAMDE.
%%
%% Important Recommendation:
%%   The LPPL requires that distributions of the Work contain all the files of
%%   the Work (see ``Important Recommendations'' at lppl.txt).
%%
%
%<*driver>
\ProvidesFile{marginnote.dtx}[%
%</driver>
%<package>\ProvidesPackage{marginnote}[%
%<*driver|package>
  2010/01/05 v1.1f non floating margin notes for LaTeX]
%</driver|package>
%<*driver>
\documentclass{ltxdoc}
\usepackage{graphicx}% needed for \reflectbox
\providecommand*{\XeTeX}{%
  X\kern-.1em\lower.5ex\hbox{\reflectbox{E}}\kern-.15em\TeX}
\begin{document}
\RecordChanges
\DocInput{marginnote.dtx}
\end{document}
%</driver>
%\fi
%
% \GetFileInfo{marginnote.dtx}
%
% \title{Non-Floating Margin Notes with \textsf{marginnote}
%   Package\thanks{This file has version number \fileversion, last revised
%     \filedate.}}
% \author{Markus Kohm\thanks{Email: \texttt{komascript@gmx.info}}}
% \date{\filedate}
% \maketitle
%
% \begin{abstract}
%   In \LaTeX{} the command \cs{marginpar}\oarg{left}\marg{right} might be
%   used to create a note in the margin. But there is a problem with this
%   command: it creates a special kind of float. For this it cannot be used
%   e.g. at floats or footnotes. Package \textsl{marginnote} supports another
%   command \cs{marginnote} to create notes in the margin. This does not use a
%   kind of float and for this does not have the disadvantage of
%   \cs{marginpar}. But there might be other problems \dots
% \end{abstract}
%
% \tableofcontents
%
% \changes{v1.0b}{2006/14/03}{spelling fixes}
%
% \section{How to Use \textsf{marginnote} Package}
%
% First of all you have to load. You may use:
% \begin{verbatim}
% \usepackage{marginnote}
% \end{verbatim}\vskip-\baselineskip
% to do so. You may also use one of the following options for a global change
% of the behaviour of \textsf{marginnote}:
% \begin{description}
% \item[\texttt{fulladjust}] adjusts the margin note at the height and depth of
% the current line. Note, that this may sometimes result in extra height and
% depth of the current line, but results in the best vertical
% alignment. This is the default.
% \item[\texttt{heightadjust}] adjusts the margin note at the height of the
% current line but not the depth. Note, that this may sometimes result in
% extra height of the current line and in vertical misplacement.
% \item[\texttt{depthadjust}] adjusts the margin note at the depth of the
% current line but not height. Note, that this may sometimes result in extra
% depth of the current line and very often in vertical misplacement.
% \item[\texttt{noadjust}] does not adjust the margin note at the height or
% depth of the current line. Note, that this often results in vertical
% misplacement but seldom in vertical extra space before or after the current
% line.
% \end{description}
%
% \DescribeMacro{\marginnote}
% The command \cs{marginnote}\oarg{left}\marg{right}\oarg{voffset} my be used
% to set a margin note using \textsf{marginnote}. The first optional argument
% and the mandatory argument are same using \cs{marginpar} from the \LaTeX{}
% kernel. Even \cs{reversemarginpar} will be considered. The note \meta{left}
% or \meta{right} will be put at the current vertical position. Second
% optional argument \meta{voffset} may be used to adjust the vertical position
% of the margin note. Use a negative dimension to move it up or a positive
% dimension to move it down.
%
% \DescribeMacro{\marginnoteleftadjust}
% \DescribeMacro{\marginnoterightadjust}
% At some environments, e.g. \texttt{framed} from the \textsf{framed} package
% the horizontal placement of the margin notes are not correct. In this case
% you may redefine \cs{marginnoteleftadjust} and \cs{marginnoterightadjust} to
% fix this. Note that these are macros not lengths! So you have to use
% \cs{renewcommand}, \cs{def} or \cs{let} to change them. You may e.g. use
% \begin{verbatim}
% \begingroup
%   \makeatletter
%   \g@addto@macro\framed{%
%     \let\marginnoteleftadjust\FrameSep
%     \let\marginnoterightadjust\FrameSep
%   }
% \endgroup
% \end{verbatim}\vskip-\baselineskip
% at your preamble after loading package \textsf{framed} to fix the problem
% using \texttt{framed} environment.
%
% NOTE: \cs{marginnoteleftadjust} and \cs{marginnoterightadjust} will be used
% only, if the correct horizontal position cannot be determined using
% PDF\TeX{} features (\cs{pdfsavepos} and \cs{pdflastxpos}). So if you are
% using PDF\LaTeX{} with PDF output or PDF\LaTeX{} with PDF\TeX-version since
% 1.40 or \XeTeX{} you will not need to use the example code above, but you
% will need at least two \LaTeX{} runs to get the correct horizontal positions
% of the margin notes.
%
% \DescribeMacro{\marginnotetextwidth}
% Package \textsl{marginnote} needs to know the real width of the type area to
% find the right margin. While some environments (e.g. thos of package
% \textsl{framed}) change \cs{textwidth}, \textsl{marginnote} defines it's own
% text width macro. If you change type area after \cs{begin\{document\}} you
% should add
% \begin{verbatim}
%   \edef\marginnotetextwidth{\the\textwidth}
% \end{verbatim}\vskip-\baselineskip
% after changing the type area. Maybe you should do this globally using
% \verb|\xdef| instead of \verb|\edef|. Most users will never need to change
% \cs{marginnotetextwidth}.
%
% \DescribeMacro{\marginnotevadjust}
% At some environments the vertical adjustment of the margin note will be
% wrong, e.g. one base line to low. In this case you may use the additional
% optional argument of \cs{marginnote} at every usage of \cs{marginnote} or
% redefine \cs{marginnotevadjust} at the begin of the environment. The default
% definition is \texttt{0pt}.
%
% \DescribeMacro{\raggedleftmarginnote}
% \DescribeMacro{\raggedrightmarginnote}
% These macros define how the margin note will be aligned. The defaults are:
% \begin{itemize}
% \item align margin notes at the left margin right to the margin,
% \item align margin notes at the right margin left to the margin.
% \end{itemize}
% You may change this using \cs{renewcommand}, e.g. use^^A
% \changes{v1.0a}{2006/02/06}{Example to macros \cs{raggedleftmarginnote} and
%   \cs{raggedrightmarginnote} at documentation fixed [thanks to Susumu
%   Tanimura].}
% \begin{verbatim}
% \renewcommand*{\raggedleftmarginnote}{}
% \renewcommand*{\raggedrightmarginnote}{\centering}
% \end{verbatim}\vskip-\baselineskip
% to get justified text at the left and centered text at the right margin.
%
% \DescribeMacro{\marginfont}
% This macro defines the font that will be used to set margin notes. The
% default is \cs{normalcolor}. You may use \cs{renewcommand} to change this,
% e.g. use
% \begin{verbatim}
% \renewcommand*{\marginfont}{\color{red}\sffamily}
% \end{verbatim}\vskip-\baselineskip
% to get red colored margin notes in sans serif font family. You need to load
% e.g. package \textsf{color} to use \cs{color}.
%
%
% \section{Known Problems Using \textsf{marginnote}}
%
% At double side layout (e.g. using class option \texttt{twoside})
% \cs{marginnote} needs to know the number of the current page to decide
% wether the page is odd or even and so wether to use left or right
% margin. \LaTeX{} uses an asynchronous output. Because of this counter
% \texttt{page} should not be used to get the number of the current page
% unless you are at page head or foot. To solve the problem
% \textsf{marginnote} uses a mechanism similar to labels. But this means, that
% the correct margin won't be known at this \LaTeX{} run but only at the
% next. So after adding or deleting a margin note or after each change of page
% break you need two \LaTeX{} runs to get all margins right.
%
% The command \cs{marginnote} uses \cs{strut} and \cs{vadjust} to put the
% margin note at the correct position. But under some circumstances this may
% fail. You may adjust the vertical position of the margin note using the
% second optional argument of \cs{marginnote}. Sometimes even the text outside
% \cs{marginnote} will be moved because of using \cs{marginnote}. You may use
% one of the package options \texttt{fulladjust}, \texttt{heightadjust},
% \texttt{depthajust} or \texttt{noajust} to change the global adjustment or
% a local redefinition of |\mn@strut| or |\mn@zbox|.
%
% Note: The margin note will be placed at the current vertical line. This
% means, if you are using two \cs{marginnote} commands at the same line, they
% will be put on the same place. This is not a bug but a feature!
%
% Since release~1.1b \cs{marginnote} between paragraphs (in vertical mode)
% will place the note between the paragaphs instead of the end of the previous
% paragraph. You may use \cs{leavevmode} or the third optional argument of
% \cs{marginnote} to place it different.
%
% No page break may occure inside a margin note created with \cs{marginnote}.
%
% \cs{marginnote} somewhat different from \cs{marginpar} if used immediate
% after \cs{item}. This is not a bug, it's a feature!
%
% With math \cs{marginnote} may work or may not depending on the math
% environment.
%
% If you are using \XeTeX{}, PDF\LaTeX{} since version~1.40 or PDF\LaTeX{}
% before version~1.40 with PDF output and the horizontal position of
% a margin note is wrong, do one more PDF\LaTeX{} run.
%
% Sometimes lines are stretched vertically using \cs{marginnote}, e.g.\ if
% you're using \cs{marginnote} at a list \emph{and} upper case umlauts like
% ``\"U'' or if \verb|\lineskiplimit>0pt|. In this case
% \verb|\lineskiplimit=0pt| or \verb|\lineskiplimit=-\maxdimen|, or one of the
% options may help.
%
% You should not use \cs{marginnote} at the optional argument of \cs{item}.
%
%
% \StopEventually{\PrintIndex\PrintChanges}
%
% \section{Implementation}
%
% \iffalse
%<*package>
% \fi
%
% First test $\varepsilon$-\TeX.
%    \begin{macrocode}
\begingroup
  \def\@tempb{}%
  \def\@tempa{%
    \PackageError{marginnote}{seems you are not running e-TeX\@tempb}{%
      Since 2004 the LaTeX team recommends to use e-TeX.\MessageBreak
      marginnote since version 1.1d uses e-TeX features.\MessageBreak
      At actual systems `latex' should already use e-TeX.\MessageBreak
      At deprecated systems it may be called `elatex'.\MessageBreak
      Use either unsupported marginnote up to version 1.1c or\MessageBreak
      ask you administrator for LaTeX using e-TeX\@tempb.\MessageBreak
      Not using e-TeX\@tempb\space is a fatal error!\MessageBreak
      Processing cannot be continued!}%
    \endgroup
    \batchmode \errmessage{}\csname @@end\endcsname\end\relax
    \csname endinput\endcsname
  }%
  \expandafter\ifx\csname eTeXversion\endcsname\relax\else
    \ifnum\eTeXversion <2
      \def\@tempb{ V 2}%
    \else
      \let\@tempa\endgroup
    \fi
  \fi
\@tempa
%    \end{macrocode}
%
% Next declare and process the options.
%
% \begin{macro}{\if@mn@verbose}
% Use verbose output mode by default. But you may change this using option
% \texttt{quiet}.
%    \begin{macrocode}
\newif\if@mn@verbose\@mn@verbosetrue
\DeclareOption{verbose}{\@mn@verbosetrue}
\DeclareOption{quiet}{\@mn@verbosefalse}
%    \end{macrocode}
% \end{macro}
%
% \changes{v1.1e}{2009/06/06}{new options \texttt{fulladjust},
%   \texttt{heightadjust}, \texttt{depthadjust}, and \texttt{noadjust}}
% \begin{macro}{\mn@strut}
%   \changes{v1.1e}{2009/06/06}{new (semi internal)}
% The package needs to adjust the margin note at the current line. Sometimes
% this provocates extra vertical line spacing. To avoid this you may redefine
% \cs{mn@strut}. The default value is \cs{strut}.
%    \begin{macrocode}
\newcommand*{\mn@strut}{}
%    \end{macrocode}
% \begin{macro}{\mn@zbox}
%   \changes{v1.1b}{2009/02/16}{new (internal)}
% This macro is used to set a horizontal box without height, depth and width.
%    \begin{macrocode}
\newcommand{\mn@zbox}[1]{}
%    \end{macrocode}
% The options do redefine both, \cs{mn@strut} and \cs{mn@zbox}.
%    \begin{macrocode}
\DeclareOption{fulladjust}{%
  \renewcommand*{\mn@strut}{\strut}%
  \renewcommand{\mn@zbox}[1]{%
    \bgroup
      \setbox\@tempboxa\vbox{#1}%
      \ht\@tempboxa\ht\strutbox
      \dp\@tempboxa\dp\strutbox
      \wd\@tempboxa\z@
      \box\@tempboxa
    \egroup
  }%
}
\DeclareOption{heightadjust}{%
  \renewcommand*{\mn@strut}{\begingroup\dp\strutbox\z@\strut\endgroup}%
  \renewcommand{\mn@zbox}[1]{%
    \bgroup
      \setbox\@tempboxa\vbox{#1}%
      \ht\@tempboxa\ht\strutbox
      \dp\@tempboxa\dp\z@
      \wd\@tempboxa\z@
      \box\@tempboxa
    \egroup
  }%
}
\DeclareOption{depthadjust}{%
  \renewcommand*{\mn@strut}{\begingroup\ht\strutbox\z@\strut\endgroup}%
  \renewcommand{\mn@zbox}[1]{%
    \bgroup
      \setbox\@tempboxa\vbox{#1}%
      \ht\@tempboxa\ht\z@
      \dp\@tempboxa\dp\strutbox
      \wd\@tempboxa\z@
      \box\@tempboxa
    \egroup
  }%
}
\DeclareOption{noadjust}{%
  \renewcommand*{\mn@strut}{\relax}%
  \renewcommand{\mn@zbox}[1]{%
    \bgroup
      \setbox\@tempboxa\vbox{\kern-\ht\strutbox #1}%
      \ht\@tempboxa\ht\z@
      \dp\@tempboxa\dp\z@
      \wd\@tempboxa\z@
      \box\@tempboxa
    \egroup
  }%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%    \begin{macrocode}
\ExecuteOptions{verbose,fulladjust}
\ProcessOptions\relax
%    \end{macrocode}
%
% \begin{macro}{\newmarginnote}
%   We need a macro to define a new note at the \texttt{aux} file. This will
%   be done using the mechanism of \LaTeX{} that is used for
%   \cs{newlabel}. But we use another prefix. This will result in the usual
%   ``Labels(s) may have changed. Rerun to get cross-references right.'' if a
%   margin note is new or have moved to another page.
%    \begin{macrocode}
\newcommand*{\newmarginnote}{\@newl@bel{mn}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\if@mn@pdfmode}
%   \changes{v1.1}{2006/10/23}{new switch}%
%   \changes{v1.1a}{2008/11/10}{PDF\TeX\ since 1.40 allows \cs{pdfsavepos} in
%     DVI mode too}%
%   \changes{v1.1b}{2009/02/16}{if level fixed}%
%   \changes{v1.1c}{2009/02/26}{\protect\XeTeX has working \cs{pdflastxpos}
%     but no \cs{pdftexversion}}%
% We need to know, wether or not PDF\TeX{} and which version of PDF\TeX{} is
% used. With PDF\TeX{} the horizontal output position may be detected using
% \cs{pdfsavepos} and \cs{pdflastxpos}. So the relative position of the margin
% may be calculated. Without PDF\TeX{} only manual adjustment is
% available. While PDF mode or not may change before start of the document,
% setting up the switch is delayed.
%    \begin{macrocode}
\newif\if@mn@pdfmode\@mn@pdfmodefalse
\AtBeginDocument{%
  \begingroup\expandafter\expandafter\expandafter\endgroup
  \expandafter\ifx\csname pdflastxpos\endcsname\relax\else % bg or 1
    \begingroup\expandafter\expandafter\expandafter\endgroup
    \expandafter\ifx\csname pdftexversion\endcsname\relax % bg 2
      \begingroup\expandafter\expandafter\expandafter\endgroup
      \expandafter\ifx\csname pdfoutput\endcsname\relax % bg 3
        \begingroup\expandafter\expandafter\expandafter\endgroup
        \expandafter\ifx\csname XeTeXrevision\endcsname\relax\else % bg 4
          \@mn@pdfmodetrue
        \fi % ed 4
      \else % or 3
        \ifcase\pdfoutput\else\@mn@pdfmodetrue\fi % bg ed 4
      \fi % ed 3
    \else % or 2
      \ifnum \pdftexversion<140 % bg 3
        \begingroup\expandafter\expandafter\expandafter\endgroup
        \expandafter\ifx\csname pdfoutput\endcsname\relax % bg 4
        \else % or 4
          \ifcase\pdfoutput\else\@mn@pdfmodetrue\fi % bg ed 5
        \fi % ed 4
      \else % or 3
        \@mn@pdfmodetrue
      \fi % ed 3
    \fi % ed 2
  \fi % ed 1
  \if@mn@verbose
    \if@mn@pdfmode
      \PackageInfo{marginnote}{%
        \string\pdfoutput\space not 0 and \string\pdflastxpos\space
        available.\MessageBreak
        Extended position detection mode activated\@gobble
      }%
    \else
      \PackageInfo{marginnote}{%
        either \string\pdflastxpos\space or \string\pdfoutput\space not
        available\MessageBreak
        or \string\pdfoutput\space set to 0.\MessageBreak
        Extended position detection mode deactivated\@gobble
      }%
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\marginnotetextwidth}
%   \changes{v1.1}{2006/10/23}{new macro}
% Some environments change \cs{textwidth}. But at PDF mode we need to know the
% real text width to find the right margin. So we use our own text width
% macro. Sometimes it may be usefull if the user can set it up. Because of
% this it is a user command.
%    \begin{macrocode}
\newcommand*{\marginnotetextwidth}{}
\let\marginnotetextwidth\textwidth
\AtBeginDocument{\if@mn@pdfmode\edef\marginnotetextwidth{\the\textwidth}\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@mn@margintest}
%   \changes{v1.1}{2006/10/23}{new PDF mode feature}
% \begin{macro}{\@mn@thispage}
% \begin{macro}{\@mn@atthispage}
% \begin{macro}{\@mn@currpage}
%   \changes{v1.1}{2006/10/23}{new (internal)}
% \begin{macro}{\@mn@currxpos}
%   \changes{v1.1}{2006/10/23}{new (internal)}
% \begin{macro}{mn@abspage}
%   Macro \cs{@mn@margintest} does the complete test, which margin to use. The
%   result may be found at \cs{if@tempswa}. To avoid changes on the last page
%   if there is a new note on the first page, try to count the notes by
%   page. We know that this can not be successfull, but never the less it may
%   be a good try. \cs{@mn@thispage} saves the page number of the last usage
%   of \cs{@mn@margintest}. \cs{@mn@atthispage} saves the number of margin
%   note at this page. But we need to know the absolut page number to do
%   this. So we increase the absolut page number \texttt{mn@abspage} at every
%   \cs{@outputpage}. \cs{@mn@currpage} is the page from the page label if
%   found. \cs{@mn@currxpos} is somehow special. Using PDF\TeX{} the real $x$
%   position may be written with the page label and used to calculate the
%   correct horizontal offset. In this case \cs{marginnoteleftadjust} and
%   \cs{marginnoterightadjust} will not be used!
%    \begin{macrocode}
\newcommand*{\@mn@thispage}{}
\newcommand*{\@mn@currpage}{}
\newcommand*{\@mn@currxpos}{}
\newcounter{mn@abspage}
\AtBeginDocument{\setcounter{mn@abspage}{1}%
  \g@addto@macro\@outputpage{\stepcounter{mn@abspage}}}
\newcommand*{\@mn@margintest}{%
%    \end{macrocode}
%   Number of the next margin note at this page.
%    \begin{macrocode}
  \expandafter\ifx\csname @mn@thispage\endcsname\@empty
    \gdef\@mn@atthispage{1}%
  \else\expandafter\ifnum \@mn@thispage=\value{mn@abspage}%
      \begingroup
        \@tempcnta\@mn@atthispage\advance\@tempcnta by \@ne
        \xdef\@mn@atthispage{\the\@tempcnta}%
      \endgroup
    \else
      \gdef\@mn@atthispage{1}%
    \fi
  \fi
  \xdef\@mn@thispage{\themn@abspage}%
%    \end{macrocode}
%   Use the number of the page and the number of the margin note at this page
%   to save the real number of this page at the \texttt{aux} file. At PDF mode
%   save the current $x$ position too.
%    \begin{macrocode}
  \let\@mn@currpage\relax
  \let\@mn@currxpos\relax
  \if@mn@pdfmode
    \pdfsavepos
    \protected@write\@auxout{\let\themn@abspage\relax}{%
      \string\newmarginnote{note.\@mn@thispage.\@mn@atthispage}{%
        {\themn@abspage}{\noexpand\number\pdflastxpos sp}}%
    }%
  \else
    \protected@write\@auxout{\let\themn@abspage\relax}{%
      \string\newmarginnote{note.\@mn@thispage.\@mn@atthispage}{%
        {\themn@abspage}{}}%
    }%
  \fi
%    \end{macrocode}
%   If the margin note label was not defined, it seems to be new. In this case
%   the absolut page number will be used for the test instead of the saved
%   real page number.
%    \begin{macrocode}
  \expandafter\ifx\csname mn@note.\@mn@thispage.\@mn@atthispage\endcsname\relax
%    \end{macrocode}
%   If we are not in two side mode, we are on a odd page.
%    \begin{macrocode}
    \if@twoside
      \if@mn@verbose
        \PackageInfo{marginnote}{Suggest that margin
          note \@mn@thispage.\@mn@atthispage\space will be on\MessageBreak
          absolute page \themn@abspage.\MessageBreak
          This may be wrong}%
      \fi
      \ifodd\value{mn@abspage}\@tempswatrue\else\@tempswafalse\fi
    \else
      \if@mn@verbose
        \PackageInfo{marginnote}{right page because not two side mode}%
      \fi
      \@tempswatrue
    \fi
  \else
    \edef\@mn@currpage{\csname
      mn@note.\@mn@thispage.\@mn@atthispage\endcsname}%
    \edef\@mn@currxpos{\expandafter\@secondoftwo\@mn@currpage}%
%    \end{macrocode}
% \changes{v1.1d}{2009/05/06}{take care of \cs{hoffset}}%
% Ulrike Fischer suggested a simple change to take care of \cs{hoffset},
% e.g., using package \textsf{crop}.
% \changes{v1.1d}{2009/05/06}{take care of \cs{pdfhorigin}}%
% We use this occasion to take care of \cs{pdfhorigin}, too.
%    \begin{macrocode}
    \edef\@mn@currxpos{\the\dimexpr \@mn@currxpos -\hoffset\relax}%
    \begingroup\expandafter\expandafter\expandafter\endgroup
    \expandafter\ifx\csname pdfhorigin\endcsname\relax\else
      \begingroup\expandafter\expandafter\expandafter\endgroup
      \expandafter\ifx\csname pdfoutput\endcsname\relax\else
        \ifnum \pdfoutput=1 %
          \edef\@mn@currxpos{\the\dimexpr \@mn@currxpos -\pdfhorigin 
            +1in\relax}%
        \fi
      \fi
    \fi
    \edef\@mn@currpage{\expandafter\@firstoftwo\@mn@currpage}%
    \if@mn@verbose
      \PackageInfo{marginnote}{Margin note \@mn@thispage.\@mn@atthispage\space
        is on absolute page \@mn@currpage\MessageBreak}%
    \fi
    \if@twoside
      \ifodd\@mn@currpage\relax
        \@tempswatrue
      \else
        \@tempswafalse
      \fi
    \else
      \if@mn@verbose
        \PackageInfo{marginnote}{right page because not two side mode}%
      \fi
      \@tempswatrue
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\marginnote}
% \begin{macro}{\@mn@marginnote}
% \begin{macro}{\@mn@@marginnote}
% \begin{macro}{\@mn@@@marginnote}
%   \changes{v1.1}{2006/10/23}{new PDF mode feature}
% Command \cs{marginnote} is the main macro of the package. The others are
% helpers to manage the optional arguments.
%    \begin{macrocode}
\newcommand*{\marginnote}{%
  \@dblarg\@mn@marginnote
}
\newcommand{\@mn@marginnote}[2][]{%
  \ifhmode
    \@bsphack
    \begingroup
    \ifdim\@savsk>\z@\else
      \def\:{\@xifnch}\expandafter\def\: { \futurelet\@let@token\@ifnch}%
    \fi
  \else
    \begingroup
  \fi
  \@ifnextchar [{\@mn@@marginnote[{#1}]{#2}}{\@mn@@marginnote[{#1}]{#2}[\z@]}%
}
\newcommand{\@mn@@marginnote}{}
\def\@mn@@marginnote[#1]#2[#3]{%
  \endgroup
%    \end{macrocode}
% In horizontal mode the space hack of the \LaTeX{} kernel will be used. In
% vertical mode this should not be used.
%    \begin{macrocode}
  \ifhmode
    \@mn@@@marginnote[{#1}]{#2}[{#3}]%
    \@esphack
  \else
    \@mn@@@marginnote[{#1}]{#2}[{#3}]%
  \fi
}
\newcommand{\@mn@@@marginnote}{}
\def\@mn@@@marginnote[#1]#2[#3]{%
%    \end{macrocode}
%   \changes{v1.1b}{2009/02/16}{use \cs{mn@vadjust} instead of \cs{vadjust}}%
%   \changes{v1.1e}{2009/06/06}{use \cs{mn@strut} instead of \cs{strut}}%
% All changes (but change of counters that are global because of using the
% \LaTeX{} commands to change them an \cs{gdef} and \cs{xdef}) should be
% local. In h-mode a \cs{strut} will be used to fix base line. The margin
% note will be put to vertical list using \cs{vadjust}. This also means that
% wie are one line to deep. This will be corrected later using negative kern.
% In v-mode wie use a special kind of vbox to simply set everything. Math
% mode should behave like v-mode. And if we are just after an item we have
% to leave v-mode first.
%    \begin{macrocode}
  \begingroup
    \ifmmode\mn@strut\let\@tempa\mn@vadjust\else
      \if@inlabel\leavevmode\fi
      \ifhmode\mn@strut\let\@tempa\mn@vadjust\else\let\@tempa\mn@vlap\fi
    \fi
    \@tempa{%
%    \end{macrocode}
% Everything will be put upwards using a vbox with zero height and depth and
% \cs{vss}. At this box the margin test will be done. If cs{reversemargin}
% was used, the logic switchs. Then the note will be places to the margin.
%    \begin{macrocode}
      \vbox to\z@{%
        \vss
        \@mn@margintest
        \if@reversemargin\if@tempswa
            \@tempswafalse
          \else
            \@tempswatrue
        \fi\fi
        \if@tempswa
          \rlap{%
%    \end{macrocode}
% If \cs{@mn@currpos} is neither \cs{relax} nor empty it is the real current
% $x$ position of the last PDF\LaTeX{} run and may be used to calculate the
% real horizontal offset.
%    \begin{macrocode}
            \ifx\@mn@currxpos\relax
              \kern\marginnoterightadjust
              \if@mn@verbose
                \PackageInfo{marginnote}{%
                  xpos not known,\MessageBreak
                  using \string\marginnoterightadjust}%
              \fi
            \else\ifx\@mn@currxpos\@empty
                \kern\marginnoterightadjust
                \if@mn@verbose
                  \PackageInfo{marginnote}{%
                    xpos not known,\MessageBreak
                    using \string\marginnoterightadjust}%
                \fi
              \else
                \if@mn@verbose
                  \PackageInfo{marginnote}{%
                    xpos seems to be \@mn@currxpos,\MessageBreak
                    \string\marginnoterightadjust
                    \space ignored}%
                \fi
                \begingroup
                  \setlength{\@tempdima}{\@mn@currxpos}%
                  \kern-\@tempdima
                  \if@twoside\ifodd\@mn@currpage\relax
                      \kern\oddsidemargin
                    \else
                      \kern\evensidemargin
                    \fi
                  \else
                    \kern\oddsidemargin
                  \fi
                  \kern 1in
                \endgroup
              \fi
            \fi
            \kern\marginnotetextwidth\kern\marginparsep
            \vbox to\z@{\kern\marginnotevadjust\kern #3
              \vbox to\z@{%
                \hsize\marginparwidth
%    \end{macrocode}
%   Here's the correction of the vertical position. The rest is simple.
%    \begin{macrocode}
                \kern-\parskip
                \marginfont\raggedrightmarginnote\hspace{\z@}\strut#2\endgraf
                \vss}%
              \vss}%
          }%
        \else
%    \end{macrocode}
%   Using the left margin.
%   \changes{v1.1f}{2010/01/05}{missing usage of \cs{marginnotevadjust} on
%     left margin fixed}
%    \begin{macrocode}
          \llap{%
            \vbox to\z@{\kern\marginnotevadjust\kern #3
              \vbox to\z@{%
                \hsize\marginparwidth
%    \end{macrocode}
%   Same like above for left margins.
%    \begin{macrocode}
                \kern-\parskip
                \marginfont\raggedleftmarginnote\hspace{\z@}\strut#1\endgraf
                \vss}%
              \vss}%
            \ifx\@mn@currxpos\relax
              \kern\marginnoteleftadjust
              \if@mn@verbose
                \PackageInfo{marginnote}{%
                  xpos not known,\MessageBreak
                  using \string\marginnoteleftadjust}%
              \fi
            \else\ifx\@mn@currxpos\@empty
                \kern\marginnoteleftadjust
                \if@mn@verbose
                  \PackageInfo{marginnote}{%
                    xpos not known,\MessageBreak
                    using \string\marginnoteleftadjust}%
                \fi
              \else
                \if@mn@verbose
                  \PackageInfo{marginnote}{%
                    xpos seems to be \@mn@currxpos,\MessageBreak
                    \string\marginnoteleftadjust
                    \space ignored}%
                \fi
               \begingroup
                  \kern\@mn@currxpos
                  \if@twoside\ifodd\@mn@currpage\relax
                      \kern-\oddsidemargin
                    \else
                      \kern-\evensidemargin
                    \fi
                  \else
                    \kern-\oddsidemargin
                  \fi
                  \kern-1in
                \endgroup
              \fi
            \fi
            \kern\marginparsep
          }%
        \fi
      }%
    }%
  \endgroup
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\marginnoterightadjust}
% \begin{macro}{\marginnoteleftadjust}
% These may be used to define an automatic horizontal adjust. The default is
% zero. They will be used only if not PDF\TeX{} or PDF\TeX{} before version~1.40
% in DVI mode is used, because in this case the save position features are not
% available.
%    \begin{macrocode}
\newcommand*{\marginnoterightadjust}{}
\newcommand*{\marginnoteleftadjust}{}
\let\marginnoterightadjust\z@
\let\marginnoteleftadjust\z@
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\marginnotevadjust}
% This may be used to define an automatic vertical adjust. The defaul tis
% zero. Values greater than zero will move the margin note down, values less
% than zero will move the margin note up.
%    \begin{macrocode}
\newcommand*{\marginnotevadjust}{}
\let\marginnotevadjust\z@
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\mn@vlap}
% This macro is used to set a vertical box without size at vertical mode.
%    \begin{macrocode}
\newcommand{\mn@vlap}[1]{%
  \setbox\@tempboxa\vbox to \ht\strutbox{#1\vss}%
  \box\@tempboxa\vskip-\baselineskip
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\mn@vadjust}
%   \changes{v1.1b}{2009/02/16}{new (internal)}
% This macro is used to set a vertical box at horizontal mode.
%    \begin{macrocode}
\newcommand{\mn@vadjust}[1]{%
  \mn@zbox{\kern-\parskip
    \leavevmode\vadjust{#1}%
    \kern\parskip
  }%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\marginfont}
%  \changes{v1.0a}{2006/02/06}{Use \cs{providecommand} to define it.}
% \begin{macro}{\raggedleftmarginnote}
% \begin{macro}{\raggedrightmarginnote}
%   These are very simple. A class may also define \cs{marginfont}. Use this
%   if available. I don't use \cs{let} for the definitions of the ragged
%   macros, so the meaning may change loading e.g. package \textsf{ragged2e}.
%    \begin{macrocode}
\providecommand*{\marginfont}{}
\newcommand*{\raggedleftmarginnote}{\raggedleft}
\newcommand*{\raggedrightmarginnote}{\raggedright}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \Finale
%
\endinput
%
% end of `marginnote.dtx'
%
% \iffalse
%%% Local Variables:
%%% mode: doc-tex
%%% TeX-master: t
%%% End:
% \fi