1# Producing LaTeX documents
2
3On this page, you will learn how to use
4BSD Owl Scripts (`bsdowl`) to:
5
6- produce simple documents and publish them on the file system;
7- produce the bibliography of your LaTeX document with BIBTeX;
8- produce figures for your document with METAPOST;
9- deal with documents having parts that need to be automatically generated.
10- deal with documents whose source spans across several directories.
11
12
13# Foreword: working with TeX or LaTeX
14
15There is multiple TeX formats in use, plain TeX and LaTeX are
16examples of such formats.  The LaTeX format enjoys a wide community of
17users, so the module `latex.doc.mk` is used in examples. However most of
18the following applies to the module `tex.doc.mk` as
19well. Some paragraphs in the sequel document mechanisms specific to
20`latex.doc.mk`, they are explicitly identified as such.
21
22
23# Simple use
24
25The preparation of a simple document with LaTeX itself is very
26easy, thus the use of `bsdowl` may at a first glance look like a useless
27complication instead of a simplification. It provides some
28useful features, however.
29
30
31## The first time
32
33Assume the file `script.tex` holds your
34manuscript. Put it in a directory dedicated to your document, and
35create a `Makefile` file (note the leading capital) with the following
36content:
37
38	DOC = script.tex
39	.include "latex.doc.mk"
40
41Your document's directory now contains the `paper.tex` file and the
42Makefile described above.  Point your shell's working directory to
43your document's directory, and issue the `make` command:
44
45	$ make
46	make build
47	===> Multipass job for script.dvi (aux)
48	latex script.tex
49	This is pdfeTeX, Version 3.141592-1.21a-2.2 (Web2C 7.5.4)
50	entering extended mode
51	(./script.tex
52	LaTeX2e <2003/12/01>
53	...
54
55If your manuscript has no error, you end up with the following
56_object files_ in your working directory:
57
58	$ ls
59	Makefile    script.log
60	script.aux  script.tex
61	script.dvi  script.toc
62
63Once you are done with the objects, you can clean the directory
64with the `make clean` mantra:
65
66	$ make clean
67	rm -f  script.dvi script.log script.aux script.toc
68
69Cleaning the directory is an optional step, but it prevents your
70storage and your archive media to end up filled with unused data, that
71can be quickly recreated on demand. While DVI files are usually very
72small, a few hundred kilobytes, the PS or PDF objects are often much
73larger.
74
75
76## Installing
77
78Before you clean up your working directory with the _make clean_
79mantra, you may wish to store the document you created in some
80adequate place of the local file system.  This step is called
81_installation_ of your document, because it is analogous to the
82installation of a program you freshly compiled.  You require the
83installation of your document with the `make install` command, but you
84must first tell make which place is actually adequate. This is done by
85assigning the _DOCUMENTDIR_ variable with the path to the directory
86you want your files to be copied to, as displayed by the following
87`Makefile`:
88
89	DOCUMENTDIR = ${HOME}/doc/report
90	DOC = script.tex
91	.include "latex.doc.mk"
92
93You can then proceed with the `make install` command:
94
95	$ make install
96	install -d /home/michi/doc/report
97	install -o michi -g michi -m 440 script.dvi /home/michi/doc/report
98
99In comparison with the manual approach for storing the object in a
100safe place with the _cp_ command, you save retyping the target
101directory name the next time you update your document.  But delegating
102this easy step to the `Makefile` has other benefits:
103
104- It eases the organisation of your document sources library.
105- It scales to a large number of documents, see _Working with a large
106  number of documents_ below.
107- In draft-mode, a time stamp identifying the compilation time is
108  automatically added to the file name, see below.
109
110
111## Selecting an output format
112
113The TeX tool chain is capable of producing electronic documents in
114several formats. Commonly used formats are DVI, PostScript (PS) and
115PDF. The _TEXDEVICE_ variable governs the format of documents produced
116with the help of latex.doc.mk. Its value is usually _dvi_, so
117that latex.doc.mk will produce a DVI file from your source.  Other
118possible values are _ps_ or _pdf_.  If you configured a
119PostScript printer _TEXPRINTER_ with the texconfig program, you
120also can use _ps.TEXPRINTER_ as a value in TEXDEVICE, it will
121instruct dvips to use the settings for TEXPRINTER when translating
122your DVI file to PostScript. It is also possible to list several
123output formats in TEXDEVICE, like _dvi pdf ps.TEXPRINTER1 ps.TEXPRINTER2_.
124
125
126## Drafts and multipass jobs
127
128Some formats or macros need your manuscript to be processed several
129times by TeX or LaTeX before you obtain the final version of your
130document.  The `latex.tex.mk` module enforces multipass treatment of
131your manuscript, because LaTeX needs this to produce correct cross
132references created with _label_ and _ref_ commands within your
133document.  The `doc.tex.mk` module will not do multiple treatment of
134your manuscript unless you set the variable _MULTIPASS_ to a list of
135names, each element giving its name to a pass.  The choice of these
136names does not have a real importance, as they are only displayed to
137the user.  It is even possible to specify the same name several times!
138
139In the early stages of existence of a document, updates are likely to
140be frequent and it is thus desirable to avoid the lengthy multiple
141passes processing.  `bsdowl` has a draft mode for this. To enable the draft
142mode, set make's variable _DRAFT_ to yes, this is done by adding an
143appropriate statement in the `Makefile`, as shown by the following
144example:
145
146	DRAFT?= yes
147	DOC = script.tex
148	.include "latex.doc.mk"
149
150The statement _DRAFT?=_ is a special form of an assignment
151statement, that allows you to type _make DRAFT=no_ at the shell
152prompt if you want to re-enable multipass processing of your job for
153one time, without editing the `Makefile`.
154
155When you have finished polishing your manuscript, you can remove the
156`DRAFT` assignment from the Makefile, your paper is then ready for a
157last _make_ run producing the final version of your document.  If you
158are satisfied with it, you can _install_ it.
159
160When working on a document, it might be useful to keep copies of the
161objects you produced at some point of your work.  For instance,
162picture yourself sending a copy of your work to a friend.  Your friend
163will read your paper with attention and send you back his comments,
164but in the meanwhile, you kept on improving your document.  When you
165receive the comments of your friend, you will compare them to the
166document you sent him.  It is therefore useful to keep a copy of it.
167The best way to do this is probably to use a
168[RCS](http://en.wikipedia.org/wiki/Revision_Control_System), a
169software keeping track of the various revisions of a file. If you do
170not use such a system and want to try one, you might be interested in
171GIT, especially if you are relying on EMails to organise your
172collaborative work.
173
174When the variable _DRAFT_ is set to _yes_ and the variable
175_TEXDRAFTSTAMP_ is not initialised, it receives the value
176`-${TEXDRAFTTIMESTAMP}` where _TEXDRAFTTIMESTAMP_ expands to the date
177when `make` is being run.  When the variable _TEXDRAFTSTAMP_ is
178initialised and not empty, its value is appended to the name of all
179document installed by `latex.doc.mk` or `latex.doc.mk`. If you do not
180want the name of the files to be modified, you just need to initialise
181_TEXDRAFTSTAMP_ with an empty value.
182
183
184## Split document
185
186If you are working on a complex document, you certainly have split
187your sources into several files. Usually one file per chapter, or per
188section, plus a main file containing the preamble and many
189_input_ statements to instruct LaTeX to read all of the files
190representing the document's contents.
191
192Assume that your document is split into a main file `galley.tex`
193and two other files `part1.tex` and `part2.tex`. Your `galley.tex`
194certainly looks like this:
195
196	\documentclass{article}
197	\begin{document}
198	\input{part1}
199	\input{part2}
200	\end{document}
201
202A suitable `Makefile` is then:
203
204	DOC = galley.tex
205	SRCS = part1.tex
206	SRCS+= part2.tex
207	.include "latex.doc.mk"
208
209
210# More advanced features
211
212## Figures with METAPOST
213
214Modules `latex.doc.mk` and `tex.doc.mk` comes with a nice support for
215[METAPOST](http://cm.bell-labs.com/who/hobby/MetaPost.html).
216This is something worth noting, since METAPOST is
217often the right tool to be used for drawing figures appearing in TeX
218documents, but support for it is missing in many GUI editors for
219LaTeX.
220
221These modules assume that your METAPOST source contains the lines
222
223	prologues := 3;
224	outputtemplate := "%j-%c.mps";
225
226The first declaration parametrises the inclusion of fonts in the
227output, while the second reconfigures the names used for output.
228
229Assume you prepared illustrations for your article with METAPOST,
230and split your illustrations into two files `illustr1.mp` and
231`illustr2.mp`. To let `latex.doc.mk` handle the production of your
232figures, add _FIGS_ statements to your `Makefile`:
233
234	DOC = galley.tex
235	SRCS = part1.tex
236	SRCS+= part2.tex
237	FIGS = illustr1.mp
238	FIGS+= illustr2.mp
239	.include "latex.doc.mk"
240
241Then type in _make_ at the shell prompt. The `latex.doc.mk` will
242then figure out how many illustrations are present in each file, and
243produce the image files required by your _TEXDEVICE._  For instance, if
244your _TEXDEVICE_ is _dvi,_ and `illustr1.mp` contains two figures
245introduced by `beginfig(1)` and `beginfig(2)`, you end up with four files
246
247
248	illustr1-1.mps
249	illustr1-2.mps
250	illustr1-1.eps
251	illustr1-2.eps
252
253The first two files are intermediary files in PostScript format,
254and the last ones are Encapulated PostScript suited for inclusion
255in your document.
256
257Using the _graphicx_ package, inclusion is as easy as it should be:
258
259	\includegraphics{illustr1-1}%
260
261_Discovering METAPOST._
262It seems that many people do not know about METAPOST.  If it is true
263for you but are interested in discovering it, the first good news is
264that this program is included by many (if not all) TeX distributions,
265hence it is probably already available on your system.
266
267The second good news is that you can easily find plenty of information
268and examples of its use on the WWW. For instance, the
269[TeX users group](http://www.tug.org/metapost.html)
270has a page on its website devoted to this tool. The list you will find
271there is pretty long, so let me add that I especially like the
272[introduction written by André Heck](http://staff.science.uva.nl/~heck/Courses/mptut.pdf), it might also be a good starting point for you.
273
274
275## Bibliography
276
277`bsdowl` supports the preparation of bibliographies with BibTeX. First,
278you must be sure that the program TeX will find the bibliographic
279databases you enumerated in your document with _bibliography_
280statements.  It is customary to gather bibliographic databases in some
281directory, for instance _${HOME}/share/bib_.  To let bibtex
282find these files, it is enough to add _${HOME}/share/bib_ to the content
283of the variable _BIBINPUTS_.  If your bibliographic databases are
284scattered among several directories, you just need to let each of them
285appear in the value of the variable _BIBINPUTS_:
286
287	DOC = galley.tex
288	SRCS = part1.tex
289	SRCS+= part2.tex
290	BIBINPUTS = ${HOME}/share/bib
291	BIBINPUTS+= ../morebib
292	.include "latex.doc.mk"
293
294Note that the _make clean_ mantra will leave intact the BBL file
295produced by bibtex.  This is because you sometimes need to send this
296file to your publisher rather than an unprocessed bibtex database.
297Hence the _make clean_ or _make distclean_ will leave you document's
298directory in the state you want to have it when you want to
299redistribute it. To get rid of the BBL file as well, you need to use
300the more powerful mantra _make realclean_.
301
302
303## Index
304
305At that time, there is no specific provision in `bsdowl` to
306help the preparation of LaTeX documents featuring an index.  You are
307on your own.  But this is definitely a feature that we want to have,
308so if you have interest, please come in touch.
309
310
311## Several documents in the same directory
312
313While it is often a good idea to reserve a directory for each of
314your documents, you might have some reasons to keep several documents
315in the same directory.  You have your reasons and they are probably
316good ones, so `bsdowl` will do its best to help you.
317
318We assume that you have two documents whose sources are living in the
319same directory, let's say an article and an abridged version of this
320article. These files share a macro file `macro.tex`, but are otherwise
321rather independent from LaTeX's point of view. The text of the article
322is split across two files, `section1.tex` and `section2.tex`. The
323summary has just one text file `summary.tex`. The Makefile used looks
324like this:
325
326	DOC = article.tex
327	DOC+= summary.tex
328
329	SRCS = macros.tex
330
331	SRCS.article.tex = section1.tex
332	SRCS.article.tex+= section2.tex
333
334	.include "latex.doc.mk"
335
336
337## Automatically generating a part of a document
338
339Assume you are working on a document containing a table whose
340content is likely to change several times and will need to be
341updated.  Such a table could be a budget:  when information on the
342situation evolves, so does the budget.  It can be quite tedious to
343type in a table in LaTeX, and updating it might even be trickier.  In
344such a situation, it is probably a good idea to write a program
345reading the raw data of your table and writing a LaTeX table
346displaying your data and everything you want to compute from it.  Such
347a program is usually very easy to write, because you only need to deal
348with text files all of the time.
349
350So you have gathered the raw data of your table in the file
351`table.raw` and written a small program gentable that will write for you
352a LaTeX table on its standard output.  In your manuscript, you use the
353name _table_ to refer to the file containing the generated
354table. Here is your Makefile:
355
356
357	DOC = galley.tex
358
359	table.tex: gentable table.raw
360		./gentable < table.raw > table.tex
361
362	REALCLEANFILES+= table.tex
363
364	.include "latex.doc.mk"
365
366The example assume that the _gentable_ utility in the
367recipe above is a filter, hence input and output are defined with the
368help of shell redirections.  Other utilities may have other ways to
369define their input and output, a described by their respective manuals.
370
371If you send your files to someone else, he will maybe not want to
372run your program gentable, so it is better to list `table.tex` in
373_REALCLEANFILES_ than in _CLEANFILES_: you can clean your directory
374with `make clean` archive its contents and send the archive to someone
375else without handling the generated `table.tex` in a special way.
376
377Of course, you can compute some text or a METAPOST picture, or
378pretty-print a piece of code, or whatever, instead of generating a
379table!
380
381
382## Source files spanning across several directories
383
384Some workflows may prescribe that your source files are not located
385in a single directory, but disseminated across the file system.
386
387A reason for doing this is that your organisation uses a custom
388document class for its letters, where some picture appears.  You do
389not want to copy the picture file in each of the folders hosting your
390letters, nor do you want to have a symbolic link to the picture in
391each of your directories because the file is irrelevant to your work:
392you just want to not even know anything about it. The solution to this
393problem is to rely on the _TEXINPUTS_ variable, its content is a list
394of directories searched by TeX for its input files.
395
396Another reason motivating the dissmenination of source files in
397several directories is the preparation of a large document such as a
398book.  If the files for each chapter are in separated directories, it
399is easy to process an isolated chapter with LaTeX during the
400preparation of the manuscript.  TeX must find all these files when it
401processes the main file including all the chapters, which is achieved
402by setting _TEXINPUTS_ to an appropriate value, as explained in the
403sequel.
404
405You can set the _TEXINPUTS_ variable in your environment or in your
406`Makefile`, or even write a custom `Makefile` template including this
407setting.  The role of this variable for TeX is pretty similar
408to the role of the _PATH_ environment variable in your shell.
409
410Assume that the picture visually impersonating your organisation
411is in the _${HOME}/share/texmf/tex/organisation/visual.eps_, in
412order to let TeX look for files in the folder containing the picture,
413you write a _TEXINPUTS_ statement in your _Makefile_, like this:
414
415	DOC = galley.tex
416	TEXINPUTS = ${HOME}/share/texmf/organisation
417	.include "latex.doc.mk"
418
419If you run _make_ in the folder containing this `Makefile`, you
420will see something like this in your terminal:
421
422	$ make
423	make build
424	===> Multipass job for galley.dvi (aux)
425	env TEXINPUTS=".:${HOME}/share/texmf/organization:" latex galley.tex
426	This is pdfeTeX, Version 3.141592-1.21a-2.2 (Web2C 7.5.4)
427	...
428
429Take a look at the _TEXINPUTS_ assignment in the _env_
430command.  Its difference with respect to the declaration in the
431`Makefile` above means that TeX will also look for files
432in the current directory (this is what the initial dot stands for) and
433all standard TeX locations (this is what the final colon stands for).
434
435If you want to have absolute control on the value of _TEXINPUTS_, you
436must add an assignment _USE\_STRICT\_TEXINPUTS=yes_ in your
437`Makefile`.  If it sees this statement, `bsdowl` will refrain from adding the
438initial dot and the final colon to your `TEXINPUTS` declaration.
439
440The supporting macros for METAPOST also understand _TEXINPUTS_ and
441_USE\_STRICT\_TEXINPUTS_.  There is an analogous variable _MPINPUTS_
442governing the look up of METAPOST input files, it is accompanied with a
443_USE\_STRICT\_MPINPUTS_ flag.  If you want to have your TeX program and
444your METAPOST program to be run with different values for _TEXINPUTS_,
445you can pass the correct value to METAPOST through the _MPTEXINPUTS_
446variable, this variable is also accompanied by a
447_USE\_STRICT\_MPTEXINPUTS_ flag.
448