You can use the normal \label/\ref system for this. The link is then done with
\hyperref[label]{text}
Discussion \label setting and destination name
A \label
uses the currently active anchor (usually, the last anchor set) when
it writes the reference data into the .aux
file. The example does not show
whether the target for preamble/hyperref.tex
should be the section with the
same title or it should moved below to the position of \labelfile
. In the first
case, the part in the solution example file between the %%%
markers should be:
%%%
\newcommand*{\labelfile}[1]{%
\label{file:#1}%
\index{file!#1@\string\FileName{#1}}%
}
%%%
If \labelfile
should set its own anchor, but the destination name does not
matter, then the part between the %%%
markers should be:
%%%
\newcommand*{\labelfile}[1]{%
\phantomsection
\label{file:#1}%
\index{file!#1@\string\FileName{#1}}%
}
%%%
The anchor setting is done by \phantomsection
.
The full variant below also controls the destination name. It uses the fact that hyperref
's
redefinition of \refstepcounter
sets an anchor. (With default options) the destination
name is constructed from the counter name and the counter value. Different destinations must not share the same destination name. A problem is that \the<counter>
is not always unique.
Therefore hyperref
uses \theH<counter>
for destination names. The file names here
are unique because they are also used for \label
. \thefile
shows the "file number"
that is not needed here (at end of document it could be used to get the maximum number of files). \theHfile
is completely different, it shows the file name and the destination
name would be file.example.tex
for file example.tex
, the first part file
is the
counter name, the following dot is hardwired, and the remaining part is the file name.
File name formatting
File names can be formatted in many ways. There are packages that deal with
hyphenation, font settings, … As an example, I have used package url
to
define a formatting command \FileName
and also added this to the index
entry to show the special syntax of the index entries. I assume that the
file names does not contain too fancy characters. Otherwise some extra
work would be needed to deal with the problematic characters.
\documentclass{article}
\usepackage{hyperref}
\usepackage{url}
\DeclareUrlCommand{\FileName}{\urlstyle{tt}}
\usepackage{makeidx}
\makeindex
%%%
\newcounter{file}
\providecommand*{\theHfile}{\thefile}
\newcommand*{\labelfile}[1]{%
\renewcommand*{\theHfile}{#1}%
\refstepcounter{file}%
\label{file:#1}%
\index{file!#1@\string\FileName{#1}}%
}
%%%
\newcommand*{\file}[1]{%
\hyperref[file:#1]{\FileName{#1}}%
}
\begin{document}
\section{about links in pdf}
The code is in \file{preamble/hyperref.tex}
\dots
\section{preamble/hyperref.tex}
\labelfile{preamble/hyperref.tex}
\printindex
\end{document}
Best Answer
Assumptions:
hyperref
is loaded.\hyperlink
.hyperref
(hypertexnames
,naturalnames=false
,pageanchor
).With these assumptions, the following example patches the index writing to add the (sub)(sub)section values to the page number, constructing a composite page number (Makeindex supports up to ten parts). This also should automatically disable implicit page ranges (don't make too much sense with section numbers).
A project specific style file
\jobname.mst
redefines some keys to insert a macro in front of the page numbers. Because of the fixed format of the page number curly braces around the argument are not needed.Then the anchor names can be calculated using the counter values stored in the composite artificial page number of the index entry.
Full example:
The raw index file
\jobname.idx
:The index file
\jobname.ind
, generated by Makeindex:And finally the index on the page:
Update for
\see
The first token after
\MyIndexPage
is the "encap" command (e.g.,\see
). Therefore the updated example adds macro\MyEncap
before the "encap" command and puts an additional\MyIndexPage
before the page number in the argument of the "encap" command via the style file for makeindex:Then
\MyIndexPage
is redefined in such a way, that it ignores itself, if an "encap" command follows, detected by the macro\MyEncap
: