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.

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}
share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

If for some reason you prefer not to use the currfile package (eg you only have a version strictly earlier than v0.7b and are using the import package with files in different directories with the same name), here's a possible alternative solution, relying just on the import package.

The idea is to use the \import@path macro when it is available, i.e. because the import package is loaded, and that should keep track of the path for us. When it is not available, it is because the sub-document is being compiled directly (not through import) and so we should use a blank path prefix in that case. We wrap everything up in a user macro \currentimportpath.

  1. main.tex:

    \documentclass[titlepage]{article}%
    \usepackage{standalone}
    \usepackage{import}
    \usepackage{hyperref}
    \makeatletter
    \providecommand{\currentimportpath}{\import@path}
    \makeatother
    \begin{document}
    \subimport*{A/}{main}%
    \end{document}
    
  2. A/main.tex:

    \documentclass{article}
    \usepackage{hyperref}
    \providecommand{\currentimportpath}{}% for when this file is compiled directly
    \begin{document}
    \href{\currentimportpath foo.pdf}{my pdf file}
    \end{document}
    
share|improve this answer
    
excellent! It worked. I found that I can just use \providecommand{\currentimportpath}{\import@path} in both the parent and the child just the same and will work even when I build the child on its own. This is good, since in my tree, it is many levels, and a file can be both a child and a parent at the same time, so I need to be able to build at any level without changing things. Thanks again for your help. I also sent an email about the problem with currfiledir to the author of the package as you suggested. –  Nasser Jan 28 '13 at 10:15
    
Note that this issue with currfile is created by the changes done by import. Filename with the same name in different directories are supported in general. –  Martin Scharrer Jan 29 '13 at 19:43
1  
@cyberSingularity: I'm working on a fix for currfile, but still have to find the time to analyse the issue and the import source code. –  Martin Scharrer Jan 29 '13 at 21:13
    
@cyberSingularity: I just uploaded v0.7b of currfile which supports \subinput* and similar macros from import. It turned out that they need a different file path search order to work. I'm in email contact with the OP and informed him already. –  Martin Scharrer Feb 1 '13 at 22:32
    
@MartinScharrer: Fantastic. Thanks for that. I have edited my posts slightly to refer to the new version, and deleted previous comments. –  cyberSingularity Feb 2 '13 at 0:19
add comment

How about using the currfile package (already loaded by standalone):

  1. main.tex:

    (just add \usepackage{currfile} to make it explicit, and in case you ever remove standalone)

    \documentclass[titlepage]{article}%
    \usepackage{standalone}
    \usepackage{import}
    \usepackage{hyperref}
    \usepackage{currfile}
    \begin{document}
    \subimport*{A/}{child}%
    \end{document}
    
  2. A/child.tex

    add \usepackage{currfile} and prefix \currfiledir to each \href:

    \documentclass{article}
    \usepackage{hyperref}
    \usepackage{currfile}
    \begin{document}
    \href{\currfiledir foo.pdf}{my pdf file}
    \end{document}
    

Note the space after \currfiledir, and lack of slash:

  • When compiling child.tex directly, \currfiledir is empty (so you don't want a slash).
  • When compiling child.tex through main.tex,\currfiledir is A/ (so the slash is already there).

You could of course write your own \myhref to automatically prefix \currfiledir, eg

\newcommand*{\myhref}[1]{\href{\currfiledir #1}}

and then invoke as

\myhref{foo.pdf}{my pdf file}

(Note that if you are using the import package with files in subdirectories having the same name as the main file, then you need currfile version 0.7b or newer for this solution to work.)

share|improve this answer
    
Thank you so much! I did not know about this package. I just tried it on the above example, and it does worked very well. –  Nasser Jan 27 '13 at 15:52
    
Well, I have some bad news. I found a big problem in this package when I started to use it on my actual tree. Easier to explain with diagram. I'll add an update above. I need to also research this more to see if there is an option to avoid this. –  Nasser Jan 27 '13 at 16:48
add comment

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.