Before I even start I will give a disclaimer: Today’s post is way more technical than those you have found thus far in this blog.
Almost anybody who is somewhat involved into science, especially mathematics, is very likely to have ran across the Latex typesetting system. It is an extremely powerful and versatile markup language that allows for scientific (and non-scientific) formatting of documents.
I have been familiar to Latex for about five years already, however, since starting my job at Dallas I confronted the biggest Latex project I have ever been in. The program I am has 12 mathematics classes going on every day. Each day every class has at least one hand out, which in general is typed using Latex. However, these handouts are to be stored and later made into booklet containing all of them. The question is, what is the best way of doing this?
Well, the first solution to come into mind is to make a new latex file and copy/paste manually the code of each of the individual handout and then compile. This may work for small projects, however, considering that the program I am in lasts for 3 weeks, by the end of it we would have 180 handouts at the very least. That would take a while to do, not to mention the possibility of mistakes and the tediousness.
Another alternative is to use the command \input. It should be as simple as making a ‘master file’ where the command \input is called to get the code of all the individual handout files. However this solution is still not perfect. Whenever we call \input, the whole code of the handouts is read by the compiler and as soon as it reads the header commands of the handouts (\documentclass{article}, begin{document}, etc) it will start complaining. So this solution, as is, would require to manually delete the header declarations of each handout. For my project this is still not feasible.
You may think “there should be a better way” – I thought this myself – and fortunately there is. First let’s make clear what we want: We want to make Latex files that compile normally separately, however we also want to be able to use a ‘master file’ where we can call all of them and also compile without problems. After asking my friends and googling for around this past 2 days I have finally solved it. Here it goes:
We will have three main files: header.tex, footer.tex and master.tex. I will explain all of what I will write – just follow on me for now:
In header.tex we will write the following:
\providecommand{setflag}{\newif \ifwhole \wholefalse}
\setflag
\ifwhole\else
% Here you write your declarations, usepackages, etc
\begin{document}
\fi
The footer.tex file should say
\ifwhole\else
\end{document}
\fi
The master.tex file should have
\input{head.tex}
\renewcommand{\setflag}{\newif \ifwhole \wholetrue}
\input{handout-1.tex} //put as many inserts here as needed
\renewcommand{\setflag}{\newif\ifwhole \wholefals}
\setflag
\input{footer.tex}
And finally, individual handout should look like
\input{header.tex}
% handout text here
\input{footer.tex}
So let’s explain what’s going on. First of all look a the header.tex file. We have \providecommand which checks if a command already exists, if it does, it leaves it alone otherwise it defines it as specified. . Here we provide the command ‘\setflag’ and defines a new boolean variable by calling ‘\newif \ifwhole’. This boolean (‘whole’) can be set to true or false by calling \wholetrue or \whofalse respectively. In this case we set it to true. Conditional statements can be written using this boolean using the command
\iswhole
%Type here what should be executed if 'whole' is true
\else
%Type here what should be executed otherwise
\fi
With this we can explain how individual handouts compile. The compiler reads \input{header.tex} goes to the file and reads \providecommand{setflag}. Since \setflag does not exists, it creates it. Afterwards \setflag is run (as defined by \providecommand) creating a boolean ‘whole’ and set to false. After we get a conditional statement, but since ‘whole’ is false, we get to the else and read all of the declarations, definitions and \begin{document}. Similarly, the compiler reads the footer.tex in the individual handouts.
Now, when we run the master.tex file the \input{header.tex} works the same as before. However, just after reading header.tex the compiler encounter \renewcommand which overrides the definition of \setflag. From this point on \setflag is set to define the boolean variable ‘whole’ and set it to true. Afterwards, when the input{handout.tex} are read the first thing the compiler sees is \provide{seflag}. The catch is that now \setflag is defined, so \providecommand doesn’t do anything to it. Next, \setflag is run returning a boolean ‘whole’ with value true. Hence this time the conditional statement goes through the first clause (instead of the else). This way all of the declarations, headers, begin{document} and all of those things that the compiler would complain if it were to read them in the middle of the document, are magically skipped. The handouts as I defined them here will compile both individually and inside the master.tex file with no hiccups. That’s how it works.
Sorry, again, to most of you for the technicality of this post. However this might prove extremely useful for the more scientifically oriented out there. If you have a big project this may help to save tons of time. Hope you liked it and let me know if you have any troubles implementing it. As for me, I need to go sleep as I have to wake up early tomorrow to continue my work. See you ’round.
Filed under: Computers, Technology, boolean variable, command, compiler, Latex, \input, \newcommand, \providecommand
Recent Comments