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'm writing a document in LyX which I will add to over time, adding a new section on each edit. I have enabled hyperref in the pdf properties and have a table of contents which correctly links to each section. However, for convenience I'd like to have a link before the toc which jumps directly to the start of the most recent (i.e. last) section.

Is it possible to access the variables used in generating the toc for this purpose? Alternatively, can I just put a label at the very end of the document (which would be a reference to a subsection) and do some maths to find the section itself? If so, how would I then link to it?

share|improve this question
    
Welcome to TeX.sx! Please register on this site, too. –  Stephan Lehmke Jan 4 '13 at 18:33
    
Wouldn't it be easiest to put a label lastsection or similar manually always to the last section? I don't see too much benefit in automating this. –  Stephan Lehmke Jan 4 '13 at 18:36
add comment

2 Answers

I wouldn't take this too seriously, but it works, even if \section* is used. Of course you should never use \label{lastsection} in your document, but only \ref{lastsection} or \pageref{lastsection}.

\documentclass{article}
\usepackage{atenddvi}
\usepackage{hyperref}

\makeatletter
\let\latex@@section\section
\renewcommand{\section}{\@ifstar{\new@@ssection}{\@dblarg\new@@section}}
\def\new@@section[#1]#2{%
  \latex@@section[#1]{#2}%
  \xdef\perhaps@last{perhaps@last@\thesection}%
  \label{\perhaps@last}%
}
\newcounter{ssection}
\def\new@@ssection#1{%
  \latex@@section*{#1}%
  \refstepcounter{ssection}%
  \xdef\perhaps@last{perhaps@last@star@\thessection}%
  \label{\perhaps@last}%
}
\AtEndDvi{
\write\@mainaux{%
  \unexpanded{\global\expandafter\let\expandafter\r@lastsection\csname} r@\perhaps@last\endcsname}
}
\makeatother

\begin{document}

Last section is at page \pageref{lastsection}

\tableofcontents

\section{Abc}
abc
\newpage

\section{Def}
def
\newpage

\section{Ghi}
ghi

\end{document}

enter image description here

share|improve this answer
add comment

The MWE below does what I was hoping to achieve, but ideally I'd like to do something so that I don't have to manually add the \newpage or \label{sec:lastmeeting} bits. I think I need to do a \renewcommand but, as I understand it, it all gets a bit messy because \section takes arguments.

\documentclass{report}

\usepackage{hyperref}
\usepackage{nameref}
\usepackage{lipsum}


\begin{document}
Last meeting: \nameref{sec:lastmeeting}
\tableofcontents
\newpage\section{Meeting 1}\label{sec:lastmeeting}\lipsum[1-6]
\newpage\section{Meeting 2}\label{sec:lastmeeting}\lipsum[1-6]
\end{document}
share|improve this answer
    
@Stephan Thanks for the suggestion. I'm not sure what happened with my site registration and for some reason I can't comment on the original question. The benefit of automating the labelling is that I'm lazy and prone to error so would rather something repetitive be done for me. –  howecome Jan 8 '13 at 9:53
add comment

Your Answer

 
discard

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