# Exporting Graphics for LaTeX

## Introduction to LaTeX

LaTeX is one of the most widespread text systems in the scientific community. Its origin are mainly in those parts of the science, where math formulas play a major role. But LaTeX can also typeset things like chemical formulas, program code, or even music. From small papers to large books, LaTeX seems to be ready for everything. Even slides for overhead projections, or PDF presentations can be done and are done in LaTeX.

LaTeX creates beautifully formatted text on par with any other layout program. Page layout, indices, table of content, references, footnotes and image insertion are on a professional level. Math looks better than anywhere else. This unmatched quality of output is the main reason to use LaTeX, despite the difficulties that have to be overcome when learning the system. Here is a sample screenshot.

The root of LaTeX is the TeX system programmed by D. Knuth. This system is more a text compiler, than a word processor. The user has to learn and know a text formatting language, in which he formats the text. The code is then compiled into a device independent text layout, stored in a special file format. Using these compiled files, rendering programs (so-called previewers) can either present the file on the computer screen, create print versions, e.g. in Postscript or PDF, or print directly on a printer. Newer versions can create PDF directly.

Clearly, this kind of text generation looks more like text programming than like the usual layouting of texts. To ease the writer, there are special editors to help with the language, and also editors that show a preview of the finished texts. There are even editors that let the user edit the finished layout in WYSIWYG style. Most users however, prefer to write the code directly, and use the traditional compiler, preview and print mechanism.

LaTeX is based on TeX, using the mighty macro facilities of TeX. The system was created by L. Lamport and is now the quasi standard for TeX. Using LaTeX makes it far easier to create texts than using plain TeX. I will demonstrate the use of C.a.R. images in LaTeX only.

## Images in LaTeX

Images were always a weak spot of LaTeX. The main reason is the device independent output format, that cannot hold images. Instead, commands for the preview program are included into the file, different for each possible previewer. LaTeX, however, makes it easier to use images. There are still some problems with different target outputs. We will discuss output in PDF for the Acrobat Reader, and output for the rendering program yap, which is often used and available for Windows and Unix.

We cannot explain details about the LaTeX language here. There are good books for this, and also online tutorials. For more information have a look at this page.

### PNG for PDF

The easiest output is PNG (portable network graphics) for PDF (to be used with the Acrobat Reader), and it does also yield the highest possible quality for exporting C.a.R. graphics. I recommend it for general use. Moreover, with some precaution the same code can be used for yap.

There are two ways to generate PDF files from LaTeX sources. One is to use the pdflatex compiler, and the other is to go over DVI, postscript and then PDF. For simplicity, I prefer to use pdflatex directly. This description is based on pdflatex.

The first concept one must be aware of is scaling. To get good looking graphics, it is not enough to scale the screen proportionally to the target space on paper. This would yield much too narrow or broad lines, unreadable small or rediculously large text labels, and ugly representations of points. Instead, each of these items must normally be scaled properly to look good in the target surrounding. The PNG output with its export dialog does just this.

In the dialog, you set the designated print size in cm, and sizes in mm for your fonts, lines and points. Since PNG is a bitmap format, you need also specify the resolution, usually 200 to 300 dots per inch (dpi). C.a.R. will then compute the necessary bitmap size, and save the file in lossless, compressed PNG format.

Since the text sizes are changed, it is a good idea to preview the output. C.a.R. tries to show the preview on the screen as good as it can. It may be necessary to change the construction layout a bit to accommodate to the new sizes. In C.a.R., you can switch the preview on or off at any time.

To appreciate the quality of the output, have a look at test.pdf. Of course, Acrobat Reader must be installed for this. I present a screen print, from a scaled down version in Acrobat Reader.

Note, that the fonts are larger than usual in C.a.R. Also the line widths are wider, especially on the grid. This is all necessary to embed this picture properly into a LaTeX document. Simply scaling the screen up would not look so good.

How was it achieved? The image was exported 1417×1145 pixels wide. This rather huge file is necessary for 300 dpi resolution. The resolution is written to the PNG file too. The program pdflatex is able to read it and determine the true output size for this image. So in the following LaTeX source code, it is not necessary to give a specific print size for the image.

We are using the LaTeX figure environment to place the graphics into the text. Inside this environment, includegraphics does the job. This command is defined in the graphicx package. So we have to use this package.

\documentclass[a4paper,11pt]{article}

