This is a follow up on the question
How to import separate standalone latex documents and assemble into one document
I am using the package standalone
to assemble many child latex files from different folders in the tree into one parent file at the top level while keeping each child in its own folder and be able to build each child document as separate latex document.
Using the above package, with the package import
is working well. But there is one big problem when using links.
When using href
in a child document to link to a file relative to it (say in the same folder), then when building the parent document, the links do not work since the path in the link is now not valid. i.e. hyperlinks do not get adjusted for the level the parent document is in.
Here is a picture of the set up with 2 small files as a complete example
main.tex (now the link below is wrong when main document is build)
|
|
A/----+ child.tex (this contains href to foo.pdf)
foo.pdf
main.tex is
\documentclass[titlepage]{article}%
\usepackage{standalone}
\usepackage{import}
\usepackage{hyperref}
\begin{document}
\subimport*{A/}{child}%
\end{document}
and child.tex is
\documentclass{article}
\usepackage{hyperref}
\begin{document}
\href{foo.pdf}{my pdf file}
\end{document}
One solution is to use absolute path in the links. But this is a problem since I can move the tree itself to different location and the absolute path can change. Another option is to use a path in the child file that is already adjusted taking into account the parent level. So in this example I would write in the child.tex this
\href{A/foo.pdf}{my pdf file}
instead of
\href{foo.pdf}{my pdf file}
But now the links when I build the child document only will be wrong.
Also this means if I change the internals of the tree layout, (say move the folder A one level down) the links will all have to be fixed manually.
I'd like to find if there is a better and automated way to handle this?
The import package handles the relative path for the import fine, but I need something to handle the href
links as well used in the document.
Or may be I should just give up on the idea of importing separate standalone latex documents and assembling then into one document because if the href links are broken, then this whole thing will not work. I have many links that I use that are relative to the document level. May be Latex is not right for such a task.
Update 1
I found a big problem with the currfile
package but I have not yet looked to see if there is an option to handle this case. Given this tree, same as above, except I just renamed the files:
index.tex
|
|
A/----+ index.tex (this contains href to foo.pdf)
foo.pdf
Notice that I used the same file name (index.tex) which is very common for the above level and the lower level.
Now currfiledir
does not work. It gets confused by the same name and does not handle the link correctly. When I change the name to index2.tex
for the child, then it worked. i.e. the same exact files as I have shown above, just renamed them to index.tex
in both fodlers caused this problem !
I used index.tex
all over the place. In each folder, I have index.tex
. I suppose I can rename each file to be unique. But I hope there is an option in this package to handle this.
Here are the files for completion based on the above tree:
index.tex (in the top level)
\documentclass[titlepage]{article}%
\usepackage{standalone}
\usepackage{import}
\usepackage{hyperref}
\usepackage{currfile}
\begin{document}
\subimport*{A/}{index} %renaming this from index1 will make it work!
\end{document}
index.tex (in the lower level, the child) %rename to index1.tex to make it work!
\documentclass{article}
\usepackage{hyperref}
\usepackage{currfile}
\begin{document}
\href{\currfiledir foo.pdf}{my pdf file}
\end{document}