2

I have the following directory structure

Thesis/
      main.tex
      Chapter1/
              section1.tex
              subsection1.1.tex
              figure1.png
              figure2.png
      Chapter2/
              section1.tex

main.tex:

             ...
             \input{Chapter1/section1.tex}
             \input{Chapter2/section1.tex}
             ...

Chapter1/section1.tex:

             ...
             \input{subsection1.1.tex}
             ...
             \includegraphics{figure1.png}
             ...

Using relative path in file "Chapter1/section1.tex" to include "subsection1.1.tex" throws an error ! LaTeX Error: File subsection1.1.tex not found. Is there a way to include file "subsection1.1.tex" in "Chapter1/section1.tex" using relative path?

TBourne
  • 23
  • 2
    It's probably just me being old, but what does one gain by splitting each chapter into potentially many sub files? – daleif May 12 '21 at 15:04
  • I am writing thesis for first time in Latex. My plan was to type everything and include files using relative path once I completed typing. (I assumed relative path would work out of the box). – TBourne May 12 '21 at 15:36
  • 1
    That is not my question, why split the chapter up into smaller files, we see a lot of people doing this and I don't get this is useful compared to having the chapter in one file each. All the books I've prepared over the years used a single file per chapter. As mentioned it might be a generational thing – daleif May 12 '21 at 15:40
  • I thought splitting a chapter into smaller files would later help me correct errors in my Thesis. – TBourne May 12 '21 at 15:50
  • path to document files should definitely be either absolute or explicitly relative (start with a /, ./ or ../). Replace \input{foo} by \input{./foo} and the same for \includegraphics as well. This speeds up file opening and more importantly avoids actual and future name conflicts with the system wide TeX files. – Jérôme LAURENS Nov 18 '21 at 12:47

4 Answers4

5

You can use the import package. It defines two commands:

