Practice Week 4 - LaTeX

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Practice: Getting Started with LATEX

Dr. Phan Thanh Hien


December 2024

The best way to learn LATEX is by writing your own code and practicing in different sit-
uations. This practical exercise includes several sample problems and their solutions. The
problems are setting up following a process, the one later is based on the previous, and more
difficulty. Feel free to refer back to the guide, use the hints. or use Google if you are stuck.
Recall that there can be more than one way to answer some of these questions, so if your
solution does not match what is given but the code compiles correctly, that is okay.
These excercise is taken from the website of the NYU Library [1]. The list of LATEX
Mathematical Symbols can be found at the https://www.cmor-faculty.rice.edu/~heinken/
latex/symbols.pdf [2].

1 Getting started
Create a document with the title Hello World!, your name, and today’s date. Include in the
document the following text ”Hello World! Today I am learning LATEX.”
Overleaf is a great online LaTeX editing tool that allows you to create LaTeX documents
directly in your web browser.

1.1 Log in to Overleaf


• To start using Overleaf go to www.overleaf.com.

• If you don’t have an account enter your e-mail address and set a password in the corre-
sponding boxes.

• If you already have an account, click Login in the upper right corner, then type in your
email and password and click the Login button.

1
1.2 Your first document: Hello World!
Once you’ve logged in already, you can start a new project and give a name to it: ”Hello
World”. Then create your first document as shown in Figure 1.

Figure 1: Create your first document using Overleaf.com

• Remember that the first part of any document is the preamble and it must begin with
\documentclass{} an article is a good document class for this exercise.

• To make the title look nice use the command \maketitle, but make sure you put it in the
body of the document

• For \maketitle to work correctly you will want to use \title{}, \author{}, and \date{},
but they don’t belong in the document body.

• What happens if you don’t include \date{}? What happens if you use \date{} with no
argument? What if you use \date{\today}?

• To start the body of your document use the command \begin{document}. To end your
document use the command \end{document}

• Try the command \LaTeX

1 \ documentclass { article }
2
3 \ title { Hello World !}
4 \ author { Your Name }
5 \ date { January 1 , 1831}
6 \ begin { document }
7 \ maketitle
8
9 \ textbf { Hello World !} Today I am learning \ LaTeX {}.
10 \ end { document }
Listing 1: Your first LATEX document

2
2 Adding some simple math
Add to the document you created in exercise 1 the following text:

LaTeX is a great program for writing math. I can write in line math such as aˆ2 + bˆ2
= cˆ2. I can also give equations their own space:
gammaˆ2 +thetaˆ2 = omegaˆ2

Your final document should look like the the one that showed in the Figure 2.

Figure 2: Adding some simple math.

• Is there a problem with the spacing after the second \LaTeX? Try to use \LaTeX{} an
empty argument can improve spacing with some commands.
• To write in line math mode you need to include a $ before and after the text that should
be written as math.
• $$ before and after an equation or \[ and \] will cause LaTeX to entire a display environ-
ment
• If you don’t know how to create Greek letters check out the resources tab.
• Use ∧ for superscripts. Use for subscripts.

1 \ documentclass { article }
2
3 \ title { Hello World !}
4 \ author { Your Name }
5 \ date { January 1 , 1831}
6 \ begin { document }
7 \ maketitle
8
9 \ textbf { Hello World !} Today I am learning \ LaTeX . \ LaTeX {} is a great
program for writing math . I can write in line math such as $a ^2+ b ^2= c ^2 $ .
I can also give equations their own space : \[ \ gamma ^2+\ theta ^2=\ omega
^2\]
10 \ end { document }
Listing 2: Adding some simple math

3
3 More Math
In this exercise you will build onto the document created in exercises 1 and 2 with more
complicated math and structures. This is a much more difficult exercise so take your time.
First give the display environment equation from exercise 2 an equation number. Second
add the following line of text (make sure there it is not indented as a new paragraph:

“Maxwell’s equations” are named for James Clark Maxwell and are as follow:

Now write Maxwell’s equations (as seen in the Figure 3), use an align environment and align
the equations at the = signs and at the equations names.

Figure 3: Maxwell’s equations

• Did you include the amsmath package command, \usepackage{amsmath}? Some symbols
and advanced math environments such as align will not work with out it!

• The equation environment, \begin{equation}, automatically puts you in display mode


and includes equations numbers. If you want to use this mode but don’t want equation
numbers use equation*

• To create quotation marks in LaTeX use the symbol ‘ (the ∼ key) twice on the left and
use the ’ key twice on the right. Using ” on the left and right will not work properly.

• In the align environment you use the & to denote points of alignment. If you want a
second alignment point use &&.

• If you are having trouble with spacing use around = use the command \quad this adds
extra horizontal space.

4
• When adding text in math mode you need to use the command \text{} where the text
you want added is the argument. This command tells LaTeX to use regular text at that
location.

• \vec{} will create vector notation. \partial will give you partial derivatives, and remember
for fractions use \frac{numerator}{denomonator}.

• If you are having trouble getting your parenthesis to look right use the command \left(
and \right) this will automatically fit the parenthesis to the equation within. This also
works with \left[, \left{, and \left|, but you always need a \right to go with it.

1 \ documentclass { article }
2
3 \ usepackage { amsmath }
4
5 \ title { Hello World !}
6 \ author { Your Name }
7 \ date { January 1 , 1831}
8
9 \ begin { document }
10 \ maketitle
11

12 \ textbf { Hello World !} Today I am learning \ LaTeX . \ LaTeX {} is a great


program for writing math . I can write in line math such as $a ^2+ b ^2= c ^2 $ .
I can also give equations their own space :
13 \ begin { equation }
14 \ gamma ^2+\ theta ^2=\ omega ^2
15 \ end { equation }
16 ‘‘ Maxwell ’ s equations ’ ’ are named for James Clark Maxwell and are as
follow :
17 \ begin { align }
18 \ vec {\ nabla } \ cdot \ vec { E } \ quad &=\ quad \ frac {\ rho }{\ epsilon_0 } &&\ text {
Gauss ’ s Law } \\
19 \ vec {\ nabla } \ cdot \ vec { B } \ quad &=\ quad 0 &&\ text { Gauss ’ s Law for
Magnetism }\\
20 \ vec {\ nabla } \ times \ vec { E } \ quad &=\ hspace {10 pt } -\ frac {\ partial {\ vec { B
}}}{\ partial { t }} &&\ text { Faraday ’ s Law of Induction } \\
21 \ vec {\ nabla } \ times \ vec { B } \ quad &=\ quad \ mu_0 \ left ( \ epsilon_0 \ frac {\
partial {\ vec { E }}}{\ partial { t }}+\ vec { J }\ right ) &&\ text { Ampere ’ s Circuital
Law }
22 \ end { align }
23 \ end { document }
Listing 3: Adding more math: Maxwell’s equations

4 Creating Sections and Referencing Equation


Labels and reference are very simple to execute in LaTeX, can be used with any numbered
object such as figures, equations, and sections, and are automatically updated whenever the
document is complied. If, for example, you realized you forgot an equation somewhere in the
middle of your document, between 10 other equations, all the equations after the newly inserted
equation would automatically be renumbered and proper references to them will reflect this new
numbering!
For this exercise take the document made in the previous exercise and create two sections
one at the beginning (Getting Started) and one after all your texts (What about Matrix Equa-
tions?). Also add to your document references to each of Maxwell’s Equations, and then write

5
a sentence that references each equation. You will need to use the commands \label{}) and
\ref{}.
It is common practice in LaTeX when labeling to use the format eq:name, fig:name, tab:name,
and so on depending on the type of object you are labeling. If you are confused by this look at
the labels in the solution.
Also, if you would like your citations to act as hyperlinks you need to use the package hyper-
ref, \usepackage{hyperref}, remember with this package you can change the default hyperlink
settings with the command \hypersetup{} in the preamble i.e. \hypersetup{colorlinks=true,
linkcolor=blue, urlcolor=blue, citecolor=blue}.

• For sections use the \section{} command.

• Make sure you use the \label{} command at the end of each equation before the line
break command \\.

• You may need compile your document twice in order to get cross references to work.

Figure 4: Creating Sections

1 \ documentclass { article }
2
3 \ usepackage { amsmath }
4 \ usepackage { hyperref }

6
5 \ hypersetup { colorlinks = true , linkcolor = blue , urlcolor = blue , citecolor = blue }
6

7 \ title { Hello World !}


8 \ author { Your Name }
9 \ date { January 1 , 1831}
10
11
12 \ begin { document }
13 \ maketitle
14 \ section { Getting Started }
15 \ textbf { Hello World !} Today I am learning \ LaTeX . \ LaTeX {} is a great
program for writing math . I can write in line math such as $a ^2+ b ^2= c ^2 $ .
I can also give equations their own space :
16 \ begin { equation }
17 \ gamma ^2+\ theta ^2=\ omega ^2
18 \ end { equation }
19 ‘‘ Maxwell ’ s equations ’ ’ are named for James Clark Maxwell and are as
follow :
20 \ begin { align }
21 \ vec {\ nabla } \ cdot \ vec { E } \ quad &=\ quad \ frac {\ rho }{\ epsilon_0 } &&\ text {
Gauss ’ s Law } \ label { eq : GL }\\
22 \ vec {\ nabla } \ cdot \ vec { B } \ quad &=\ quad 0 &&\ text { Gauss ’ s Law for
Magnetism } \ label { eq : GLM }\\
23 \ vec {\ nabla } \ times \ vec { E } \ quad &=\ hspace {10 pt } -\ frac {\ partial {\ vec { B
}}}{\ partial { t }} &&\ text { Faraday ’ s Law of Induction } \ label { eq : FL }\\
24 \ vec {\ nabla } \ times \ vec { B } \ quad &=\ quad \ mu_0 \ left ( \ epsilon_0 \ frac {\
partial {\ vec { E }}}{\ partial { t }}+\ vec { J }\ right ) &&\ text { Ampere ’ s Circuital
Law } \ label { eq : ACL }
25 \ end { align }
26 Equations \ ref { eq : GL } , \ ref { eq : GLM } , \ ref { eq : FL } , and \ ref { eq : ACL } are some
of the most important in Physics .
27 \ section { What about Matrix Equations ?}
28 \ end { document }
Listing 4: Adding more math: Maxwell’s equations

5 Creating Matrix Equations


In this exercise add the matrix equation to your document as seen in the Figure 5.

• There are different matrix environments in LATEX such as matrix, textbfpmatrix, and
textbfbmatrix.

• In a matrix use the & character to denote new columns and the \\ command to start
new rows.

• If you are having trouble creating the dots use the commands \dots, \ddots, and \vdots.

1 \ begin { equation *}
2 \ begin { pmatrix }
3 a_ {11}& a_ {12}&\ dots & a_ {1 n }\\
4 a_ {21}& a_ {22}&\ dots & a_ {2 n }\\
5 \ vdots &\ vdots &\ ddots &\ vdots \\
6 a_ { n1 }& a_ { n2 }&\ dots & a_ { nn }
7 \ end { pmatrix }
8 \ begin { bmatrix }
9 v_ {1}\\
10 v_ {2}\\

7
Figure 5: What about Matrix Equation?

11 \ vdots \\
12 v_ { n }
13 \ end { bmatrix }
14 =
15 \ begin { matrix }
16 w_ {1}\\
17 w_ {2}\\
18 \ vdots \\
19 w_ { n }
20 \ end { matrix }
21 \ end { equation *}
Listing 5: Adding more math: Maxwell’s equations

6 Tables and Figures


For this exercise create a new section to your document for figures and tables. In that section
create a table and a figure. Give each object a caption, try putting the table caption above the
table and the figure caption below. Try this with a figure of your choice.

• To include a caption with your table use the environment table, \begin{table}.

• To create the table itself you need to use the environment tabular, \begin{tabular}{}.

8
– In the second argument of tabular you will define the number of columns in the table,
the justification of each column, as well as if you would like any lines between the
columns. In the table used in this question the command \begin{tabular}{|l||c||r|}
was used. This creates a 4 column matrix, the first column is left justified, l, the
middle two are center justified, c, and the last column is right justified, r. The table
also has borders on the outside as well as between the columns using the | (shift
backslash key). Notice there is a double || after the first column.

• Entering values into your table works much like a matrix. use the & character to separate
columns and the \\ command to start working on a new row. Use $ if you want to write
in math mode.
• If you want to create a horizontal line at the top, bottom, or between rows of your table
use the command \hline at the start of the row and after the last \\.
• Placing the command \centering at the start of the table of figure environment will center
the object.
• For figures you need to include the graphicx package, \usepackage{graphicx}.
• Using the graphicx package you can create an environment figure, \begin{figure}.
• To insert your figure use the command \includegraphics[ ]{}.
– The optional argument of \includegraphics can be used to resize the figure try
[width=\textwidth] and [width=.5textbackslashtextwidth] and see the difference it
makes.
– The required argument of \includegraphics is the name of the file, DO NOT include
the file type and make sure the file is located in the same folder as the LaTeX
document otherwise it is more complicated to include it. For example the argument
for file A21.jpg should just be \includegraphics{A21}.
• When including a floating object in your document such as a figure or a table you can
follow the required argument with an additional optional argument to indicate the pre-
ferred placement of the object. The letter h indicates here (current location), b indicates
bottom (bottom of a page), and t indicated top (top of page). Using an ! stresses to
LaTeX to make this placement. So you may see commands for figures of tables that look
like this \begin{figure}[hbt!].
Practice: Add table and figure as shown in Table 1 and Figure 6.

Table 1: This is a table that shows how to create different lines as well as different justifications

x 1 2 3
f (x) 4 8 12
f(x) 4 8 12

9
Figure 6: A21 building of USTH.

1 \ section { Tables and Figures }


2 Creating a Table is not unlike creating a matrix :
3 \ begin { table }[ hbt !]
4 \ centering
5 \ caption { This is a table that shows how to create different lines as
well as different justifications }
6 \ begin { tabular }{| l || c | c | r |}
7 \ hline
8 $x$ &1&2&3\\
9 \ hline
10 $f ( x ) $ &4&8&12\\
11 f ( x ) &4&8&12\\
12 \ hline
13 \ end { tabular }
14 \ end { table }
15

16 \ begin { figure }
17 \ centering
18 \ includegraphics [ width =0.8\ linewidth ]{ images / A21 . jpeg }
19 \ caption { A21 building of USTH .}
20 \ label { fig : A21 }
21 \ end { figure }
Listing 6: Adding more math: Maxwell’s equations

7 More Equations
A) Trying creating the following calculus equation:

10
B) If you are feeling really good try your luck with these equations (no hints are given):

