Code: Select all
\begin{filecontents}{locinput.sty}
% Quick and dirty replacement for the 'import' package
%
% One command is provided, '\locinput', to be used
% instead of '\input' command
%
% Usage:
%
% \locinput{}{file}
% equivalent to \input{<working directory>file}
% working directory is initially empty
%
% \locinput{subdir/}{file}
% appends 'subdir/' to working directory,
% executes \input{<working directory>file}
% and restores previous working directory
%
% \locinput[dir/]{subdir/}{file}
% sets working directory to 'dir/subdir/',
% executes \input{<working directory>file}
% and restores previous working directory
%
% TODO:
% Provide optional hooking into '\input' command
\def\EmptyMacro{}
\def\CurrentPath{}
\def\AbsPath{}
% simple stack (modified from "TeX by Topic")
\newtoks\DirStack
\def\PushCurrentPath{\edef\act{\noexpand\DirStack={\CurrentPath<StackSep>\the\DirStack}}\act}
\def\SplitStack#1<StackSep>#2<EndOfStack>{\def\CurrentPath{#1}\DirStack={#2}}
\def\PopCurrentPath{\edef\act{\noexpand\SplitStack\the\DirStack<EndOfStack>}\act}
\def\NewPath#1#2#3{%
\def\AbsPath{#1}%
\ifx\AbsPath\EmptyMacro%
\edef\CurrentPath{\CurrentPath#2}%
\else%
\edef\CurrentPath{#1#2}%
\fi%
\edef\CurrentFile{\CurrentPath#3}%
}
\newcommand{\locinput}[3][]{%
\PushCurrentPath%
\NewPath{#1}{#2}{#3}
\input{\CurrentFile}%
\PopCurrentPath%
}
\end{filecontents}
\begin{filecontents}{dir1/foo.tex}
\immediate\write16{This is file 'foo.tex'}
\locinput{dir2/}{bar}
\end{filecontents}
\begin{filecontents}{dir1/dir2/bar.tex}
\immediate\write16{This is file 'bar.tex'}
\end{filecontents}
\documentclass{minimal}
\usepackage{locinput}
\listfiles
\begin{document}
main file
\immediate\write16{This is file '\jobname.tex'}
\locinput{dir1/}{foo}
\locinput[dir1/]{}{foo}
\locinput[dir1/]{dir2/}{bar}
\end{document}
Tomek