Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}

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

\reversemarginpar

\begin{document}

\section{Section 1}

\Hlink{Sec 1 Link}
Lorem Ipsum

\subsection{Subsection 1.1}

\Hlink{Subsec 2 Link}
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?

share|improve this question
    
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

1 Answer 1

up vote 4 down vote accepted

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:

enter image description here

\documentclass{article}
\makeatletter
\def\sectionlink#1{\def\@sectionlink{#1}}% User interface macro for defining section link
\let\@sectionlink\@empty% Default value for \@sectionlink
\def\@seccntformat#1{% Update sectional unit formatting macro
  \ifx\@sectionlink\@empty\else% If a section link exists...
    \llap{\normalfont\normalsize\@sectionlink\hspace*{\marginparsep}}\fi% ...print it in left margin.
  \csname the#1\endcsname\quad}
\let\old@xsect\@xsect
\def\@xsect{\let\@sectionlink\@empty\old@xsect}% Erase old section link
\makeatother

\begin{document}

\sectionlink{Sec 1 Link}
\section{Section 1} Lorem Ipsum

\sectionlink{Subsec 2 Link}
\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.

enter image description here

\documentclass{article}
\makeatletter
\newsavebox\@sectionlinkbox
% User interface macro for defining section link
\newcommand{\sectionlink}[2][r]{% ***
  \savebox{\@sectionlinkbox}{%
    \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.
  \csname the#1\endcsname\quad}
\let\old@xsect\@xsect
\def\@xsect{\savebox{\@sectionlinkbox}{\@empty}\old@xsect}% Erase old section link
\makeatother

\begin{document}

\sectionlink{Sec 1 Link}
\section{Section 1} Lorem Ipsum

\sectionlink{Subsec 1 \\ Link}
\subsection{Subsection 1.1} Lorem Ipsum

\subsection{Subsection 1.2} Lorem Ipsum

\sectionlink[l]{Subsec 3 \\ Link}
\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.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.