VoLuong / Begin-Latex-in-minutes
- вторник, 1 ноября 2016 г. в 03:13:40
708 stars today
Brief Intro to LaTeX for beginners that helps you use LaTeX with ease. Comments and Contributions are welcomed 👍
Inspired by my professor Nghiem Quoc Minh
LaTeX, which is pronounced «Lah-tech» or «Lay-tech» (to rhyme with «blech»), is a document preparation system for high-quality typesetting. It is most often used for medium-to-large technical or scientific documents but it can be used for almost any form of publishing.
LaTeX doesn't come without drawbacks, but is still worth learning.
You will need the following things:
In addition, you need to choose a compiler. The default compiler of most editors is pdfLaTeX, but if you need support for Unicode or TTF/OTF fonts from your system, use LuaLaTeX.
Or you can choose a simple online solution like ShareLaTeX. Please look at Additional Tools for a wider variety of choices.
Let's do the traditional Hello World in LaTeX.
If you have installed TexMaker, first create a new file with ending .tex
. Then type in the following code below to render "Hello World!" and run "quick build"
\documentclass[a4paper]{article}
\begin{document}
Hello World
\end{document}
It should look like this in TexMaker:
main()
in java or C++ ... without which the document can't be rendered.Hello World
) is simply your own content.Some languages won't work right out of the box. You will need to include some packages for the font to render. Also, you will learn about "packages" later. For example:
\documentclass[a4paper]{article}
\usepackage[T5]{fontenc}
\usepackage[utf8]{inputenc}
\begin{document}
Xin chào thế giới. This is Hellow World in Vietnamese.
\end{document}
Here we use two package usepackage[T5]{fontenc}
and usepackage[utf8]{inputenc}
. This is really simple to understand as the package will import font encoders to display your content correctly. If you are using TexMaker this is what the above code display :
vs without the packages
To use TeX with other languages, you have some options. If you use pdfLaTeX, the
default compiler, you are limited to 256 characters and various encoding
issues. However, if you switch your TeX compiler to LuaLaTeX (or XeLaTeX) and
use fontspec
and polyglossia
, Unicode will work out of the box:
\documentclass[a4paper]{article}
\usepackage{fontspec}
\usepackage{polyglossia}
%\setmainfont[]{DejaVu Serif}
\begin{document}
Xin chào thế giới. This is Hello World in Vietnamese.
\end{document}
The default font (Latin Modern) does not support all characters. You can,
however, use almost any font installed on your system by uncommenting the
\setmainfont
line. (TTF and OTF fonts are fully supported).
\section
and a paragraph with \paragraph
.
\subsection
and subparagraph with \subparagraph
\tableofcontents
Example:
\newpage
if you want to make a new page.
It's as easy as pie to use footnote+label+ref to make all kinds of footnotes you want. For example:
Hi let me introduce myself\footnote{\label{myfootnote}Hello footnote}.
... (later on)
I'm referring to myself \ref{myfootnote}.
\newline
to make a new line.
LaTeX offers a lot of functions by default, but in some situations it can come in handy to use so called packages. To import a package in LaTeX, you simply add the \usepackage
Here is an example of using two packages for displaying math:
A practical example
\begin{table}[h!]
\centering
\caption{Caption for the table.}
\label{tab:table1}
\begin{tabular}{l|c||r}
1 & 2 & 3\\
\hline
a & b & c\\
\end{tabular}
\end{table}
Now let's take a closer look
\begin{table}
and \end{table}
.\centering
to keep the table at the center of the page.{l|c||r}
is where we format the content inside the table. Here we can see :
\hline
actually adds a horizontal line to separate each row.\usepackage{booktabs}
for a visually better table.
To add an image to the LaTeX file , you need to use figure environment and the graphicx package. Use \usepackage{graphicx}
and
\begin{figure}
\includegraphics[width=\linewidth]{filename.jpg}
\caption{What is it about?}
\label{fig:whateverlabel}
\end{figure}
\begin{figure}[h!]
Here's how the image is rendered :
One aspect of text compiling that is of the utmost importance to programmers and developers is how to professionally insert codes into the document.
For LaTeX, the process is simple and very professional. We just wrap the code with some predefined content, then we are good to go.
Example :
\documentclass[a4paper]{article}
\begin{document}
Hello world!
\begin{verbatim}
#include <iostream>
int main()
{
std::cout << "hello world!\n";
return 0;
}
\end{verbatim}
\end{document}
As you can see, with the {verbatim} wrapper you can easily insert code without worrying about how the syntax is formatted. Here is how it looks out of the box, clean and professional :
This method gives you more options, including insert code inline, make custom styles code, choose a specific language for code, import code from another file within the same directory.... With this method, you dont use {verbatim} , but include a package
Consider the following example :
\documentclass[a4paper]{article}
\usepackage{listings}
\usepackage{color}
\lstdefinestyle{mystyle}{
keywordstyle=\color{magenta},
backgroundcolor=\color{yellow},
commentstyle=\color{green},
basicstyle=\footnotesize,
}
\lstset{style=mystyle}
\begin{document}
Hello world!
\begin{lstlisting}[language=Python]
print "Hello World!"
\end{lstlisting}
\lstinputlisting[language=C++]{hello.cpp}
Lorem ipsum dolor sit amet \lstinline{print "Hello World"} , consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{document}
From this, you can see:
\begin{lstlisting}
and end with \end{lstlisting}
lstinputlisiting{name_of_file}
[language=C++]
\lstinline
\usepackage{color}
and define your own style then define the listing with your own theme (Please look at code below). You can modify many things with your own style, but you need to read the doc for the correct property name.Here is how the code above compiles in TexMaker :
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Copyright (C) 2016 Luong Vo Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed. TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION : You just DO WHAT THE FUCK YOU WANT TO.