
How to LaTeX in RMarkdown
Learn some basics of LaTeX that you can use in RMarkdown to make your projects look more professional.
INTRODUCTION
Have you ever not known how to pronounce a word, especially one that you use relatively often? There have been many times I have felt embarrassed about how to say a word that I have only seen written, so I just avoid saying it out loud. It can make things a little complicated sometimes, but it is always nice to learn how to pronounce a word.
Something I learned relatively recently is how to pronounce LaTeX. If you were like me, I always pronounced it as ‘lay-tex’, but did you know the X at the end is not the English letter X, but the Greek letter chi, which is pronounced with a K like sound, so LaTeX become ‘lay-tech’.
Now you might be wondering why this data science blog is sounding more like an English lesson, but I just wanted to describe something I have recently become very passionate about, which is using LaTeX to write equations in RMarkdown. It can make your documents look more professional, and it is not hard to use. Here are some basics.
BASICS
When making an equation in RMarkdown, you just put two dollar signs at the beginning and end (see Fig1). Then you will be able to start typing your equations, but a couple of things to keep in mind, there is no spacing when you are doing an equation in LaTeX unless you specify that there should be one.
Spacing To add a line, you must put in \ to basically press Enter on your keyboard. There are a couple of different ways to add a single space, you can add : \; or \, for different sizes of spaces, though I really cannot tell the difference between them, so I think it will not matter too much (see Fig2). Use spacing to increase the readability of your equations (see Fig3).
Sub/Superscripts Another useful tool in LaTeX is sub and superscripts. They are pretty similar to each other, first you just put down the value that you want the sub/super script to go on, and then if it is a superscript, put ^ and then the value that you want as the superscript. For a subscript, you would use _ instead of ^. One thing to note, if you want more than one symbol in your sub/super script, you need to surround it in this type of bracket {} (see Fig4).
MATH FUN
Now what we have been waiting for, how to do use LaTeX for fancy math stuff. I am going to talk about fractions, sums, and integrals. When making a fraction, you are first going to start with the function \frac followed first by the numerator and denominator both surrounded by {} type of bracket, see Fig5.
Sums and Integrals Sums and integrals are similar to each other in that they use sub and superscripts that we talked about previously. To start an integral, you use \int and \sum is how you start the sum. You can have multiple arguments in the sub/super scripts, just make sure that you surround them by {} type of brackets. Fig6.
Greek Alphabet Another thing that I love about LaTeX is how easy it is to include Greek letters in the equations. As a statistics major, most of the math I do is filled with different Greek symbols that are annoying to include in a text editor like word. LaTeX in RMarkdown makes it easy, you just need to put a \ followed by the name of the Greek letter you want, so to include a theta, you would type out \theta. One thing to be aware of is whether you want the capital or lowercase Greek letter, you can change that by either capitalizing the first letter of the wanted Greek symbol or just leaving it lowercased, see Fig7.
One thing to note is that LaTeX does not include all of the capital and lowercase Greek letters, though I have no idea why that is. You can see which ones are missing in Fig 8. since I go through the Greek alphabet and do both lower and uppercase.
TEXT IN EQUATIONS
Sometimes, you just want to add a block of text to your equation, how I have found you do that is you start with \text and then have the text you want surrounded by {}, see Fig9.
A couple things to note, in a block of text, you do not need to add the spaces like you in an equation, but the same way you do in regular typing. Another thing is that in an equation, the words are italicized while text is not. see Fig10.
CONCLUSION
While there is still lots to learn, I have really enjoyed practicing my LaTeX skills in RMarkdown. If you have more questions, my advice is to just start practicing. I learned a lot of this stuff through trial and error, and I feel very comfortable using LaTeX now. There are also lots of resources online if you want to learn more.