10.1 Understand the basics of LaTeX - Video Tutorials & Practice Problems
Video duration:
7m
Play a video:
Video transcript
<v Voiceover>LaTeX is a very</v> powerful typesetting tool that many scientific reports, or just reports in general, are written in. Thanks to the knitr package, it is very easy to integrate R code with LaTeX code and make nice reproducible presentations. This is not intended to be a full introduction to LaTeX, but we will go over some of the basics so that it can be used with knitr. LaTeX does not come pre-installed on all computers. If you don't have it, you need to go get it. If you Google LaTeX, you'll be able to find it. On Windows, the most popular one is MiKTeX. Linux and Mac have their own distributions and you just pick the best one. And after you get it installed, you're ready to start using it with R. So to create a new knitr document, we'll come to File. Say, New File, R Sweave. Now Sweave is the old-fashioned way of working with LaTeX documents. We are going to be using knitr, but we're still going to use an Sweave document. Notice it builds some of it for us already. It does document class, which tells it to use an article. There's also book class, there's resume class, there's all sorts of things that have been built out there. And then, begin document and end document. That is where all the content will go. In between the document class declaration and the begin document is the preamble. You can put stuff in here that helps tell it what we're going to do. So up here, we'll say this is a comment. Anything that comes on a line after a percent sign is a comment. Since we are going to be using graphics, we say use package graphic x. This, if we spell it correctly, allows us to use images. While we're at it, we're going to declare the graphics extension. What this means is, when you're searching for images, first look for a .png of the appropriate file name. If you can't find that, find a .jpg. It's also possible to make new variables of a kind in LaTeX. They're called shortcuts or macros. We will call it a variable. So we do new command, and we say we want data frame, because we'll be using that word a lot. So every time we do \dataframe, it will be rewritten as slash t-e-x-t-t. This \texttt turns the font into a typewriter style font. Then we put in data.frame, then close the curly braces. Anytime in this document where we write \dataframe, it will show up as data.frame in a special font. You can also declare some other things here. We can say \author is Jared P. Lander. We can then also say that the title is A Simple Article. And lastly, we can give it a date, which is August 28th, 2013. Then we'll begin the document. If we want that title to show up, we say \maketitle. This prints out that title page. If we want a table of contents, we say \tableofcontents. LaTeX is great for breaking a document in to discrete parts. It's a really great way to stay organized. So to begin a section of a document, you say \section. We'll call it Getting Started. If you give it a label, you'll be able to refer to it later. In this case we'll say sec Getting Started. That's just a label to help us keep things straight. So in here, I'll just write something like This is the first section of our document. So if we were to just print this out right now, we'd get a little document that says "This is the first section of our article. We will only talk about dataframes." Why don't we see if it works because sometimes LaTeX can be a bit finicky and doesn't always work on the first shot. Since we're using R studio, everything is easy. You just the Compile PDF button. Of course, the file needs to be saved first because LaTeX runs off actual written files. So we do latex.rnw. That stands for R no-web. Don't worry about the extension. You just need it to say rnw. Now it's ready. First it converts it to a tech file, then to a PDF. We now already have a nice PDF file. Back to our document, if we want to have another paragraph, you simply leave a blank line. So a new paragraph comes after a blank line. Now let's say we're ready for a new section. You don't need to end the current section, you just start the new section. We will call this More Information. And here we will give it a label. We'll call it sec More Info. And we'll say, "Here is another section". In \ref sec Getting Started, we are going to refer to that first section and it will automatically put the right number. We say we learned about dataframes. We'll say that again. If we run this, and we'll see if it works, we now have two sections. So if we zoom in, because it's a PDF. It doesn't always come in the right zoom. Here's another section. "In 1", we've got to clarify it. So let's go back to our document and say, In Section tilde reference. That tilde means put a space in there, but don't let it break over our line. Here we say, In section one we learned about dataframes. It can reference itself. It can also do subsections. So we'll say \subsection, and we'll call it First Subsection, and we'll give it a label of First Sub. And we'll just put some stuff in here. Content for a subsection. Nothing big deal. Then we'll put- Label needs to be spelled correctly. Then we'll put another subsection. We'll call it Second Subsection. We'll give it another label called Second Sub. And we'll just put in some more content. More content that is nested in section ref sec More Info. So if we are to run this, You'll see we have this nice little document with an automatically generated table of contents, sections and subsections that can reference each other. You'll notice over here in our Git pane that we created latex.rnw, a .log file was created, a .tex file, a table of contents, and a final PDF. That all happens automatically. It's also possible to create an index using LaTeX. It's a little beyond what we want to do but indexing is a great thing so you can build amazing looking indexes that reference different parts of your document very easily. LaTeX is a great tool to learn if you'll be running off reports repeatedly or doing anything mathematic or scientific. It is much easier to write with LaTeX than it is to say, write with Word, especially once you start interweaving R code.