I'm writing a book with lots of chapters using a command \input{filename.tex} for each chapter in the book book.tex. How to make it not to have book.tex and all filename.tex in the same directory? Is it possible to have directory book.tex with subdirectories filename.tex?
- 6,403
3 Answers
\newcommand\subdir{mysubdir}
...
\input{\subdir/file}
Then you have to change only one name if the subdir changes. However, using \include instead of \input makes more sense if you are using chapters.
-
1more on when to use
\inputand when to use\includecan be found here. – greyshade Aug 09 '14 at 19:46 -
I don't know how to do it according to your advice. Let say I have a directory named Work. There is book.tex, file1.tex, file2.tex and file3.tex files in the Work. I would like to have book.tex which will include file1.tex, file2.tex and file3.tex I put in preambule \newcommand\subdir{book} and then \include{\subdir/file1} \include{\subdir/file2} \include{\subdir/file3} It's not working in this way. – Laura Aug 09 '14 at 20:24
-
@Jane -- If all
.texfiles are in the same directory, then you only need to use\include{file1},\include{file2}and so on. Herbert's\subdircommand suggestion was for a sub-directory under (in this case) theWorkdirectory. – jon Aug 09 '14 at 20:41 -
Jon, I don't want to have it like this, because I have so many outputs in one directory and it's so confusing. I want directory Work which will include Book.tex and the Book.tex will include file1.tex, file2.tex and file3.tex, but I cannot find the way how to make it like this. – Laura Aug 09 '14 at 20:45
-
@Jane -- Then you need to create sub-directories in the Work directory; e.g., in
Work, you have the following directories:chap01, which includesch01.tex, andchap02, which includesch02.tex. ThenBook.texwould have\include{chap01/ch01}and\include{chap02/ch02}. Obviously under normal circumstances a single command like\subdircannot point to multiple directories.... – jon Aug 09 '14 at 21:19 -
Just a suggestion: I've found it much more natural to declare a command along the lines of
\newcommand\includechapter[1]{\input{subdir/#1}}. – Sean Allred Aug 09 '14 at 21:56
Assuming a directory struture like the following (where directory Work contains the following sub-directories: Chap01 and Chap02, which each have a .tex file of their own):
Work/
|-Book.tex
|-mystyle.sty
|-Chap01/
|-chapter01.tex
|-Chap02/
|-chapter02.tex
Then the main file would have this basic structure:
% Contents of Book.tex
\documentclass{book}
\usepackage{mystyle}
\begin{document}
\include{Chap01/chapter01}% NOTE: these 'sub-files' must not include things like
\include{Chap02/chapter02}% \usepackage, \begin{document}, or \end{document}
% You could also use \input; for the relative advantages, see http://tex.stackexchange.com/q/246/8528
% \input{Chap01/chapter01}
% \input{Chap02/chapter02}
\end{document}
And the included files would look something like this:
% Contents of chapter01.tex
\chapter{The First Chapter}
This is the first chapter.
- 22,325
-
There's no need to use
\include; also\inputwill accept a subdirectory prefix. – egreg Aug 09 '14 at 21:41 -
1@egreg -- Yes indeed (will edit). For chapters, I recommend
\includemainly because of the possibility to use\includeonly. – jon Aug 09 '14 at 22:18 -
Well, it seems very complicated for me. I tried to do it like this but it writes mystyle.sty is not defined. – Laura Aug 09 '14 at 22:44
-
@Jane -- Just omit that line:
\usepackage{mystyle}was invented simply to point out that you would load "personal" packages normally in comparison with.texfiles located in sub-directories. – jon Aug 09 '14 at 22:50 -
It works now fine.Thans a lot.I am just wondering if it could be proved.I'm using some pictures in almost each chapter.I thought it would be enough just keep the pictures in each chapter directory together with chapter.tex, but I was supposed to put all pictures in work directory together with book.tex. So I still have with book. tex lots of pictures there. – Laura Aug 09 '14 at 23:13
-
@Jane -- I'm sorry but that is not clear: what do you mean by 'supposed'? According to whom? You can (1) keep all your pictures in the main 'Work' directory; (2) put all pictures in a separate directory (called, e.g., 'graphics'); or (3) put pictures corresponding to each chapter in the appropriate chapter directory. Note that
graphicx.styhas a useful command called\graphicspath-- please refer to the documentation for use with different operating systems. – jon Aug 11 '14 at 03:11
You need a folder Work/Book with the chapter .tex files in there in order for this to work.
Alternatively, you can give a path for each \include, relative to the path of yout main .tex file, directly in the curly braces so you don't need the \subdir definition. Just remember to use / instead of \ to concatenate folders in the path.
- 19
\input{subdirectory/filename}? – Paul Gessler Aug 09 '14 at 19:41