# Cross-referencing between local files with \href

I am struggling with the creation of cross-references between different files. What I want to do is the following. I want to create two separate documents (books). One of the documents (doc1) will include exercises, and the other document (doc2) will provide the solutions to these exercises. What I want is to create a link after each exercise in doc1 that would say something like: "the solution is here", and when you click on "here", it opens doc2 and takes you to the solution of the exercise.

Similarly, in doc2 there would be a header for each exercise solution stating "this is the solution to exercise x", and when you click on x, it would take you back to doc1 right at the beginning of the exercise.

I looked at many related topics on this forum, and with what I found I thought at first I could do the job with a combination of the hyperref and xr-hyper packages. I tried with something like this:

doc1:

\documentclass[11pt,a4paper,fleqn]{book}
\usepackage{xr-hyper}
\externaldocument[doc2-]{doc2}
\usepackage{hyperref}
\usepackage[nopar]{lipsum}

\begin{document}

\lipsum[1-6]
\\ \\
This is exercise 1 \label{ex1} \\
\lipsum[1-6] \\
Solution is \href{doc2.pdf#correx1}{\textbf{here}}.
\\ \\
This is exercise 2 \label{ex2} \\
\lipsum[1-6] \\
Solution is \href{doc2.pdf#correx2}{\textbf{here}}.
\\ \\
\lipsum[1-6]

\end{document}


and doc2:

\documentclass[11pt,a4paper,fleqn]{book}
\usepackage{xr-hyper}
\externaldocument[doc1-]{doc1}
\usepackage{hyperref}
\usepackage[nopar]{lipsum}

\begin{document}

\lipsum[1-6]
\\ \\
This is the solution to exercise \href{doc1.pdf#ex1}{1} \label{correx1} \\
\lipsum[1-6]
\\ \\
This is the solution to exercise \href{doc1.pdf#ex2}{2} \label{correx2} \\
\lipsum[1-6]
\\ \\
\lipsum[1-6]

\end{document}


In a sense, it works: the hyperlinks are created, and they do open the other document. However, when clicking on the link, it does not lead to the \ref-erenced label, but rather to some random spot of the document. This ruins the whole thing.

Would you have any suggestion?

• Welcome! Never use \\  to end lines outside special contexts such as tabular and array. You haven't used anything which is cross-reference-able in either document. No sections or numbered lists of anything like that. So there is nothing to label except the document generally. – cfr May 23 '17 at 21:02
• Thanks a lot for your comments! So, is there a way I can do what I want (referencing from one document to another at arbitrary locations with activable links) by using something else than references and labels? – Romain Legrand May 23 '17 at 21:06
• @cfr not sure what you mean by xr being newer, it hasn't changed since 1994 as far as I can see? – David Carlisle May 23 '17 at 21:54
• Thanks again for trying. The reason for which I want lable to be placed at arbitrary locations is the one I explained in my original post: I want to create hyperlinks between different documents, and those hyperlinks would refer to exercises (in doc 1) and their corrections (in doc 2). As you mentioned, labels will not work for objects which are not labelable (like chapters, sections, and so on). Obviously the location where I want to place the links (the sentences "Solution is here" and "This is the solution to exercise x", at "here" and "x") don't belong to the class of labelable elements. – Romain Legrand May 23 '17 at 21:57
• So I need to find a substitute to labels, something that would create a kind of anchor at the desired locations, and then a way to create a hyperlink that would link to this anchor. The solution I mentioned in my original post works for labelable elements (I originally tried with equations, and it worked fine), but at arbitrary non-labelable locations I am stuck. – Romain Legrand May 23 '17 at 21:58

This is simply a proof of concept and needs to be refined, but it works as required and illustrates the relevant methods.

You should create a new, empty directory, and then save the following three files under that one directort, with the names specified below.

## Contents of file CommonMacros.tex:

\newcommand*{\SolutionBook}{SoluBook.pdf}
\newcommand*{\ExerciseBook}{MainBook.pdf}

\newcommand*{\exercisename}[1]{Exercise~#1.}
\newcommand*{\exercisecategory}{exercise}
\newcommand*{\solutionname}[1]{Solution of Exercise~#1.}
\newcommand*{\solutioncategory}{solution}

% In the following two commands, #1 stands for the part that is to be made
% clickable:
\newcommand*{\seeexercisephrase}[1]{See exercise #1.}
\newcommand*{\seesolutionphrase}[1]{See solution #1.}
\newcommand*{\herename}{here}

\newcommand*{\exesolubreak}{%
}

\newcounter{exercise}[section]
\renewcommand*{\theexercise}{\thesection.\arabic{exercise}}
\newenvironment*{exercise}{%
\exesolubreak
\noindent
\refstepcounter{exercise}%
\hyperdef{\exercisecategory}{\theHexercise}%
{\textbf{\exercisename{\theexercise}}}%
\ignorespaces
}{%
\par
\nobreak\smallskip
\begingroup
\footnotesize
\noindent
\seesolutionphrase{%
\hyperref{\SolutionBook}{\solutioncategory}{\theHexercise}%
{\herename}%
}%
\par
\endgroup
}

% In a real-life example, the two books would "communicate" through some sort
% of auxiliary file; here, we assume that each solution correspond to the
% exercise with the same number.
\newcounter{solution}[section]
\renewcommand*{\thesolution}{\thesection.\arabic{solution}}
\newenvironment*{solution}{%
\exesolubreak
\noindent
\refstepcounter{solution}%
\hyperdef{\solutioncategory}{\theHsolution}%
{\textbf{\solutionname{\thesolution}}}%
\ignorespaces
}{%
\par
\nobreak\smallskip
\begingroup
\footnotesize
\noindent
\seeexercisephrase{%
\hyperref{\ExerciseBook}{\exercisecategory}{\theHsolution}%
{\herename}%
}%
\par
\endgroup
}


## Contents of file MainBook.tex:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly
% declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{hyperref}
\usepackage{lipsum}

\input{CommonMacros}

\begin{document}

\section{Some argument}

\lipsum[1-2]

\begin{exercise}
\lipsum*[\arabic{exercise}]
\end{exercise}

\lipsum[3]

\begin{exercise}
\lipsum*[\arabic{exercise}]
\end{exercise}

\lipsum[4-6]

\begin{exercise}
\lipsum*[\arabic{exercise}]
\end{exercise}

\lipsum[7-8]

\begin{exercise}
\lipsum*[\arabic{exercise}]
\end{exercise}

\lipsum[9]

\begin{exercise}
\lipsum*[\arabic{exercise}]
\end{exercise}

\section{Another argument}

\lipsum[10]

\begin{exercise}
\lipsum*[1\arabic{exercise}]
\end{exercise}

\lipsum[11-12]

\begin{exercise}
\lipsum*[1\arabic{exercise}]
\end{exercise}

\lipsum[13]

\begin{exercise}
\lipsum*[1\arabic{exercise}]
\end{exercise}

\lipsum[14]

\begin{exercise}
\lipsum*[1\arabic{exercise}]
\end{exercise}

\lipsum[15-16]

\begin{exercise}
\lipsum*[1\arabic{exercise}]
\end{exercise}

\end{document}


## Contents of file SoluBook.tex:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly
% declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{hyperref}
\usepackage{lipsum}

\input{CommonMacros}

\begin{document}

\section{Some argument}

\begin{solution}
\lipsum*[\arabic{solution}]
\end{solution}

\begin{solution}
\lipsum*[\arabic{solution}]
\end{solution}

\begin{solution}
\lipsum*[\arabic{solution}]
\end{solution}

\begin{solution}
\lipsum*[\arabic{solution}]
\end{solution}

\begin{solution}
\lipsum*[\arabic{solution}]
\end{solution}

\section{Another argument}

\begin{solution}
\lipsum*[1\arabic{solution}]
\end{solution}

\begin{solution}
\lipsum*[1\arabic{solution}]
\end{solution}

\begin{solution}
\lipsum*[1\arabic{solution}]
\end{solution}

\begin{solution}
\lipsum*[1\arabic{solution}]
\end{solution}

\begin{solution}
\lipsum*[1\arabic{solution}]
\end{solution}

\end{document}


Now, compile the files MainBook.tex and SoluBook.tex. Ignore warnings about duplicate destinations, they are absolutely normal since more than one exercise/solution winds up on the same page. The resulting two files MainBook.pdf and SoluBook.pdf should exhibit the requested behavior.

• Thank you so much. I tried it and it works wonder. So yes, I need now to understand all the details of the code to be able to adapt this to my specific needs. But this is indeed what I needed. – Romain Legrand May 23 '17 at 23:15
• @RomainLegrand: I’ll try to explain the detail when I find the time to—I’m afraid that means on the next weekend! :-) Meanwhile, perhaps someone else will have already posted a more detailed answer… – GuM May 23 '17 at 23:25
• @GustavoMezzeti No problem, I should take a look on my own anyway to try to understand all this. No wonder I didn't make it however: I couldn't have come with something that complex on my own. I just wish Latex can be simpler, sometimes. – Romain Legrand May 24 '17 at 0:57

So I started looking again for simple solutions for my problem. @GustavoMezzeti proposed a nice solution, but it seems really complicated. On top of that, I am not sure the kind of hyperlinks he creates make it possible not only to hyperlink, but also to hypereference (refer automatically to the potential label associated to the linked element).

After more research, I think I found a solution. It mostly builds on the elements of this post, which suggests the use of \hyperdef (to label) + \hyperref (to link). The idea is that of double-labelling. One label is an actual \label that generates the numbering for later references and has to be inserted into a labaleable environment (for this reason I use the theorem environments for the exercises and solutions). The second "label" is actually a \hyperdef: it is mostly a phantom element used as an anchor for the hyperlink.

The solution has pros and cons.

pros: it works; it is simple; it is flexible as it allows not only for linking to the other document, but also for referencing to external labels, as shown by the equation example at the end of the code.

cons: heavy: everything has to be double-labelled.

So here is the code:

doc1:

\documentclass[11pt,a4paper,fleqn]{book}
\usepackage{xr-hyper}
\externaldocument[doc2-]{doc2}
\usepackage{hyperref}
\usepackage[nopar]{lipsum}
\usepackage{amsthm}
\newtheoremstyle{standardstyle}{}{}{}{}{\bfseries}{:}{ }{}
\theoremstyle{standardstyle}
\newtheorem{exrc}{exercise}[chapter]

\begin{document}

\chapter{}
\label{chap1}

\lipsum[1]

\hyperdef{categoryname}{hrex1}{}
\begin{exrc} \label{ex1} This is an exercise.\\ \lipsum[2] \\
Solution is \hyperref{doc2}{categoryname}{hrsolex1}{\textbf{here}}.
\end{exrc}

\lipsum[3]

\hyperdef{categoryname}{hrex2}{}
\begin{exrc} \label{ex2} This is a second exercise.\\ \lipsum[4] \\
Solution is \hyperref{doc2}{categoryname}{hrsolex2}{\textbf{here}}.
\end{exrc}

\lipsum[5] \\

Finally, this is a link to equation \hyperref{doc2}{categoryname}{hreq1}{\ref*{doc2-eq1}} in doc2.

\end{document}


and for doc2:

\documentclass[11pt,a4paper,fleqn]{book}
\usepackage{xr-hyper}
\externaldocument[doc1-]{doc1}
\usepackage{hyperref}
\usepackage[nopar]{lipsum}
\usepackage{amsthm}
\newtheoremstyle{standardstyle}{}{}{}{}{\bfseries}{:}{ }{}
\theoremstyle{standardstyle}
\newtheorem{sol}{solution}[chapter]

\begin{document}

\chapter{}
\label{chap1}

\lipsum[1]

\hyperdef{categoryname}{hrsolex1}{}
\begin{sol} \label{solex1} This is the solution to the first exercise. \\ \lipsum[2] \\
Exercise is \hyperref{doc1}{categoryname}{hrex1}{\textbf{here}}.
\end{sol}

\lipsum[3]

\hyperdef{categoryname}{hrsolex2}{}
\begin{sol} \label{solex2} This is the solution to the second exercise. \\ \lipsum[4] \\
Exercise is \hyperref{doc1}{categoryname}{hrex2}{\textbf{here}}.
\end{sol}

\lipsum[5] \\

Now this is an equation:
\hyperdef{categoryname}{hreq1}{}
$$2+2 = 4 \label{eq1}$$

\end{document}