GeneralIncluding tex files

LaTeX specific issues not fitting into one of the other forums of this category.
User avatar
T3.
Posts: 208
Joined: Fri Mar 14, 2008 12:58 pm

Including tex files

Post by T3. »

As a sort of experiment I cooked up a few macros that implement the user interface and logic I would expect from something like the import package. Code with an example usage follows:

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}
Cheers,

Tomek

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Post Reply