\import{〈full-path〉}{〈file〉
\subimport{〈path-extension〉}{〈file〉}

When you use \import or \subimport, the included file can reference files relative to the path of that included file.

In your case:

...
\subimport{Chapter1}{section1.tex}
\subimport{Chapter2}{section1.tex}
...

and then in Chapter1/section1.tex:

...
\input{subsection1.1.tex}
...
\includegraphics{figure1.png}
...

should work if files Chapter1/subsection1.1.tex and Chapter1/figure1.png exist.

1

I have a not quite ready for widespread distribution package that I've created for this purpose for my own use. I make no guarantees of its suitability. Put this in betterinclude.sty and then use \include instead of \input in your main.tex file.

Updated the code to support include hooks in newest LaTeX.

\ProvidesPackage{betterinclude}

\def@finddir#1#2#3/#4//#5.{ \def@tempa{#4}% \ifx@tempa@empty % There is no directory name provided \gdef#1{} \gdef#2{#3} \else % There is a directory name provided \gdef#1{#3/} \gdef#2{#4} \fi }

% Redefine @include to find any directory portion of the input name \def@include#1 {% @finddir\bi@@dir\bi@@name#1///.% \clearpage \if@filesw \immediate\write@mainaux{\string@input{\bi@@name.aux}}% \fi @tempswatrue \if@partsw @tempswafalse \edef\reserved@b{#1}% @for\reserved@a:=@partlist\do {\ifx\reserved@a\reserved@b@tempswatrue\fi}% \fi \if@tempswa \let@auxout@partaux \if@filesw \immediate\openout@partaux \bi@@name.aux \immediate\write@partaux{\relax}% \fi \let\bi@iinput@iinput \edef@iinput##1{\noexpand\bi@iinput{\bi@@dir##1}} \let\bi@vi\verbatiminput \edef\verbatiminput##1{\noexpand\bi@vi{\bi@@dir##1}} \graphicspath{{\bi@@dir}} \bi@setinputs @filehook@set@CurrentFile \UseHook{include/before}% \UseHook{include/before/#1}% @input@{#1.tex}% \UseHook{include/end/#1}% \UseHook{include/end}% \let@iinput\bi@iinput \let\verbatiminput\bi@vi \bi@restoreinputs \clearpage @writeckpt{#1}% \if@filesw \immediate\closeout@partaux \fi \else \deadcycles\z@ @nameuse{cp@#1}% \fi \let@auxout@mainaux}

\let\bi@setinputs\relax \let\bi@restoreinputs\relax

(this is really old code—I wrote the original in the ‘90s and made an update to handle \includegraphics somewhat more recently).

Don Hosek
  • 14,078
  • Note that this disables all of the include hooks in newer LaTeX – Phelype Oleinik May 12 '21 at 14:46
  • Like I said—really old code. I'll update the answer to indicate as such. – Don Hosek May 12 '21 at 14:47
  • And the code is updated to support hooks. – Don Hosek May 12 '21 at 15:21
  • @DonHosek Thanks for help. But I'm getting error: \@include ...\bi@vi {\bi@@dir ##1}} \graphicspath {{\bi@@dir }} \bi@setinput.... I am new to latex. I couldn't solve this on my own. – TBourne May 12 '21 at 15:29
  • Ah, this code assumes that either the graphicx or graphics package is loaded. – Don Hosek May 12 '21 at 15:56
  • Imho it is a bad idea to propagate redefinitions of internal commands like @include. The fact that you had to adapt your answer shows the intrinsic problem of such an approach: the code is unstable and can break if the internal change. – Ulrike Fischer May 13 '21 at 07:19
  • @UlrikeFischer which is the big reason why I have so many caveats around it & haven't distributed it publicly. On the flip side, given that this is code that dates back to the early 90s and that it only had a minor issue as of last October argues for its stability. On the third hand, I've been running into a great deal of pain thanks to bidi.sty doing similar. I ended up switching an example in my book from Hebrew to Greek just to avoid that issue (thankfully Greek required a similar action). – Don Hosek May 13 '21 at 15:27
1

Try this (requires LaTeX 2020-10-01 at least):

\documentclass{article}

\ExplSyntaxOn \tl_new:N \l__bourne_tmpa_tl \cs_new_protected:Npn \bourne_path_push: { \seq_put_left:NV \l_file_search_path_seq \CurrentFilePath \AddToHookNext {file/after/\CurrentFile} { \bourne_path_pop: } } \cs_new_protected:Npn \bourne_path_pop: { \seq_pop_left:NN \l_file_search_path_seq \l__bourne_tmpa_tl } \NewDocumentCommand \topinput { } { \AddToHookNext {file/before} { \bourne_path_push: } \input } \ExplSyntaxOff

\usepackage{graphicx} \begin{document}

\topinput{Chapter1/section1.tex}

\topinput{Chapter2/section1.tex}

\end{document}

The \topinput command adds the path given to the \l_file_search_path_seq, so that while in that file, that path is searched, so \topinput{Chapter1/section1.tex} will add Chapter1 to the search path. As soon as the file ends, the path is removed so that duplicate files are not searched (however it is not recommended to have files with the same name!).

0

You could organize your files so that everything in subsection 1 is in the subsection1.1.tex file and each section and subsection has its own file. This would have the advantage of making the inputs clear in the main.tex file. Change the graphics path before each.

main.tex:

\begin{document}
\graphicspath{{Chapter1/}}
\input{Chapter1/section1.tex}
\input{Chapter1/subsection1.1.tex}

\graphicspath{{Chapter2/}}
\input{Chapter2/section2.tex}
\input{Chapter2/subsection2.1.tex}

\end{document}

I would recommend against naming files "section1.tex". Let LaTeX do the numbering. Name your files something meaningful, like "introduction.tex," and use labels to reference sections. Then if section 2 becomes section 3, you don't have to rename files.

directory structure:

Thesis/
    thesis_main.tex
    introduction/
        introduction.tex
        intro_sec1_filename.tex
        figures/
            figure1_name.png
            figure2_name.png
    theory/
        theory.tex
        theory_sec1_filename.tex
        figures/

thesis_main.tex:

\documentclass[12pt]{book}

% List of packages \usepackage{graphicx}

\begin{document}

% INTRODUCTION
\graphicspath{{introduction/figures/}}
\input{introduction/introduction.tex}
\input{introduction/intro_sec1_filename.tex}

% THEORY
\graphicspath{{theory/figures/}}
\input{theory/theory}
\input{theory/theory_sec1_filename.tex}

\end{document}

introduction.tex

% Introduction
\chapter{Introduction}
This is content in the first part of the introduction

\includegraphics{figure1.png}

intro_sec1_filename.tex

% Title of introduction section
\section{Title of introduction section}
This includes all of introduction section 1 content, including subsections

\subsection{Title of introduction section subsection} This is the first subsection content

\includegraphics{figure2.png}

theory.tex

% Theory
\chapter{Theory}
This is content in the first part of the theory section

theory_sec1_filename.tex

% Title of theory section
\section{Title of theory section}
This includes all theory section 1 content, including subsections

figure1.png

enter image description here

figure2.png

enter image description here

  • Thanks for suggestion. But if you have "picture" that you want in "section1" then you might want "picture" file included in "section1.tex". – TBourne May 12 '21 at 15:12
  • That's why you set the graphicspath. Is figure1.png in section 1 or subsection 1.1? Put all section 1 figures in the same directory – mcarchmiller May 12 '21 at 15:14
  • I would put all Chapter 1 figures in the same directory – mcarchmiller May 12 '21 at 15:20
  • figure1.png is used in "section1". Suppose you have lots of figures in lots of different directories? if you could use relative path to include files/figures then you wouldn't have to care about "declaring path". – TBourne May 12 '21 at 15:23