I would like to insert a hyperlink or two for each heading and I would like to have them inserted into the margin such that they are aligned with the header. I was hoping to create a command which would do this automatically, but this is proving to be harder than I expected.

So to begin with, I simply had

\documentclass{article}

\usepackage{marginnote}

\marginnote{#1}[-1.9\baselineskip]%
\noindent}

\reversemarginpar

\begin{document}

\section{Section 1}

Lorem Ipsum

\subsection{Subsection 1.1}

Lorem Ipsum

\end{document}


Where the -1.9\baselineskip was chosen by trial and error, but this no longer works for other headings (e.g. \subsection). Is there a command which defines the spacing after a heading?

The \noindent preserves the un-indented paragraph, but requires that no lines separate \Hlink and the start of the text. Though this is a relatively minor issue, surely there is a way around it.

This command has the additional problem that if the header is near the end of a page, it will most most likely cause a page break. I have tried inserting \nobreak before and after the command, but to no avail.

An additional problem with this is that the margin changes sides depending on the page, though in print it probably looks better, in a PDF this may not be desirable (and the links are meant to be hyperlinks). Is it possible to modify \Hlink to always use the left side?

-
Welcome to TeX.SE! –  cmhughes Mar 7 '13 at 1:41
Please add a complete minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. Headings and their behavior depends on the used document class ... –  Kurt Mar 7 '13 at 1:59
One approach might be to hack into the definition of \@section to make it take another optional argument –  cmhughes Mar 7 '13 at 5:33
I have added the MWE. I also have realised that marginnote may not be the best way to address this since it changes side every time. It would probably be better if it remained beside the number. @cmhughes How hard would it be to hack into the definition of \@section? I have never done such hacking in LaTeX. –  Josh Mar 7 '13 at 6:59

If you're willing to call macro on a as-needed basis to insert content to the left of the sectional unit heading, then using \sectionlink as defined below is sufficient:

\documentclass{article}
\makeatletter
\def\@seccntformat#1{% Update sectional unit formatting macro
\llap{\normalfont\normalsize\@sectionlink\hspace*{\marginparsep}}\fi% ...print it in left margin.
\let\old@xsect\@xsect
\makeatother

\begin{document}

\section{Section 1} Lorem Ipsum

\subsection{Subsection 1.1} Lorem Ipsum

\subsection{Subsection 1.2} Lorem Ipsum

\end{document}


The intent would be to call \sectionlink{<stuff>} before a call to \section and friends.

It would be possible to incorporate this as part of the sectioning macro, although that would require more work. Additionally, more work is required if the "section link" is too wide to fit in the margin (allowing, for example, to place it in a \parbox).

For allowing line breaks in the sectional link, you can set it in the tabular. In the example below, the code has been updated to store the link in a tabular (default is right- and [t]op-aligned, and then stored in a box. This box is then \smashed to not impede any overflow below the line that it's called in.

\documentclass{article}
\makeatletter
% User interface macro for defining section link
\tabular[t]{@{}#1@{}}#2\endtabular}}
\def\@seccntformat#1{% Update sectional unit formatting macro
\llap{\normalfont\normalsize\smash{\usebox\@sectionlinkbox}\hspace*{\marginparsep}}% ...print it in left margin.
\let\old@xsect\@xsect
\makeatother

\begin{document}

\section{Section 1} Lorem Ipsum

\subsection{Subsection 1.1} Lorem Ipsum

\subsection{Subsection 1.2} Lorem Ipsum

\subsection{Subsection 1.3} Lorem Ipsum

\end{document}


If you have more paragraph-style sectional links, you can modify the line of code marked *** to read

\newcommand{\sectionlink}[2][p{\marginparwidth}]{%


or, experiment with the regular tabular settings contained within the definition of \sectionlink.

-
That nearly does the job! The "section link" in this case will usually be one short hyperlink (and the displayed text will be really short) or alternatively a quick reference to some book. What you have done above works brilliantly, and to me surprise, I actually understand how it works. Ideally however, I do need it to allow for two or three lines to be displayed (with the top line being aligned with the title). I have tried modifying the code above, but to no avail :( How would you allow for multiple lines? e.g. \sectionlink{link1 \\ link2} –  Josh Mar 7 '13 at 12:29
@Josh: I've added something... –  Werner Mar 7 '13 at 17:58
Thanks. That does exactly what I needed. I don't completely understand how it achieves it, but I shall read up on it. –  Josh Mar 7 '13 at 23:06