Difference between revisions of "LaTeX:Pictures"

(Cropping)
(Cropping)
Line 75: Line 75:
 
\end{document}
 
\end{document}
 
</pre>
 
</pre>
 
+
For example, the code above should result in a [http://www.artofproblemsolving.com/LaTeX/Samples/image1cropped.pdf document like this]. You can see that we only have a 90x90 pixel part of the image.
For example, the code above should result in a [http://www.artofproblemsolving.com/LaTeX/Samples/image1cropped.pdf document like this]. You can see that we only have a 90x90 pixel part of the image.
 
  
 
You may have noticed the subtle change to \includegraphics*: we added a * to the end of the command. This * "clips" the image to the viewport that we specified. If we had omitted it:
 
You may have noticed the subtle change to \includegraphics*: we added a * to the end of the command. This * "clips" the image to the viewport that we specified. If we had omitted it:

Revision as of 19:21, 10 July 2007

LaTeX
About - Getting Started - Diagrams - Symbols - Downloads - Basics - Math - Examples - Pictures - Layout - Commands - Packages - Help

This article will cover incorporating pictures and diagrams into LaTeX documents. If you're dealing strictly with geometric diagrams, consider reading about Asymptote, a graphics language that works extremely well with LaTeX.

How to Incorporate Pictures

Consult the Asymptote article for producing geometric diagrams within LaTeX. Otherwise, if you are using LaTeX to produce PDF documents, you can images such as geometric diagrams in your documents. The machine that we'll use to include images is the graphicx package.

To walk through this example, first download a sample image by clicking here to open the image, then saving it to your own computer. (Alternatively, depending on your browser, you may be able to right-click on the link to the image and choose "Save link as...") Save the image with the name 'myimage.png'. Make sure you pay attention to what folder you are saving the image to.

Next, open a new LaTeX source file in TeXnicCenter and input the following:

\documentclass{article}
\usepackage[pdftex]{graphicx}
\begin{document}

This is my first image.

\includegraphics{myimage.png}

That's a cool picture up above.
\end{document}

Save your source file in the same folder as you saved myimage.png. (Very important - if you save it in a different folder, you will get an error!)

Compile your source code and view the result. Make sure you have TeXnicCenter set up to produce PDF documents (LaTeX => PDF in the appropriate drop-menu). If you don't, you'll get a bunch of errors. If you followed all the steps correctly, you should get a file that looks a lot like what you'll see when you click here.

Let's take a closer look at how this code works. First, notice the new line

\usepackage[pdftex]{graphicx}

This line includes the package graphicx, which contains all the image-handling routines you need to include images. To include a specific image, we use the statement:

\includegraphics{myimage.png}

Picture Manipulation

In this section we'll discuss how you can manipulate images in LaTeX.

Note that some of these commands work with text as well (see reflecting) - diagrams are not the only items that can be reflected, rotated, etc. In practice, the centering is the only one of the above you're likely to use much with text.

We'll start where we left off on the How To page. If you haven't already reviewed that page, do so now. Make sure you save the myimage.png file to your computer for use in these examples. Our original source code is:

\documentclass{article}
\usepackage[pdftex]{graphicx}
\begin{document}

This is my first image.

\includegraphics{myimage.png}

That's a cool picture up above.
\end{document}

Usually, we manipulate the image by adding an optional command to the \includegraphics statement. This optional command goes in brackets [...] between the \includegraphics command and the name of the image. For example,

\includegraphics[angle=45]{myimage.png}

will rotate the image 45 degrees.

Cropping

The viewport optional command of the \includegraphics command tells what portion of the image file to include. The first two coordinates are the (x,y) coordinates in pixels of the lower left corner of the portion of the image file you want to inclue. The second two coordinates are the upper right values of (x,y). Try changing the values in the original source file and see what happens.

\documentclass{article}
\usepackage[pdftex]{graphicx}
\begin{document}

This is my first image.

\includegraphics*[viewport=30 30 120 120]{myimage.png}

That's a cool picture up above.

\end{document}

For example, the code above should result in a document like this. You can see that we only have a 90x90 pixel part of the image.

You may have noticed the subtle change to \includegraphics*: we added a * to the end of the command. This * "clips" the image to the viewport that we specified. If we had omitted it:

\documentclass{article}
\usepackage[pdftex]{graphicx}
\begin{document}

This is my first image.

\includegraphics[viewport=30 30 120 120]{myimage.png}

That's a cool picture up above.

\end{document}

we end up shifting the image to a new position, but not cropping it, as shown here.

Unfortunately, there's no easy way to read off the coordinates for your image. You'll have to use trial and error to set these values for each image.

Spacing

Scaling and Resizing

Rotating

Reflecting

Centering

Examples of Pictures in LaTeX

See Also