New to LaTex based on Overleaf
Writing your first piece of LATEX
\documentclass{article}
\begin{document}
First document. This is a simple example, with no extra parameters or packages included.
\end{document}
The preamble of a document
preamble means NOT the main body of the document.
Declare the type of document
\documentclass[font_size, paper_size]{the type of document}
- font_size: default 10pt
- paper_size: a4paper, legalpaper...
- the type of document: article, report...
Here is the example.
\documentclass[12pt, a4paper]{article}
more info:https://www.overleaf.com/learn/Page_size_and_margins
Declare the encoding
\usepackage[utf8]{inputenc}
Here to recommend utf-8.
Adding a title, author and date
Add three lines to the preamble
Title
\title{Title_name}
Author
\author{Author_name}
or \author{Author_name \thanks{funded by ...}}
It will add a superscript and a footnote with the text inside the braces. Useful if you need to thank an institution in your article.
Date
\date{Month Day Year}
or \date{Month Year}
or\today
Here is the example.
\documentclass[12pt, letterpaper, twoside]{article}
\usepackage[utf8]{inputenc}
\title{First document}
\author{Hubert Farnsworth \thanks{funded by the ShareLaTeX team}}
\date{\today}
\begin{document}
\maketitle
We have now added a title, author and date to our first \LaTeX{} document!
\end{document}
Adding comments
Comments are pieces of text you can include in the document which will not be printed, and will not affect the document in any way.
-
One-line comments
add**
%
** symbol at the beginning of the line
\begin{document}
\maketitle
We have now added a title, author and date to our first \LaTeX{} document!
% This line here is a comment. It will not be printed in the document.
\end{document}
- Multi-line comments
% first method
\iffalse
This line here is a comment. It will not be printed in the document.
\fi
% second method
% need to use a package
\usepackage{verbatim}
\begin{comment}
here
are
comments
\end{comment}
Bold, italics and underlining
- Bold:
\textbf{...}
- Italics:
\textit{...}
- Underline:
\underline{...}
% Example
Some of the \textbf{greatest}
discoveries in \underline{science}
were made by \textbf{\textit{accident}}.
- handling depends on the context:
\emph{...}
Some of the greatest \emph{discoveries}
in science
were made by accident.
\textit{Some of the greatest \emph{discoveries}
in science
were made by accident.}
\textbf{Some of the greatest \emph{discoveries}
in science
were made by accident.}
Adding images
Need to use a 「graphicx」 package \usepackage{graphicx}
and Write in preamble.
Here are 2 methods to add images
\graphicspath{ {images/} }
, it needs to upload the images, and the images are kept in a folder named images under the current directory.\includegraphics{image_name}
, the image is actually included in the document. The file name of the image should not contain white spaces nor multiple dots.
For more details see the section about generating high resolution and low resolution images.
Captions, labels and references
There are three important commands :
\caption{a nice plot}
: set the caption for the figure and place it above or below the figure.\label{fig:mesh1}
: refer the image within your document, set a label with this command. The label will number the image, and combined with the next command will allow you to reference it.\ref{fig:mesh1}
: substituted by the number corresponding to the referenced figure.
Here is the example.
\documentclass{article}
\usepackage{graphicx}
\graphicspath{ {images/} }
\begin{document}
\begin{figure}[h]
\centering
\includegraphics[width=0.25\textwidth]{mesh.png}
\caption{a nice plot}
\label{fig:mesh1}
\end{figure}
As you can see in the figure \ref{fig:mesh1}, the
function grows near 0. Also, in the page \pageref{fig:mesh1}
is the same example.
\end{document}
Note: If you are using captions and references on your own computer, you will have to compile the document twice for the references to work. Overleaf will do this for you automatically.
Creating lists in LATEX
Lists are very simple to create in LATEX. You can create lists using different list environments. Environments are sections of our document that you want to present in a different way to the rest of the document. They start with a \begin{...}
command and end with an \end{...}
command.
Unordered lists
Environment: itemize
Each entry must be preceded by the control sequence \item
.
Here is the example.
\begin{itemize}
\item One entry in the list
\item Another entry in the list
\end{itemize}
Ordered lists
Environment: enumerate
Have the same syntax inside a different environment.
\begin{enumerate}
\item The labels consists of sequential numbers.
\item The numbers starts at 1 with every call to the enumerate environment.
\end{enumerate}
Adding math to LATEX
2 writing modes for mathematical expressions:
- the inline mode
\( ... \)
$ ... $
\begin{math} ... \end{math}
- the display mode (numbered & unnumbered version)
\[ ... \]
→unnumbered\begin{displaymath} ... \end{displaymath}
→unnumbered\begin{equation} ... \end{equation}
→numbered$$ ... $$
(NOT encouraged)
Basic Formatting
Abstracts
The abstract environment will put the text in a special format at the top of your document.
Here is the example.
\begin{abstract}
This is a simple paragraph at the beginning of the document. A brief introduction about the main subject.
\end{abstract}
Paragraphs and newlines
- Start a new paragraph
- hit the "Enter" key twice (to insert a double blank line).
- LATEX can automatically indents paragraphs.
- Start a new line
\\
\newline
- Don't use multiple
\\
or\newline
to simulate with larger spacing between paragraphs
Chapters and Sections
start with \chapter{chapter_name}
start with \section{section_name}
(section numbering is automatic)
use \section*{section_name}
to disable automatically numbering
The basic levels of depth are listed below:
- -1|**
\part{part}
→**available in report and book document classes - 0|**
\chapter{chapter}
→**available in report and book document classes - 1|**
\section{section}
** - 2|**
\subsection{subsection}
** - 3|**
\subsubsection{subsubsection}
** - 4|**
\paragraph{paragraph}
** - 5|**
\subparagraph{subparagraph}
**
Creating tables
Creating a simple table in LATEX
- Environment: tabular
- Specify a parameter to env to declare the number of columns**
{c c c }
** - Align
r
→ right alignmentl
→ left alignment- default center alignment
- Symbol
&
→ specify the breaks in the table entries\\
→ go to the next line of your table
Here is the example.
\begin{tabular}{ c c c }
cell1 & cell2 & cell3 \\
cell4 & cell5 & cell6 \\
cell7 & cell8 & cell9
\end{tabular}
ps: not in the center of the page but left
Adding borders
|
: insert ****a vertical line{ |c|c|c| }
\hline
: insert a horizontal line.
Here is the example.
\begin{tabular}{ | c | c | c | }
\hline
cell1 & cell2 & cell3 \\
cell4 & cell5 & cell6 \\
cell7 & cell8 & cell9 \\
\hline
\end{tabular}
Captions, labels and references
- the same way as images
- Environment: table
Here is the example.
%Referencing and captioning tables
The table \ref{table:1} is an example of referenced \LaTeX{} elements.
\begin{table}[h!]
\centering
\begin{tabular}{||c c c c||}
\hline
Col1 & Col2 & Col2 & Col3 \\ [0.5ex]
\hline\hline
1 & 6 & 87837 & 787 \\
2 & 7 & 78 & 5415 \\
3 & 545 & 778 & 7507 \\
4 & 545 & 18744 & 7560 \\
5 & 88 & 788 & 6344 \\ [1ex]
\hline
\end{tabular}
\caption{Table to test captions and labels}
\label{table:1}
\end{table}
Adding a Table of Contents
**\tabelofcontents**
Here is the example.
\documentclass{article}
\usepackage[utf8]{inputenc}
\title{Sections and Chapters}
\author{Gubert Farnsworth}
\date{ }
\begin{document}
\maketitle
\tableofcontents
\section{Introduction}
This is the first section.
Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Etiam lobortisfacilisis sem. Nullam nec mi et
neque pharetra sollicitudin. Praesent imperdietmi nec ante.
Donec ullamcorper, felis non sodales...
\section*{Unnumbered Section}
\addcontentsline{toc}{section}{Unnumbered Section}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis facilisissem. Nullam nec mi et neque pharetra
sollicitudin. Praesent imperdiet mi necante...
\section{Second Section}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis facilisissem. Nullam nec mi et neque pharetra
sollicitudin. Praesent imperdiet mi necante...
\end{document}