In large project such as dissertations or even books, it is important and necessary to keep the content organized. One way is to split a document, by chapter for example, and then load the parts using input or include. The catchfilebetweentags package offers a more flexible way to organize content. It let’s you store chunks of code in another file. The code chunks don’t have to be ordered, as tags are used for identification. This way, you can store equations, tables or figures apart from text, making content more manageable.
In the following example, I will use two tex-files, a main document, main.tex
, and a document containing all figures, figures.tex
.
Main document
\documentclass[11pt]{article} \usepackage{catchfilebetweentags} % load the package \newcommand{\loadFigure}[1]{ % define command to load figures \ExecuteMetaData[figures.tex]{#1} % call the package macro to load chunk from file } \begin{document} \loadFigure{fig:01} % load figure with tags id: fig:01 \loadFigure{fig:02} % load figure with tags id: fig:02 \end{document}
First the package is loaded. Next, we define a new macro for figures that takes one argument, the tag ID, being the tag name. Inside the macro, we call another macro, ExecuteMetaData
defined by the package, to load a code chunk from file: figures.tex. The package will then find the tag with the same name as the ID and replace \loadFigure{tagID}
by the actual chunk.
Code chunks file
The code chunks can be exported in any order to a text file (in my case figures.tex). In fact, any text file will do. Important is that the chunks are delimited by tags with same ID used to load the chunk in the main file. Also notice the % symbol in from of the tags and the * inside the opening tag.
%<*tagID> ...LaTeX code... %</tagID>
To complete the example above, I created figures.tex, containing two dummy figures and placed the file into the same directory as the main document, main.tex. However, you could use any directory, just make sure to change the filename by adding the path in the main file, i.e. \ExecuteMetaData[path/to/file]{#1}
.
%Figure file: figures.tex %<*fig:02> \begin{figure}[ht] \begin{center} \rule{0.4\textwidth}{0.3\textwidth} \caption{My second figure} \label{fig:01} \end{center} \end{figure} %</fig:02> %<*fig:01> \begin{figure}[ht] \begin{center} \rule{0.3\textwidth}{0.4\textwidth} \caption{My first figure.} \label{fig:01} \end{center} \end{figure} %</fig:01>
The \rule{}{}
command used in the example is just a figure placeholder and will produce a black box of the size given.
Many thanks to Giacomo Drago who pointed the package out to me and from where I copied the macro code.
Miguel V.
That’s a very useful feature! Even for smaller documents with lots of figures and specially with long tables. Thanks a lot.
MV
Ole J. Forsberg, Ph.D.
In my dissertation, I split the document into chapters. In my next book-length project, I can definitely see where this may be helpful. Especially when I can generate the figures/tables file from R.
Thank you again!
Antti Mäki
Thanks a lot! This was very useful for me when I tried to apply the catchfilebetweentags package. Much appreciated for your effort