Difference between revisions of "LaTeX:Pictures"
Marianasinta (talk | contribs) (dr4t) |
|||
(24 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
{{Latex}} | {{Latex}} | ||
− | This article will cover incorporating pictures and diagrams into LaTeX documents. If you're dealing strictly with geometric diagrams, consider reading about [[Asymptote:About|Asymptote]], a graphics language that works extremely well with LaTeX. | + | This article will cover incorporating pictures and diagrams into LaTeX documents. If you're dealing strictly with geometric diagrams, consider reading about [[Asymptote: About|Asymptote]], a graphics language that works extremely well with LaTeX--as you can see from the length of this article, working with graphics and pictures without Asymptote in LaTeX is no easy feat. |
==How to Incorporate Pictures== | ==How to Incorporate Pictures== | ||
− | + | You can use more filetypes than just .png's--you can even use a 1 page .pdf file as your image! You could also produce geometric diagrams within LaTeX as shown in the [[Asymptote: About|Asymptote]] article. Otherwise, if you are using LaTeX to produce PDF documents, you can make 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 [http://www.artofproblemsolving.com/LaTeX/Samples/image1.png clicking here] to open the image, then | + | |
+ | To walk through this example, first download a sample image by [http://www.artofproblemsolving.com/LaTeX/Samples/image1.png clicking here] to open the image, then save 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 [https://artofproblemsolving.com/wiki/index.php/TOTO_SLOT_:_SITUS_TOTO_SLOT_MAXWIN_TERBAIK_DAN_TERPERCAYA TOTO SLOT]. | ||
Next, open a new LaTeX source file in TeXnicCenter and input the following: | Next, open a new LaTeX source file in TeXnicCenter and input the following: | ||
Line 15: | Line 16: | ||
\begin{document} | \begin{document} | ||
− | This is my first image. | + | This is my first image. Hello world! |
\includegraphics{myimage.png} | \includegraphics{myimage.png} | ||
Line 24: | Line 25: | ||
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!) | 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 [http://www.artofproblemsolving.com/LaTeX/Samples/image1doc.pdf when you click here]. | + | 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 because LaTeX will expect your pictures to be in the EPS (Encapsulated PostScript) format. If you followed all the steps correctly, you should get a file that looks a lot like what you'll see [http://www.artofproblemsolving.com/LaTeX/Samples/image1doc.pdf when you click here]. |
Let's take a closer look at how this code works. First, notice the new line | Let's take a closer look at how this code works. First, notice the new line | ||
Line 35: | Line 36: | ||
</pre> | </pre> | ||
+ | ==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: | ||
+ | <pre> | ||
+ | \documentclass{article} | ||
+ | \usepackage[pdftex]{graphicx} | ||
+ | \begin{document} | ||
+ | |||
+ | This is my first image. | ||
+ | |||
+ | \includegraphics{myimage.png} | ||
+ | |||
+ | That's a cool picture up above. | ||
+ | \end{document} | ||
+ | </pre> | ||
+ | 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, | ||
+ | <pre> | ||
+ | \includegraphics[angle=45]{myimage.png} | ||
+ | </pre> | ||
+ | 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 include. 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. | ||
+ | <pre> | ||
+ | \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} | ||
+ | </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. | ||
+ | |||
+ | 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: | ||
+ | <pre> | ||
+ | \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} | ||
+ | </pre> | ||
+ | we end up shifting the image to a new position, but not cropping it, [http://www.artofproblemsolving.com/LaTeX/Samples/image1shifted.pdf as shown here]. | ||
+ | |||
+ | To set the coordinates for an image, it is sometimes easiest to use trial and error, but it's also possible to read the coordinates by loading the image in GIMP (for a png) or ghostview (for a ps or pdf) and moving the cursor. | ||
+ | |||
+ | ===Spacing=== | ||
+ | One issue to watch out for is spacing. Suppose we take out the empty lines from our original source code: | ||
+ | <pre> | ||
+ | \documentclass{article} | ||
+ | \usepackage[pdftex]{graphicx} | ||
+ | \begin{document} | ||
+ | |||
+ | This is my first image. | ||
+ | \includegraphics{myimage.png} | ||
+ | That's a cool picture up above. | ||
+ | \end{document} | ||
+ | </pre> | ||
+ | If you compile this, you should get something that [http://www.artofproblemsolving.com/LaTeX/Samples/image1nospace.pdf looks like this]. By removing the blank lines, we are telling LaTeX to try to put the image in the same line as the text. You see the mess that results. Generally, put an extra line before and after your image inclusion in your source file and you should be fine. This will also help you find the image in the source file if you need to edit. | ||
+ | |||
+ | ===Scaling and Resizing=== | ||
+ | We can easily scale the size of the diagram by using the scale optional command. | ||
+ | <pre> | ||
+ | \documentclass{article} | ||
+ | \usepackage[pdftex]{graphicx} | ||
+ | \begin{document} | ||
+ | |||
+ | This is my first image. | ||
+ | |||
+ | \includegraphics[scale=0.25]{myimage.png} | ||
+ | |||
+ | That's a cool picture up above. | ||
+ | |||
+ | \end{document} | ||
+ | </pre> | ||
+ | This scaled our figure by a factor of 0.25 (in other words, it makes it 4 times smaller), [http://www.artofproblemsolving.com/LaTeX/Samples/image1scaled.pdf as shown here]. | ||
+ | |||
+ | We can also scale the picture by explicitly setting the width and the height by using the width and height optional commands. For example: | ||
+ | <pre> | ||
+ | \documentclass{article} | ||
+ | \usepackage[pdftex]{graphicx} | ||
+ | \begin{document} | ||
+ | |||
+ | This is my first image. | ||
+ | |||
+ | \includegraphics[width=5in,height=1in]{myimage.png} | ||
+ | |||
+ | That's a cool picture up above. | ||
+ | |||
+ | \end{document} | ||
+ | </pre> | ||
+ | resizes our image to be 5 inches by 1 inch, [http://www.artofproblemsolving.com/LaTeX/Samples/image1smushed.pdf as shown here]. | ||
+ | |||
+ | ===Rotating=== | ||
+ | We can rotate a diagram by using the angle optional command. | ||
+ | <pre> | ||
+ | \documentclass{article} | ||
+ | \usepackage[pdftex]{graphicx} | ||
+ | \begin{document} | ||
+ | |||
+ | This is my first image. | ||
+ | |||
+ | \includegraphics[angle=45]{myimage.png} | ||
+ | |||
+ | That's a cool picture up above. | ||
+ | |||
+ | \end{document} | ||
+ | </pre> | ||
+ | This rotates our picture 45 degrees (counterclockwise), [http://www.artofproblemsolving.com/LaTeX/Samples/image1rotated.pdf as shown here]. | ||
+ | |||
+ | ===Reflecting=== | ||
+ | We can horizontally reflect a diagram by using the \reflectbox command. | ||
+ | <pre> | ||
+ | \documentclass{article} | ||
+ | \usepackage[pdftex]{graphicx} | ||
+ | \begin{document} | ||
+ | |||
+ | This is my first image. | ||
+ | |||
+ | \reflectbox{\includegraphics{myimage.png}} | ||
+ | |||
+ | That's a cool picture up above. | ||
+ | |||
+ | \end{document} | ||
+ | </pre> | ||
+ | [http://www.artofproblemsolving.com/LaTeX/Samples/image1reflected.pdf See the result here] - note that the text in the center of the image now reads backwards! The \reflectbox command is pretty simple - put the item to be horizontally reflected in { }: | ||
+ | |||
+ | \reflectbox{item reflected} | ||
+ | |||
+ | \reflectbox has two companion commands \scalebox and \rotatebox. These can be used with text as well as graphics, to produce some funky effects: | ||
+ | <pre> | ||
+ | \documentclass{article} | ||
+ | \usepackage[pdftex]{graphicx} | ||
+ | \begin{document} | ||
+ | |||
+ | This document shows some funky effects with text. | ||
+ | |||
+ | \scalebox{2}{\rotatebox{60}{\reflectbox{This is really weird text!}}} | ||
+ | |||
+ | \end{document} | ||
+ | </pre> | ||
+ | The [http://www.artofproblemsolving.com/LaTeX/Samples/weirdtext.pdf output document] shows text that is doubled in size, rotated 60 degrees, and reflected. | ||
+ | |||
+ | ===Centering=== | ||
+ | We center text or images using \begin{center} and \end{center}. | ||
+ | <pre> | ||
+ | \documentclass{article} | ||
+ | \usepackage[pdftex]{graphicx} | ||
+ | \begin{document} | ||
+ | |||
+ | This is my first image. | ||
+ | |||
+ | \begin{center} | ||
+ | \includegraphics{myimage.png} | ||
+ | \end{center} | ||
+ | |||
+ | That's a cool picture up above. | ||
+ | |||
+ | \end{document} | ||
+ | </pre> | ||
+ | Just put \begin{center} when you want to start centering, and \end{center} when you want to stop centering. (If you want to center everything until the end of the document, you still need an \end{center} before the \end{document} at the end of the source file. | ||
+ | The code above should give you something [http://www.artofproblemsolving.com/LaTeX/Samples/image1centered.pdf like this]. | ||
==See Also== | ==See Also== | ||
− | *[[LaTeX: | + | *[[LaTeX:Layout | Next: Layout]] |
− | *[[LaTeX: | + | *[[LaTeX:Examples | Previous: Examples]] |
Latest revision as of 15:52, 19 February 2024
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--as you can see from the length of this article, working with graphics and pictures without Asymptote in LaTeX is no easy feat.
Contents
How to Incorporate Pictures
You can use more filetypes than just .png's--you can even use a 1 page .pdf file as your image! You could also produce geometric diagrams within LaTeX as shown in the Asymptote article. Otherwise, if you are using LaTeX to produce PDF documents, you can make 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 save 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 TOTO SLOT.
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. Hello world! \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 because LaTeX will expect your pictures to be in the EPS (Encapsulated PostScript) format. 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 include. 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.
To set the coordinates for an image, it is sometimes easiest to use trial and error, but it's also possible to read the coordinates by loading the image in GIMP (for a png) or ghostview (for a ps or pdf) and moving the cursor.
Spacing
One issue to watch out for is spacing. Suppose we take out the empty lines from our original source code:
\documentclass{article} \usepackage[pdftex]{graphicx} \begin{document} This is my first image. \includegraphics{myimage.png} That's a cool picture up above. \end{document}
If you compile this, you should get something that looks like this. By removing the blank lines, we are telling LaTeX to try to put the image in the same line as the text. You see the mess that results. Generally, put an extra line before and after your image inclusion in your source file and you should be fine. This will also help you find the image in the source file if you need to edit.
Scaling and Resizing
We can easily scale the size of the diagram by using the scale optional command.
\documentclass{article} \usepackage[pdftex]{graphicx} \begin{document} This is my first image. \includegraphics[scale=0.25]{myimage.png} That's a cool picture up above. \end{document}
This scaled our figure by a factor of 0.25 (in other words, it makes it 4 times smaller), as shown here.
We can also scale the picture by explicitly setting the width and the height by using the width and height optional commands. For example:
\documentclass{article} \usepackage[pdftex]{graphicx} \begin{document} This is my first image. \includegraphics[width=5in,height=1in]{myimage.png} That's a cool picture up above. \end{document}
resizes our image to be 5 inches by 1 inch, as shown here.
Rotating
We can rotate a diagram by using the angle optional command.
\documentclass{article} \usepackage[pdftex]{graphicx} \begin{document} This is my first image. \includegraphics[angle=45]{myimage.png} That's a cool picture up above. \end{document}
This rotates our picture 45 degrees (counterclockwise), as shown here.
Reflecting
We can horizontally reflect a diagram by using the \reflectbox command.
\documentclass{article} \usepackage[pdftex]{graphicx} \begin{document} This is my first image. \reflectbox{\includegraphics{myimage.png}} That's a cool picture up above. \end{document}
See the result here - note that the text in the center of the image now reads backwards! The \reflectbox command is pretty simple - put the item to be horizontally reflected in { }:
\reflectbox{item reflected}
\reflectbox has two companion commands \scalebox and \rotatebox. These can be used with text as well as graphics, to produce some funky effects:
\documentclass{article} \usepackage[pdftex]{graphicx} \begin{document} This document shows some funky effects with text. \scalebox{2}{\rotatebox{60}{\reflectbox{This is really weird text!}}} \end{document}
The output document shows text that is doubled in size, rotated 60 degrees, and reflected.
Centering
We center text or images using \begin{center} and \end{center}.
\documentclass{article} \usepackage[pdftex]{graphicx} \begin{document} This is my first image. \begin{center} \includegraphics{myimage.png} \end{center} That's a cool picture up above. \end{document}
Just put \begin{center} when you want to start centering, and \end{center} when you want to stop centering. (If you want to center everything until the end of the document, you still need an \end{center} before the \end{document} at the end of the source file.
The code above should give you something like this.