I have a bunch of individual .tex files and want to be able to click on a link in one PDF file and have the appropriate PDF file open. These are small files, so don't really need to index into them, just have them open.
The file name to open can (in most cases) be determined from the descriptive link text -- algorithm below. There will be a few cases where the file name can not be easily determined, so for that I want to also be able to manually specify it. So, I define:
\NewDocumentCommand{\CrossReference}{O{\NoValue} m}
where
#1 = optional file name (determined from #2 if not provided)
#2 = link text
I have the case where I manually specify the file name working in the MWE below. However, since the file names can easily be determined, I would like to not have to provide it. That is where \ConvertToFileNameWithPath
is supposed to do its job.
File Naming Algorithm:
If the file name is not specified, it is obtained form the given link text by:
- Change all characters to lower case.
- Replace any blanks with a dash.
- Add the
.pdf
extension
Notes:
For the MWE below to work (ie, to test that the links are click able), the file
equation-of-circle.pdf
must exist in the current directory.Eventually, the macro
\ConvertToFileNameWithPath
will search for this file name in a particular list of directories -- will tackle that next. So that will be step 4 in the above algorithm.I did look at the
xr
package and not sure it is appropriate for this case, mostly due to Note 2 (there may be multiple files with the same name).
References/Failed Attempts:
I attempted to adapt Joseph Wright's answer at Are there purely expandable variants of \MakeUppercase?, but was not successful. Also on the same question it mentions that
\tl_expandable_lowercase:n
is now available so that should certainly help. But not understandingexpl3
, I can't get much further than this.My start at this is based on Bruno Le Floch's answer to my question: Text being output when none should be. Not that I was able to get very far with this, but perhaps this might help others figure this out.
I also attempted to use a sequence of
\StrSubstitute{\Temp}{A}{A}[\Temp]
,\StrSubstitute{\Temp}{B}{b}[\Temp]
to make the conversion, but this runs into expansion issues, and using\tl_expandable_lowercase:n
should be easier.
Code:
When the text above and below the \hrule
is that same then this should be working: assuming of course you have equation-of-circle.pdf
in the current directory, and can open it via lines 1 and 3. Currently I can only open it via line 1 where I have manually specified the file name:
\documentclass{article}
\usepackage{xparse}
\usepackage{xcolor}
\usepackage{hyperref}
%\ExplSyntaxOn -- am guessing that this will be needed...
\DeclareExpandableDocumentCommand{\ConvertToFileNameWithPath}{o m}{%
% This will eventually also locate a file in a given path
\IfNoValueTF{#1}{% file name not specified so need to figure it out
% ????????? What goes here ???????
./#2.pdf% Need to convert #2 to a file name. ????????
}{% We were given, the file name -- Even I can handle this case
./#1.pdf%
}%
}
%\ExplSyntaxOff
\NewDocumentCommand{\CrossReference}{O{\NoValue} m}{%
% #1 = optional file name (determined from #2 if not provided)
% #2 = link text
\edef\PdfFileName{\ConvertToFileNameWithPath[#1]{#2}}%
\IfFileExists{\PdfFileName}{%
\href{run:\PdfFileName}{#2}%
}{%
\fcolorbox{red}{yellow}{#2}%
\footnote{Unable to locate \PdfFileName}%
}%
}%
\begin{document}
Manual References: \CrossReference[equation-of-circle]{Equation of Circle}%
Manual References: \CrossReference[non-existant-file]{Non Existant File}%
\medskip\hrule\medskip
Automatic References: \CrossReference{Equation of Circle}%
Automatic References: \CrossReference{Non Existant File}%
\end{document}
\tl_expandable_lowercase
is a rather specialized function, which should in most cases not be needed. Also, it is very slow. Actually, thinking about it again, we may want to generalize it, I need to think.xstring
and\Makelowercase