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.

The usual practice is that the index entries link to a page in the text where the word occurs. I am trying to index a list of files and am indexing each word in the file name. In the MWE below, clicking on the link in the index takes me to the appropriate section in the doc, and then I can click on that to open the file.

Since the body of this document has no real meaning in this case (I am only interested in the index), I would like a way to modify this two step process to be just one step. So, I would like the links in the index to be links to the file (as they are now in the main body of the document). I am assuming that this should only require a change to the \ProcessWord macro.

Notes:

  • The datatool package was used here only since my real use case gets the file names via this package.
  • For the output links to open the actual files you need to use a PDF reader that supports that (such as Acrobat). The PDF viewer with TeXShop 2.43 does have support, but not the one with TeXWorks 2.3.

References:

Code:

\documentclass{article}
\usepackage{datatool}% database
\usepackage{imakeidx}% indexing
\usepackage{hyperref}% For cross references
\makeindex

% ---------- Process Each Word:
%
% http://tex.stackexchange.com/questions/12810/how-do-i-split-a-string
\makeatletter
\def\ProcessEachWord#1{%
    \begingroup%
    \edef\@tempa{#1\space}%
    \gdef\@tempb{#1}%
    \expandafter\endgroup%
    \expandafter\ReadWords\@tempa\relax%
}%
\def\ReadWords#1 #2\relax{%
      \ProcessWord[\@tempb]{#1}%  #1 = substr, #2 = rest of string
      \begingroup%
      \ifx\relax#2\relax% is #2 empty?
         \def\next{\endgroup\EndProcessWords}% your own end-macro if required
      \else%
         \def\next{\endgroup\ReadWords#2\relax}%
      \fi%
      \next%
}%
\makeatother
% ---------- 


\newcommand{\ProcessWord}[2][]{%
    %\par\noindent\href{run:#1.pdf}{#1}% Moved Below
    \index{#2!#1}%
    %~#2%
}%
\newcommand{\EndProcessWords}{}

\begin{document}
\DTLnewdb{DB}%

\DTLnewrow{DB}% 
\DTLnewdbentry{DB}{FileName}{Now good enough}% 

\DTLnewrow{DB}% 
\DTLnewdbentry{DB}{FileName}{Now good enough for you and me}% 

\DTLforeach{DB}{\FileName=FileName}{%
    \section{\FileName}%
    \par\noindent\href{run:\FileName.pdf}{\FileName}%
    \ProcessEachWord{\FileName}%
}%

\printindex
\end{document}
share|improve this question

1 Answer 1

up vote 3 down vote accepted

It turns out that the most obvious solution actually works -- not sure why this did not occur to me earlier:

\newcommand{\ProcessWord}[2][]{%
    \index{#2!\href{run:#1.pdf}{#1}}%
}%

Futher Improvements

enter image description here

share|improve this answer

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.