8 Bibliography
Before attempting this exercise make sure you have read through the BibTex tab of this guide.

11
Create a BibTex file with a few citations in it. Add a sentence that contains these citations
and then add a bibliography to your document.
• It is much easier to create a BibTex file using a citation management tool like Zotero or
Mendeley.
• Use the \cite{} command where the argument is bibID for the citation in the .bib file.
• If you are citing multiple items at the same location you can use \cite{} command and
separate the bibIDs with commas.
• Make sure use the command \bibliographystyle{} to tell LaTeX which bibliographic style
to use.
• Some common styles are plain, ieeetr, acm, and apalike.
• To create your bibliography use the command \biblography{} where the argument is the
name of the bib file. DO NOT include .bib and MAKE SURE the .bib file is located in
the same folder as the LaTeX document.
• You may need to compile your document twice in order to get cross references to work
correctly
Practice: Add this following paragraph into your document, then add a refer-
ence list:

You will probably want references in your document so that you can cite articles like [1,
2, 3, 4, 5].

The reference should look like the one in the figure 7.

Create a BibTex file “bib.bib” in the same folder with “main.tex” and fill the references like
the one below:
1 @inproceedings { frenkel_fine_2013 ,
2 title = { Fine temperature measurement and fabrication of on - chip
whispering - gallery mode micro - sensors } ,
3 url = { http :// proxy . library . nyu . edu / login ? url = http :// search . ebscohost .
com / login . aspx ? direct = true & db = edselc & AN = edselc .2 -52.0 -84901804547& site =
eds - live } ,
4 doi = {10.1115/ MNHMT2013 -22003} ,
5 booktitle = {{ ASME } 2013 4 th { International } { Conference } on { Micro }/{
Nanoscale } { Heat } and { Mass } { Transfer } , { MNHMT } 2013} ,
6 publisher = { American Society of Mechanical Engineers ( ASME ) } ,
7 author = { Frenkel , M . and Avellan , M . and Guo , Z .} ,
8 year = {2013} ,
9 note = { Conference Proceedings }
10 }
Listing 7: bibliography file bib.bib
Continue to finish your bib.bib file with these references:
1 https :// doi . org /10.1115/ HT2013 -17245
2 https :// doi . org /10.1115/ HT2012 -58133
3 DOI 10.1088/0957 -0233/24/7/075103
4 http :// dx . doi . org /10.1002/ polb .24016
Listing 8: References

12
Finally, adding a bibliography into your “main.tex” file:
1 \ section { Bibliography }
2 You will probably want references in your document so that you can cite
articles like \ cite { frenkel_fine_2013 , frenkel_optical_2013 ,
frenkel_temperature_2012 , frenkel_whispering - gallery_2013 , frenkel_ -
chip_2016 }
3 \ bibli ograph ystyle { ieeetr }
4 \ bibliography { bib }
Listing 9: Adding bibliography into main.tex

Figure 7: Bibliography

References
[1] N. Library. Research Guides: Getting Started with LaTeX: Exercises — guides.nyu.edu.
https://guides.nyu.edu/LaTeX/exercises. [Accessed 11-12-2024].

[2] RICE. Latex mathematical symbols. https://www.cmor-faculty.rice.edu/~heinken/


latex/symbols.pdf. [Accessed 11-12-2024].

13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy