[Tex/LaTex] Is it possible to use includegraphics with relative path with subfolder, inside subfiles

graphicssubfilesxparse

My question is a followup question of this:
Is it possible to use includegraphics with relative path inside subfiles?

I have my images in sub/image folder. I want automatically add 'sub/images' path for graphics, for each of the subfiles. Can any one help me edit the code so that it can do that?

This given code adds path to the subfiles, so that the image should be in the same path as the subfile. I want to keep the image in a subfolder.

\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}

Best Answer

EDITED in light of sajid's comments. But the additional \expandafter is not needed.

You can just append the sub-directory in the usual way.

  \graphicspath\expandafter{\expandafter{\filename@area img/}}%

This works even if the image needs conversion. (I copied the standard tiger image from the distribution, which needs to be converted for pdfTeX.)

\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 img/}}%
  \org@subfile{#1}%
}
\makeatother

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

Hopefully this is the expected output:

sub-tiger

Related Question