# Is it possible to input tex files from subfolders inside subfiles?

I want to automate the process of inputting tex files from subfolders. Previously I managed to automate the inclusion of images from "images" subfolder within the subfolder of each subfile. I would like to do the same for tex files. Please have a look at: Is it possible to use includegraphics with relative path with subfolder, inside subfiles?

Now I want to add a modifier, so that

%subfolder1/subfile1.tex
\input{fig1.tex}


would add subfolder1/fig1.tex in the file, while

%subfolder2/subfile1.tex
\input{fig1.tex}


would add subfolder2/fig1.tex to my file.

• Why not just define e.g. \subinput{} to use in an analogous way? – cfr Nov 18 '15 at 2:27
• I am currently doing \edef\CurrentFileDir{\currfiledir} and \renewcommand{\myimagedir}{\CurrentFileDir images/}. \input{\myimagedir fig_matlab_sinemodulation} seems to work. Is there any easier way to do it? – sajid Nov 18 '15 at 5:14
• What I had in mind was a new input command so you could say e.g. \subinput{fig_matlab_sinemodulation} - a wrapper around \input{}, in other words. – cfr Nov 18 '15 at 14:28
• can you give any examples related to that? I am really a newbee in LaTeX. Thank you so much for the help. – sajid Nov 18 '15 at 15:23
• I've posted an example. Are you sure subfiles doesn't provide something like this already? I've never used it, but this seems like the kind of thing people might often want to do and is a bit different from the graphics case (where mostly, setting the search path in the preamble or configuring different inputs for TeX will work). I understand use of \graphicspath{} is not considered good practice, by the way. That said, bad practice works well for me since I use it all the time. – cfr Nov 18 '15 at 23:27

## 1 Answer

You might create a new command \subinput{} for handling this. Basically, this is a wrapper around \input{} which prefixes the relevant path.

File hierarchy for example is as follows:

<main filename>.tex
sub/
sub.tex
sub2.tex
img/
tigerx.eps


sub/sub.tex:

\documentclass[../<main filename>.tex]{subfiles}

\begin{document}
Sub file.

Here's a tiger:

\includegraphics[width=\linewidth]{tigerx}

\subinput{sub2}
\end{document}


sub/sub2.tex:

% sub/sub2.tex
That is some tiger!
\endinput


<main filename>.tex:

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

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

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


Output: