\RequirePackage{tikz}
\RequirePackage{ifthen}
\RequirePackage{xkeyval}

\newcommand*{\sectionName}{Liste des notes}
\newcommand*{\longNotesSectionName}{Texte des longues notes}

\newcommand\listofnotes{
  \section*{\sectionName} \@starttoc{notes}
}
\newlength\uml@xa
\newlength\uml@xb

\newcommand\l@note[2]{\noindent\hspace{5mm} Page #2 : \parbox{10cm}{#1}\par}

\newcommand*{\notesStyle}[1]{\presetkeys[notes]{note}{#1}{}}

% Command for inserting a note item
\newlength\note@width
\providecommand{\note}{}
\define@key[notes]{note}{font}{\def\note@font{#1}}
\define@key[notes]{note}{author}{\def\note@author{#1}}
\define@key[notes]{note}{color}{\def\note@color{#1}}
\define@key[notes]{note}{width}{\setlength\note@width{#1}}
\define@boolkey[notes]{note}{delete}[true]{}
\define@boolkey[notes]{note}{reverse}[true]{}% does not work using reversemarginpar => not use marginpar?
\presetkeys[notes]{note}{font=\normalsize,author=,color=orange,width=3cm,delete=false,reverse=false}{}
\renewcommand{\note}[3][]{% #1:options, #2:underlined text, #3:margin note
  % Setting options
  \setkeys[notes]{note}{#1}%
  % Add to note list
  \addcontentsline{notes}{note}{\protect{#3}}%
  % The underlined text 
  \def\notes@tempa{#2}%
  \ifx\notes@tempa\@empty%
    \begin{tikzpicture}[remember picture]%
      \node[coordinate] (inText) {};%
    \end{tikzpicture}%
  \else%
    \begin{tikzpicture}[remember picture,baseline=(inText.base)]%
      \node[inner sep=0pt,color=\note@color] (inText) {#2};%
      \ifnotes@note@delete%
      \draw[draw=\note@color,thick] (inText.west) -- (inText.east);
      \else% 
      \fi%
    \end{tikzpicture}%
  \fi%
  % Make the margin par
  \marginpar{%
    \begin{tikzpicture}[remember picture,font=\note@font,baseline=-0.5ex]%
      \draw node[draw=black,fill=\note@color!50,text width=\note@width] (inNote) {\note@author{} #3};%author empty?
    \end{tikzpicture}%
  }%
  % 
  \ifx\notes@tempa\@empty%
    \begin{tikzpicture}[remember picture, overlay]%
      \pgfextractx{\uml@xa}{\pgfpointanchor{inNote}{center}}  
      \pgfextractx{\uml@xb}{\pgfpointanchor{inText}{center}}  
      \ifthenelse{\lengthtest{\uml@xa > \uml@xb}}{
        \draw[draw=\note@color,thick] (inText.south) -- ([yshift=-0.1cm]inText.south) -| ([xshift=-0.2cm]inNote.west) -| (inNote.west);%
      }{
        \draw[draw=\note@color,thick] (inText.south) -- ([yshift=-0.1cm]inText.south) -| ([xshift=0.2cm]inNote.east) -| (inNote.east);%
      }
    \end{tikzpicture}%
  \else%
    \begin{tikzpicture}[remember picture, overlay]%
      \pgfextractx{\uml@xa}{\pgfpointanchor{inNote}{center}}  
      \pgfextractx{\uml@xb}{\pgfpointanchor{inText}{center}}  
      \ifthenelse{\lengthtest{\uml@xa > \uml@xb}}{
        \draw[draw=\note@color,thick] ([yshift=-0.2mm]inText.south) -- ([yshift=-1mm]inText.south) -| ([xshift=-0.2cm]inNote.west) -| (inNote.west);%
        \draw[draw=\note@color,thick] ([yshift=-0.2mm]inText.south west) -- ([yshift=-0.2mm]inText.south east);%
      }{
        \draw[draw=\note@color,thick] ([yshift=-0.2mm]inText.south) -- ([yshift=-1mm]inText.south) -| ([xshift=0.2cm]inNote.east) -| (inNote.east);%
        \draw[draw=\note@color,thick] ([yshift=-0.2mm]inText.south west) -- ([yshift=-0.2mm]inText.south east);%
      }
      % \draw ([yshift=1.5mm]inNote.north west) node[right] {\color{\note@color}\footnotesize \no :};
    \end{tikzpicture}%
  \fi%
}%

