MPS115/MPS116 PRESENTATION LAB 4
IMPORTANT! If you are asked to install packages when processing your file, click ‘yes’ then wait. You may need to do this for multiple packages. At points it may look as though nothing is happening but your PDF should appear eventually. Interrupting the process can cause problems, so make sure you wait long enough!
Open TeXworks or TeXmaker or whichever editor you prefer. Find the preamble template on the ‘module webpage’ in the ‘Extras’ section, and copy and paste this to start this week’s document.
If your computer is unsure what to do with .tex files like the template on the course webpage, you can open it in any text editor (e.g. Notepad).
Let’s start by creating some graphs using pgfplots. You need to ensure that
\usepackage{pgfplots} is in your preamble; you will need to delete the
comment symbol, % from the template. Start a new section called ‘Graphs
created with PGFplots’ then enter the following.
\begin{tikzpicture}
\begin{axis}[
title={$y=\sin x$},
xlabel=$x$, ylabel=$y$]
\addplot[smooth, domain=-360:360]{sin(x)};
\end{axis}
\end{tikzpicture}
If you get errors here, check that you have recreated the text precisely, including commas and the semicolon.
axis lines=center,title={$y = \sin x$},
tikzpicture environment inside a center environment.
Note the US spelling of ‘center’.
-2*pi:2*pi.
What’s happened now? PGFplots’s trigonometric functions work in
degrees: the deg(x) function takes a number (in radians) and turns it
into the value in degrees; that is, .Try a 3D plot, using the following code.
\begin{tikzpicture}
\begin{axis}[xlabel=$x$, ylabel=$y$]
\addplot3[domain=-1.5:1.5, surf]{-exp(-x^2 - y^2)};
\end{axis}
\end{tikzpicture}
ylabel=$y$ do?
(Note the US spelling of the word “color”.)There are many more examples of graphs – with source code – on the PGFplots website, http://pgfplots.sourceforge.net/gallery.html. If you need to make a graph, a combination of the simple techniques on this sheet and what you find at the link above should do the trick.
Now let’s try including an image from external software. Open a new tab in your browser and go to https://www.geogebra.org/geometry. Geogebra is a point-and-click geometry package which can do more than just create pictures for including in documents.
Play around further if you like. Once you have a picture you are happy with, go
to the options in the top-left, click Download As, then choose PDF as the file
type and save the image as geogebra_image.pdf in the same folder as your
LaTeX document.
Return to your LaTeX document. Put \usepackage{graphicx} in your
preamble. Start a new section called ‘Including image files’ and put the command
\includegraphics{geogebra_image.pdf} underneath. Your picture should
appear though it may not fit properly at this stage. Convince yourself the picture
stays smooth no matter how far you zoom in.
If you have problems, check that the name of the uploaded file is exactly the same in the includegraphics command as it is in the filename, and make sure the file is saved in the same folder as the .tex file.
\includegraphics command by changing \includegraphics
to \includegraphics[scale=0.5]. What has changed?
\includegraphics[width=8cm].
\includegraphics[width=\textwidth].
\includegraphics[width=0.8\textwidth].
center environment.Note that this might have resulted in an image where the labels are unreadable! You should always ensure that labels are legible. In this case you would have to go to geogebra and either alter the font size or else the range of the image that is exported. If you have lots of time in the lab, you should try to see how you can alter the font size in geogebra.
To enable images to be referenced, they should be placed inside the figure
environment; this will allow you to refer to Figure 1, for instance. Try this
instead of using center, so that your LaTeX reads
\begin{figure}[tbh]
\includegraphics[width=0.8\textwidth]{geogebra_image.pdf}
\caption{A Geogebra picture}
\end{figure}
LaTeX will move figure environments around the document depending on the size of the figure and the space available. Environments like this are called ‘floats’ as they can float around the document. If you are looking this in a PDF document, you will see Figure 1 has ‘floated’ to the top of the page. You might find this behaviour annoying to start with, but it is the standard way to do things in professional documents.
The term [tbh] after \begin{figure} is telling LaTeX to put the figure at the
top of a page if there’s room, and if not try the the bottom of the page and then
here, i.e., at the position it appears in the LaTeX document. Don’t fight this! Let
LaTeX put floats where it thinks is sensible.
Now add \label{fig:geogebra} after the \caption{...} command, then write
the following sentence underneath the figure environment
Geogebra creates good diagrams; see Figure~\ref{fig:geogebra}.
Process your file to see the figure being referenced automatically.
The tilde, ~, puts in a space, but does not allow it to be split across lines. You
should do this when using \ref as “Figure 1” looks good but “Figure 1” does
not.
Finish off the sheet. If necessary look at the attempt at the lab given on the module webpage. You should also look at the module solution to last week’s homework.
Consider the function defined by . We want to compare this to the function , particularly for . Your task is to produce a high-quality plot of the graphs of both of these functions on the same set of axes using two different methods, and include them in a document.
You are aiming for high-quality plots, so you should do the following.
Create a LaTeX document with title ‘MPS115: Homework 4’ (or MPS116) and your name on as author. Each of the two plots should appear in a figure environment of its own, with a sufficiently detailed caption.
Start the document with a brief paragraph which states what the two figures are and explains which method – PGFPlots or Geogebra – you would prefer to use, giving a few reasons, involving, for instance, how easy the plot is to produce and what it looks like. If you find it hard to choose between them then say why.
Print out and hand in the PDF and LaTeX file of your homework at next week’s lab, as usual. Remember that this is assessed coursework so must be your own work.
If you have any questions do use the discussion forum or the office hour.