\usepackage{graphicx}

\parskip=5pt

\begin{document}

\section*{First Section}

In this section, we just demonstrate how to include a figure into
\LaTeX. Here is the figure.

\begin{figure}[h]
\centering
\includegraphics{First.png}
\caption{Kepler's Theorem}
\end{figure}

The image is scaled automatically.

\end{document}


Since PDF output looks so good and works so easy, it is the recommended way for me. The only alternative is the yap output in the following section.

### PNG for Yap

The code for yap looks like the one above. However, yap is not capable to determine the output size from the PNG file. So it needs some help. First of all, we choose LaTeX output in the export dialog. In the next dialog, we enable bb output. This will generate a First.bb file, containing the image dimensions. If the file is in some subdirectory, we tell C.a.R. to use the file path of the original file. The other options of this dialog will be explained later.

Moreover, we change the above LaTeX source code, since we must now specify the output size.

\begin{figure}[h]
\centering
\includegraphics[width=12 cm]{First.png}
\caption{Kepler's Theorem}
\end{figure}


This works as expected. The print output for yap looks like the PDF output, even with color.

### EPS for Yap

By historical reasons, many LaTeX users prefer EPS graphics. It is possible to save graphics in C.a.R. in EPS too, but with many restrictions. In the end, the graphics will not look as good as on the screen. The advantages are the smaller file size, and the possibility to work with the EPS files in a vector program. PNG files can only be changed in a bitmap program.

During the export you get a similar, but more simple dialog than with the PNG export. Texts and fonts will also be scaled, and you can and should specify the output size. Have a look at the following output file. This time, the output is created via the DVI output, a PS conversion (dvips), followed by PDF conversion (ps2pdf).

You will notice that the epsilon is gone. Currently, C.a.R. cannot export these characters to PS. PS is not using Unicode, so it seems to be very difficult to embed foreign characters. Otherwise the PS output just looks fine.

The source code is the same, but we are now referring to the Test.eps file.

### PDF for PDF

Of course, one can embed PDF output from C.a.R. into PDF files with pdflatex. However, the same restrictions apply as with EPS for yap, so I do not recommend this.

## LaTeX writes Labels

This is a very unique feature of C.a.R. To get an even nicer embedding of the C.a.R. output into the LaTeX page, it is possible to let LaTeX draw the labels and strings in the construction. Have a look at the following PDF screen dump (scaled down in Acrobat Reader).

The two strings in the picture and the label at the focal point of the ellipse are now drawn by LaTeX using the current LaTeX font. This makes it possible to use the full LaTeX arsenal of math formatting. Even better, we can still preview the result in C.a.R. using its LaTeX formatting. The output will not quite look the same, but it is good enough to position the labels on the screen. Here is a scaled down (50%) version of what I saw on the screen.

Note that the export preview is on. That is why the strings are larger than normal.

To export for LaTeX, we enable Export for LaTeX, of course, and let LaTeX do all strings in the following dialog (check all boxes). One might also reduce to $...$ formulas in C.a.R., or even to special $$...$$ formulas, which work like $...$ in C.a.R. The program generates the following little file Kepler.ztx.

\setlength{\unitlength}{0.008466666666666667cm}
\begin{picture}(1417.0,1145.0)
\put(0,0){\includegraphics[width=11.997cm]{c:/texte/Test/ZuL/Kepler.png}}
\put(242.0,916.0){$\epsilon = 0.76$}
\put(1041.043,448.115){$S_1$}
\put(812.0,932.0){$\mbox{Covered area 13\%}$}
\end{picture}


This is not quite true, since we changed this file a bit before usage. As you see, the file places the graphics, scales it, and puts the strings on the graphics, using LaTeX commands. I recommend editing this file always.

We now have to include this file at the proper position of the LaTeX source. This is done with the LaTeX input command.

\begin{figure}[h]
\centering
\input{/texte/Test/ZuL/Kepler.ztx}
\caption{Kepler's Theorem}
\end{figure}


Then, we translate it with pdflatex and get the desired output.

## Summary

I hope this document gave a good impression and introduction into LaTeX output form C.a.R. As you see, it is possible to create images of high level quality in LaTeX, using PNG output. For those of you, who never used anything but EPS for LaTeX, I repeat that PNG is the better choice, unless you are mainly concerned about file size, or you want to edit the vector graphics later. Scalability is not a good reason to use vector graphics.