8

Preamble

I am writing a large document and I would like to keep it well structured not only in terms of source code (I am aware of commands like \input and include, as well as the standalone package), but also in terms of folder(s) containing the actual source files.

Following How to work with large projects, I have structured my project as follows:

/my_project
    |-main.tex
    |-... % other directories
    |-/contents 
        |-chap01/ % more than one chapter
            |-chap01.tex
            |/img
                |-img01.tex % more than one image

The key point is that each chapter folder has an inner folder containing the images to be typeset in that chapter. Now, the problem is that the images (usually TikZ figures and PGFplots to be compiled) placed in the lowest level of directories and included through standalone, throw an error and the document doesn't get compiled.

The actual question

So my question is: If the proposed structure is sensible, how can I make it work? And if you think it is not the best way to organize and manage a project, could you advise (or point to references about) another way?

How to reproduce the problem

main.tex

\documentclass{scrbook}
\usepackage{standalone}
\standaloneconfig{mode=buildnew}
\usepackage{graphicx}

\begin{document}
\include{./contents/chap01/chap01}
\end{document}

chap01.tex

\chapter{Test A}
\includestandalone{./img/img01}

img01.tex

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \fill [red] (0,0) circle (2cm);
\end{tikzpicture}
\end{document}

And here is a small script to help reduce the hassle of the setup for GNU/Linux users. (Place the files main.tex, chap01.tex and img01.tex in a folder and run it.)

#!/bin/bash

# Build folders
mkdir ./contents ./contents/chap01 ./contents/chap01/img

# Place files in their respective directory
cp chap01.tex ./contents/chap01
cp img01.tex ./contents/chap01/img

# Remove files from root directory
rm ./chap01.tex ./img01.tex

# Compile main.tex
pdflatex -shell-escape main.tex

Related questions (with comments)

  • 2
    Side note: I have found that a flat structure is better. 26 different image folders seem to be tidy and clean, but it is rubbish if you can't remember where you put something. – Johannes_B Feb 11 '15 at 17:35
  • @Johannes_B: Thanks for your feedback. I had thought about placing all images in a folder, say /my_project/img, but at the moment I feel that a tree structure is closer to my needs. Nonetheless, I have tried that and standalone still throws the same error. – Pier Paolo Feb 11 '15 at 17:43
  • 1
    If you are in a chapter, your $pwd is still a level higher, so you should \input{someChapter/img/imagefile} – Johannes_B Feb 11 '15 at 17:45
  • @Johannes_B: I created a folder my_project/img (at the same level of my_project/contents) and have included the image of chap01.tex with \includestandalone{../../img/img01}. – Pier Paolo Feb 11 '15 at 17:54
  • 1
    s/\includestandalone{./img/img01}/\includestandalone{./contents/chap01/img/img01}/ Your $pwd is ./; to find the imagefile, you have to cd into contents, then cd into chap01, then cd into the img folder. – Johannes_B Feb 11 '15 at 18:25
7

I am not sure if i can answer the question as posted in a precise way, but here we go.

From the LaTeX point of view, the current working directory is ./ as a unix guy would say. In order to find the image, you have to go down into the contents directory (./contents/), then further down into chapter01 (./contents/chap01/) and finally change into the image folder (./contents/chap01/img/). This is a bit tediuos to write everytime, but there are thing like $TEXINPUTS or \graphicspath. This would be the pure LaTeX side.

Look at it from a different angle, changing directories in a shell can be a lot of fun, or pure hell. If you are working with a file manager, you are clicking your way through.

In my point of view (see, this is opinion based) the best practice is to have just unique filenames, not only for figures, but for all files; tex-files, image-files, pdf-files.

I was once collaborating with a human being, who saved all journal articles as is, meaning with a strange number. The person had quite a system, you had to go at least 4 levels down to see a pdf file (remember, all file names were random numbers). If you were looking for the article written by Jones in 2011, it could take you 15 minutes to find it, as you cannot know where exactly the person had put it.

What do i want to say with that?
If you take care in naming the filenames in a precise and unique way, you will not loose oversight of your files and your supervisor/studentes/colleuges won't go crazy.

You can have a database of a thousand pdf files, looking for jones2011 is not difficult. coffeemakerInit.jpg is different from coffeemakerExploded.jpg but without opening the image file, you know what it contains. Do the same with tex-files and especially Labels. The minute you may decide what a good label is is a minute saved later in searching for the label (or the image), or saving 15 minutes in searching for a journal article ;-)

Keep it simple ;-)

| improve this answer | |
  • +1 for the names and flat directory idea. One ls shoes – jon Feb 12 '15 at 2:08
  • (On tablet...) shows everything. I'd use simple prefixes: image-ch01-coffeemaker.png and so on. A deeply-nested directory structure is not the same as a well organized one... – jon Feb 12 '15 at 2:11
5

The import package allow insert documents of another directory, allowing to that files to find its own \inputs, , \includegraphics, etc. For the example, I make only stand alone files with docmute package:

img01.tex in directory contents/chap01/img/:

\documentclass[margin=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \fill [red] (0,0) circle (1cm);
\end{tikzpicture}
\end{document}

MWE

chap01.tex in in directory contents/chap01/:

\documentclass[12pt]{article}
\usepackage{docmute}
\usepackage{import}
\usepackage{tikz}
\begin{document}
This is the subdocument\par\bigskip 
\subimport{img/}{img01}
\end{document}

MWE

main.tex

\documentclass{scrbook}
\usepackage{docmute}
\usepackage{import}
\usepackage{tikz}
\begin{document}
This is in the main document\par\bigskip   
\subimport{contents/chap01/}{chap01}
\end{document}

MWE

| improve this answer | |
  • Cool, I wasn't aware of this package. Thanks for pointing it out. – Pier Paolo Feb 12 '15 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.