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.

Question

I have many documents that I want to be merged inside one document. It could be for example a kind of year recap, with many different sources that use the same preambule like lectures, labs, assignments, tests, ...

I tried to use the subfiles package but I can't compile the main document because of relative path inside sub documents:

File `image.png' not found

Is it possible to include graphics inside a subfile using a relative path ?

Minimal working example

I want a file sub.tex to be included inside a bigger document main.tex:

main.tex
sub/sub.tex
sub/image.png

The main document is not in the same directory as the sub document and is as follow:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{subfiles}

\begin{document}
  Main file
  \subfile{sub/sub.tex}
\end{document}

My sub file sub.tex includes an image image.png with a relative path:

\documentclass[../main.tex]{subfiles}

\begin{document}
  Sub file
  \includegraphics{image.png}
\end{document}

What I tried

Following a comment below, I have defined two commands Subfile and IncludeGraphics in main.tex and I use them in both files:

\usepackage{xstring}

\def\SubfilePath{}
\def\Subfile#1{%
    \def\SubfilePath{\StrBefore{#1}{/}/}%
    \subfile{#1}}

\newcommand\IncludeGraphics[2][]{%
  \includegraphics[#1]{\SubfilePath #2}}

Unfortunately, I'm not able to compile main.tex whereas everything is fine with sub.tex:

Use of \Gin@ii doesn't match its definition
share|improve this question
1  
See my answer which is related to your question: tex.stackexchange.com/a/64205/19356 –  Oh my ghost Apr 19 at 7:19
    
Interesting, but I still need to be able to compile my subfiles separately. Using you solution, I would therefore need to define a new SubFile command that calls subfile but stores the path somewhere for use inside a new IncludeGraphics command. –  remjg Apr 19 at 8:04

1 Answer 1

up vote 6 down vote accepted

Package graphics provides \graphicspath that allows to specify further directories for the search path of image files:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{subfiles}

\begin{document}
  Main file

  \graphicspath{{sub/}}
  \subfile{sub/sub.tex}
\end{document}

Or \graphcispath can be called automatically for each \subfile:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{subfiles}

\makeatletter
\let\org@subfile\subfile
\renewcommand*{\subfile}[1]{%
  \filename@parse{#1}% LaTeX's file name parser
  \expandafter
  \graphicspath\expandafter{\expandafter{\filename@area}}%
  \org@subfile{#1}%
}
\makeatother

\begin{document}
  Main file
  \subfile{sub/sub.tex}
\end{document}
share|improve this answer
    
I forgot to mention among the hundred documents I'm considering merging in a few ones, I use images with the same name and sometimes different names for my image folders (I will, one day, standardize everything). So it is a valid answer, but I think it will not work for my use case. –  remjg Apr 19 at 9:31
1  
@remjg: (a) \graphicspath can be changed for every document setting a different path. (b) \graphicspath can be limited by a group: \begingroup\graphicspath{{sub/}}\subfile{sub/sub.tex}\endgroup. –  Heiko Oberdiek Apr 19 at 10:01

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.