LaTeX video tutorial for beginners (video 4)

This seven-part series of LaTeX tutorial videos was first published in 2013; consequently, today's editor interface has changed considerably due to the development of ShareLaTeX and the subsequent merger of ShareLaTeX and Overleaf. However, the video content is still relevant and teaches you the basics of LaTeX—skills and expertise that will apply across all platforms. You don’t need any previous background knowledge, and by the end of these LaTeX guides you will be able to create and write basic LaTeX documents and have the knowledge to start learning how to create more complicated documents. Each video is accompanied by a transcript (listed below the video)—these in-situ transcripts replace the URLs shown at the start of each tutorial.

Images in LaTeX

Video transcript

In the previous video we looked at using basic maths and equations in our LaTeX documents. In this video we are going to look at inserting graphics into our documents. Before we can start using images in LaTeX we need to load up the graphicx package. To do this we use the \usepackage command in the preamble and enter graphicx, spelt with an x after the c instead of an s, into the curly brackets, like this: \usepackage{graphicx}.

Next we need to upload the image we want to use into the ShareLaTeX project. To do this click the ‘new’ button in the left hand panel and select ‘upload file’. Once your image has uploaded it will appear in the project panel and it is now ready for us to use. I have uploaded an image called ‘lion’ which is a PNG file. You can also use JPG files or PDF files.

To put an image into the document we use the \includegraphics command. Then in the curly brackets you enter the file name of the image you want to use without the extension. If I now compile the document my picture will appear on the page. We can use the square brackets immediately after the command, but before the file name, to give LaTeX more instructions as to how we want our image to look. We can scale the size of the image by using the word scale followed by an equals sign and a number. For example by entering scale=2 we double the size of the image. We can also change the size by setting the height and/or width. When doing this we must tell LaTeX which one we want to edit and then give it a number in centimetres. If we just change one of the two, LaTeX will keep the image’s aspect ratio. To edit both of them we simply add a comma and a space after the first argument and then add the second. We can also rotate the image by using the keyword angle followed by equals and the number of degrees you want to rotate it in an anticlockwise direction.

Although this method of including graphics is a useful one, we often want more control over the image’s positioning. We may also want to add a caption and reference it. To achieve these things we need to use the figure environment. As with all environments we need to use a \begin and \end command to encapsulate it. So let’s add a \begin{figure} and \end{figure} command either side of our existing \includegraphics command. You will notice that the image has now moved onto another page. We can influence where LaTeX puts the figure by adding a specifier in square brackets after the \begin{figure} command. There are six specifiers you may want to know about. The first is a small h which stands for ‘here’ and puts the figure roughly where the code appears in the text. You can also use a small t which puts it at the top of the page, a small b which puts it at the bottom and a small p which puts it on a separate page reserved for figures and other floats. An exclamation mark is also a valid specifier which will override LaTeX’s idea of where is a good place to put the figure. Finally there is a special identifier, a capital H which will place the figure at exactly the place you are at in the LaTeX code, however you need to use the float package to make this one work. You will often need to include the exclamation mark along with one of the small letter specifiers to get the figure where you want it. You may also want to use the \centering command to centre the figure.

You can add a caption to your figure by using the \caption command. If you want the caption to go above the picture add the command into the code above the \includegraphics command, otherwise put it in below.

Lastly let’s briefly mention labels and referencing. In LaTeX we can label items that are numbered, like figures. This allows us to refer back to a labeled item from somewhere else in the document. To label an item use the \label command and then choose a name. For example I might label this figure fig:lion. Then If I put a \ref command somewhere in the document along with the name I chose, it will print the number assigned to my figure. Alternatively I could use the \pageref command to print the page number where my figure occurs. This is very useful as it means if I add figures in before this one at a later date, LaTeX will give me the the right updated numbers.

This concludes our discussion on using image in LaTeX. In the next video we’ll look at using BibTeX.