2

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}
  • Are you looking for an xparse solution? – Sean Allred Nov 16 '15 at 21:06
  • I am looking for modification of the given code. I am not very efficient with expandafter commands: \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 – sajid Nov 16 '15 at 22:42
1

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

  • Excellent! It seemed rather too simple... ;). – cfr Nov 17 '15 at 3:20
  • Wait, I think I had a mistake. I copied my image file in the root. Actually, now that I removed my copy image file, the code didn't work any more. – sajid Nov 17 '15 at 4:00
  • Did you add your image in the img folder? – sajid Nov 17 '15 at 4:02
  • @sajid I thought so. I'll check. But not right now. Too lacking in consciousness. Maybe somebody else will find something meanwhile. ... – cfr Nov 17 '15 at 4:54
  • 1
    I solved the problem. \filename@area spits out a slash '/', and after img there must be a slash. Editing your code to \filename@area \expandafter images/ worked. Please edit your answer and I can mark it as correct. – sajid Nov 17 '15 at 18:52
2

Unless you really need to do this in your TeX file, consider adding your subdirectory to the TEXINPUTS environmental variable before building (e.g., Definition of the TEXINPUTS variable). It works OK, unless, of course, there are file name conflicts.

  • would TEXINPUT work if I don't have unique image files? And adding a directory of Image seems to be against abstraction. If I copy the Tex file to another computer, I would run into trouble – sajid Nov 16 '15 at 22:41
  • Technically, a TeX file by itself is never fully portable. The other computer may lack some style files, for instance. In my projects I use Makefiles to set the environment before build, which makes it more portable. If you have conflicting file names, you will be always in trouble if you try to hide the prefixes. – Frigo Nov 16 '15 at 22:46
  • This is not really answer. It should maybe be a comment, but it doesn't answer the question at all. – cfr Nov 17 '15 at 2:16
1

This is how I solved it, (with cfr's help). Basically add the name of the folder after the filename@area (which spits out the name of the directory of the subfile with a /)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.