Latex: include with path, not just filename

  • Context: LaTeX 
  • Thread starter Thread starter gsal
  • Start date Start date
  • Tags Tags
    Latex Path
Click For Summary
SUMMARY

The discussion focuses on using LaTeX's \include command to manage multiple sections and figures in a structured document. The user aims to compile a main document (main.tex) that includes two separate sections (SectionStress.tex and SectionPressure.tex) located in different subdirectories. The challenge arises when trying to include figures from these subdirectories, as the paths must be correctly referenced for both the main document and the individual section files. A proposed solution involves defining a custom command to manage graphic paths, allowing for flexibility in referencing figures without conflicts.

PREREQUISITES
  • Familiarity with LaTeX document structure and commands
  • Understanding of the \include and \includeonly commands in LaTeX
  • Knowledge of the graphicx package for figure inclusion
  • Basic file organization principles in LaTeX projects
NEXT STEPS
  • Explore the use of the \graphicspath command in LaTeX for managing figure paths
  • Learn about the standalone package in LaTeX for compiling individual sections
  • Investigate LaTeX custom commands for dynamic path management
  • Review best practices for organizing large LaTeX documents with multiple figures and sections
USEFUL FOR

LaTeX users, document authors, and researchers who need to manage complex documents with multiple sections and figures efficiently.

gsal
Messages
1,065
Reaction score
54
latex: "include" with path, not just filename

Say, a co-worker and I are writing two separate sections for the same document; each will get large and have many figures. Somewhere, I read that 'include' is more convenient than 'input' for large documents and for actual (sub)documents than just figures.

So, I am looking to structure my documents like:
Code:
|-main.tex
|-subdirectory1
|   SectionStress.tex
|-subdirectory2
    fig1.png
    fig2.png
    fig3.png
    SectionPressure.tex
What I am hoping for is for each Section*.tex file to be full, legal, self-contained, file that can be compiled in its own right and generate a *.pdf file. Additionally, for "main.tex" to be able to include the 2 sections, compile and generate a full *.pdf document...

In main.tex, I am using
Code:
%usepackage{standalone}
\includeonly{subdirectory/SectinoStress,subdirectory2/SectionPressure}
.
.
.
\include{subdirectory/SectinoStress}
\include{subdirectory2/SectionPressure}

The above works as far as text goes (references, section numbering, etc.), but not when using figures ( \includegraphics{} ). More clearly, the section files work when the includegrahpics commands do not include any path preceeding the figure file names (since they are in the same directory); but the "main.tex" complains about not finding the figures. On the flip side, if I include path to the figure file inside the section files so that they can be found by "main.tex", then, the section files themselves fail to find the figures.

...is there fix to this?

I would appreciate any hints.
 
Physics news on Phys.org


A cheap and cheerful way is do something like this at the start of the document
Code:
\def\mygraphics#1{\includegraphics{\mypath/#1}}
and then in the first section
Code:
\def\mypath{subdirectory1}
\mygraphics{fig1.png}
\mygraphics{fig2.png}
and in the next section
Code:
\def\mypath{subdirectory2}
\mygraphics{fig1.png}
\mygraphics{fig2.png}
Note you can have different files with the same names in each subdirectory if you want.

But if you want to use optional parameters in [ ] with the \includegraphics command, that could be "too simple." For some more ideas, try
http://www.tex.ac.uk/cgi-bin/texfaq2html?label=graphicspath
http://www.tex.ac.uk/cgi-bin/texfaq2html?label=docotherdir
 


AlephZero:

Thanks for the quick response; but I am afraid I don't quite understand where I am supposed to write what.

is it clear to you that I actually have 3 different tex files? A "main.tex" one that \includes other two and that I want to be able to compile the "main" one as well as the other two as full, legal self-contained latex files? (not just contents without preamble, etc).

In regards to your post:
Is the first piece of code meant to be included in the "main.tex" (see my previous post),
the second one into "SectionStress.tex", and
the third one into "SectionPressure.tex" ?

Here is a short version of my main.tex:
Code:
\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fullpage}
\usepackage{graphicx}
\usepackage{float}
%\floatstyle{boxed} 
%\restylefloat{figure}

\usepackage{standalone}
\includeonly{./sub1/SectionStress,./sub2/SectionPressure}

\begin{document}

\include{./sub1/SectionStress}
\include{./sub2/SectionPressure}

\end{document}

I want to be able to compile sub2/SectionPressure and be able to include the graphics in such directory.
AND
I want to be able to compile main.tex and make sure that the graphics being imported from within ./sub2/SectionPressure come thru, as well.

I am reviewing the first link you provided...it looks like that is one way to achieve what I want with the limitation that figure file names need to be unique across sub directories.

Another trick is to play around with file system...ugly but it worked...just put a folder for figures in each sub directory and point to them from "main" level with links of the same name. ; -)

As I continue to review first link and test it, I would appreciate if you could confirm my suspicion or further clarify your proposal.

Thanks,

gsal
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
6K
  • · Replies 8 ·
Replies
8
Views
13K
  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
43K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 9 ·
Replies
9
Views
4K