This seems to be a bug in algorithm2e, it occurs without any of your customisations, and should be reported to the package author; a similar bug was fixed in the last releease.
A workaround is to write \\[-\baselineskip]
just after the offending block. Here it is in your example, below that I give minial example displaying the bug.
\documentclass{article}
\usepackage[utf8x]{inputenc}
\usepackage[longend]{algorithm2e}
\makeatletter
\newcommand\@algoVerboseBlock[3]{%
\hspace{-0.8em} \KwSty{#1} \ifhmode \\ \fi #2 \ifhmode \\ \fi \hspace{-0.8em} \KwSty{#3}%
}
\newcommand\@algoDefVerboseIf[2]{%
\renewcommand\algocf@uIf[2]%
{\If@ifthen{##1}\If@noend{\@algoVerboseBlock{#1}{##2}{#2}}}
%
\renewcommand\algocf@If[2]%
{\If@ifthen{##1}\If@noend{\@algoVerboseBlock{#1}{##2}{#2}}}
%
}
\newcommand\@algoDefVerboseElse[2]{%
\renewcommand{\algocf@Else}[1]%
{\Else@else\If@endif{\@algoVerboseBlock{#1}{##1}{#2}}}
\renewcommand{\algocf@uElse}[1]%
{\Else@else\If@endif{\@algoVerboseBlock{#1}{##1}{#2}}}
}
\makeatother
\begin{document}
\section{Customization}
\makeatletter
\SetKwIF{If}{ElseIf}{Else}{Si}{Alors}{Sinon Si}{Sinon}{}
\@algoDefVerboseIf{Début Si}{Fin Si}
\@algoDefVerboseElse{Début Sinon}{Fin Sinon}
\makeatother
\begin{algorithm}[H]
\uIf{1}{if...}%
\Else{else... \\ \If{1}{if...}\\[-\baselineskip]}%
\end{algorithm}
\end{document}
\documentclass{article}
\usepackage[utf8x]{inputenc}
\usepackage[longend]{algorithm2e}
\begin{document}
\thispagestyle{empty}
\begin{algorithm}[H]
\If{1}{if...}%
\Else{else... \\ \uIf{2}{if...}}%
\end{algorithm}
\end{document}
TeX tries to align the base lines of the first line of the pages.
The base line of the first element on the page is put with distance \topskip
unless the element is larger.
Example without \vspace*
:
\documentclass{article}
\usepackage[margin=1in]{geometry}
\showboxdepth=\maxdimen
\showboxbreadth=\maxdimen
\tracingonline=1
\typeout{\noexpand\topskip = \the\topskip}
\typeout{\noexpand\baselineskip = \the\baselineskip}
\begin{document}
% \vspace*{1in}
First line
\showlists
\end{document}
Log output:
\topskip = 10.0pt
\baselineskip = 12.0pt
[...]
### vertical mode entered at line 0
### current page:
\write-{}
\glue(\topskip) 3.05556
\hbox(6.94444+0.0)x469.75502, glue set 414.8105fil
.\hbox(0.0+0.0)x15.0
.\OT1/cmr/m/n/10 F
.\OT1/cmr/m/n/10 i
.\OT1/cmr/m/n/10 r
.\OT1/cmr/m/n/10 s
.\OT1/cmr/m/n/10 t
[...]
The height of the first line is 6.94444 pt, \topskip
has value 10 pt, therefore TeX inserts an additional 3.05556 pt at the top of the page to move the base line to the full 10 pt.
Now the example with \vspace*
:
### vertical mode entered at line 0
### current page:
\write-{}
\glue(\topskip) 10.0
\rule(0.0+0.0)x*
\penalty 10000
\glue 72.26999
\glue 0.0
\glue(\parskip) 0.0 plus 1.0
\glue(\baselineskip) 5.05556
\hbox(6.94444+0.0)x469.75502, glue set 414.8105fil
.\hbox(0.0+0.0)x15.0
.\OT1/cmr/m/n/10 F
.\OT1/cmr/m/n/10 i
.\OT1/cmr/m/n/10 r
.\OT1/cmr/m/n/10 s
.\OT1/cmr/m/n/10 t
TeX removes glue at the start of the page. The star form \vspace*
prevents this by starting with an invisible rule, its height and depth are zero pt. The rule is also the top element and TeX inserts the full \topskip
to move the base line of the rule at the same height as the line in of the previous example. Then the glue amount of 72 pt from the argument of \vspace*{1in}
follows. A new paragraph starts with the insertion of \parskip
(here 0 pt) and the first line is set according to \baselineskip
to get equally vertically spaced lines. The amount is 12 pt, the height of the line is 6.94444 pt and the remaining amount of 5.05556 pt is added.
First case: \topskip
- ht(first line) = 3.05556 pt
Second case:
Full \topskip
= 10 pt
\vspace
= 1 in = 72.26999 pt
\parskip
= 0 pt
\baselineskip
- ht(first line) = 5.05556 pt
Sum = 1 in + 15.05556 pt
Thus the spacing can be fixed by removing the unwanted spaces:
\vspace*{1in}
\vspace{-\parskip}
\vspace{-\baselineskip}
% next element smaller than \topskip and \baselineskip
- if the height of the text line is not higher than
\baselineskip
and \topskip
(e.g., usually a normal text line) or
- if
\baselineskip
has the same amount as \topskip
(the latter is usually smaller and therefore unlikely to happen).
Otherwise the height of the following element needs to be known or measured in order to calculate and compare, which parts of \baselineskip
and topskip
would be added.
If the height of the first element is larger than both \baselineskip
and \topskip
, the error will be the difference between them. Then the correct space can be achieved by
\vspace*{1in}
\vspace{-\parskip}
\vspace{-\topskip}
% next element larger than \baselineskip and \topskip
If it is not clear, where the \vspace*
will go, then the following also works, if the space is set in the middle of the page:
\vspace{\parskip}
\vspace{\baselineskip}
\vspace*{1in}
\vspace{-\parskip}
\vspace{-\baselineskip}
The first two \vspace
are discarded at the top of a new page.
Best Answer
A
\vspace
command at the beginning of\marginpar
establishes the skip as the first object in the box, so the vertical alignment will be the top of this space.You can measure the height of the margin note and back up by that amount.
The red rules are just to show the baselines and see that the marginpar is correctly raised by 2pt.