Getting Started With Latex Programming: Resources And Best Practices
1. Installing a LaTeX Distribution
Before you can start creating LaTeX documents, you need to install a LaTeX distribution on your computer. Some popular distributions used by millions of academics, scientists, and technical writers include MiKTeX (Windows), MacTeX (macOS), and TeX Live (Windows, Mac, Linux). These bundles contain all the tools you need to write, format, and render LaTeX files.
Overview of Popular LaTeX Distributions
MiKTeX - An easy-to-install distribution for Windows that integrates well with open-source editors. It can download missing packages automatically over the internet during compilation. The basic MiKTeX suite occupies ~3GB disk space.
TeX Live - A cross-platform distribution that works on Windows, macOS and Linux. Its upstream packages give it access to newest LaTeX core libraries. Expect the full install to take up ~6GB disk space.
MacTeX - A comprehensive TeX Live edition tailored for macOS. The GUI installers make setting it up seamless on your Mac. Account for at least ~4.5GB free disk space for full MacTeX.
Downloading and Installing a Distribution
The exact install method varies by platform, but overall the process is straightforward. Downloads are available from:
- MiKTeX - https://miktex.org/download
- TeX Live - https://www.tug.org/texlive/acquire-netinstall.html
- MacTeX - https://www.tug.org/mactex/
On Windows double click the exe installer for MiKTeX or TeX Live to launch the setup wizard that handles everything automatically. It may take 30-60mins to fully install all packages if you chose the complete bundle.
On macOS use the MacTeX DMG installer which also guides you through the install process seamlessly in under 10 minutes. TeX Live can also be manually installed but takes more effort.
On Linux the TeX Live net installer is handy for installing from a terminal/shell prompt. Or you can use the package manager for your specific distro - apt for Ubuntu/Debian, pacman for Arch, etc.
Verifying Installation
To confirm LaTeX is properly set up on your system, open up a text editor like Notepad++, Visual Studio Code, Atom etc. Paste this basic document template:
\documentclass{article} \begin{document} Hello World! \end{document}
Save the file with a .tex extension. Then compile it either via the editor's Build option, or by running pdflatex filename.tex
from the terminal. This should successfully generate a PDF file with the "Hello World!" text. Install any missing packages prompted during compilation to complete the setup.
2. Understanding the Basic LaTeX Structure
LaTeX documents have a simple underlying framework you should recognize before writing complex files. Let's break down the key components:
The Document Class
Every LaTeX file starts by defining its document class - article, book, report etc. This specifies parameters like default font size, margins, spacing, as well as built-in structural elements.
\documentclass[options]{class}
For example, \documentclass[12pt, letterpaper]{article} applies 12pt font and letter sized paper to the article class. Common document classes include:
- article - For short documents like academic papers.
- report - Has chapters and more structural units compared to article.
- book - For actual books with parts, chapters and front/back matter.
- letter - For writing letters.
- beamer - For presentations.
The Preamble vs Document Body
The preamble contains the \documentclass line, followed by important \usepackage declarations that import handy LaTeX packages. For example, \usepackage{graphicx} enables embedded images.
The actual content comes in the document body, contained within \begin{document} and \end{document} tags. This is where your text, figures and formatting commands go. The preamble and body together form the logical structure.
Common Packages to Import
Here are some frequently used packages worth importing in the preamble with \usepackage:
- graphicx - Enables embedding images
- amsmath - Advanced math typesetting
- fancyhdr - Custom headers/footers
- fontenc - Additional font support
- geometry - Paper size and margin customization
Refer to over 7000 packages at https://www.ctan.org/pkg as needed.
Delimiting Document Body
The document content must go within the \begin{document} and \end{document} tags like:
\begin{document} Actual content goes here... \end{document}
3. Creating Your First LaTeX File
Now that you grasp LaTeX structure basics, let's discuss writing and compiling LaTeX files:
Using a Text Editor
You need a text editor like Notepad++, Atom, Sublime Text, Visual Studio Code etc for coding LaTeX files. They provide helpful tools like syntax highlighting, smart autocompletion, integrated PDF previews and more.
XLS and Word processors like Excel and MS Word won't work because LaTeX isn't WYSIWYG. The tex source contains code that gets transformed into publication-ready documents after compilation.
Compiling LaTeX Source Files
The TeX engines convert .tex files to formatted output like PDF. Common engines include pdfTeX (to directly output PDF) and XeTeX (better Unicode support). Use their respective commands for compiling:
pdflatex myfile.tex xelatex myfile.tex
This generates the formatted PDF from source. recompile after updating source to refresh the output.
Fixing Compilation Errors
LaTeX will highlight any compilation issues like undefined environments, missing packages etc. Fix these errors and recompile. Key things to resolve errors:
- Install missing packages with LaTeX package manager
- Check for missing braces {} or delimiters
- Fix invalid syntax, spell corrections etc
- Delete temporary files to rebuild from scratch
4. Formatting Text in LaTeX
LaTeX offers extensive options for styling text with logical markup and tags. Let's check some examples:
Paragraphs and Sectioning
Logical markup like \section, \subsection etc. structure long documents and outline hierarchy:
\section{Introduction} ...content here... \subsection{Background} ...subsection content...
Use blank lines to separate paragraphs instead of indentation. Line breaks \\ force newline.
Text Styling Tags
Embed inline formatting as follows:
\textbf{bold text} \textit{italics text} \underline{underlined text} \texttt{monospaced text}
Use {\color{red} red text } for color. Change font with {\fontfamily{Times New Roman}\selectfont text}.
Lists and Itemizations
Numbered lists:
\begin{enumerate} \item First item \item Second item \end{enumerate}
Bullet lists:
\begin{itemize} \item Bullet one \item Bullet two \end{itemize}
The enumerate and itemize environments enable lists.
Footnotes and Margin Notes
Footnotes[^1] appear at page bottom:
\footnote{Footnote text goes here}
Margin notes[^note] are in the page margin:
\marginpar{Margin note text goes here}
5. Inserting Tables and Figures
Visuals in LaTeX require the graphicx package. Then you can:
Import External Image Files
Embed external graphics like JPEG, PNG images that aren't native LaTeX:
\includegraphics[width=5cm]{figure.jpeg}
Position and Label Figures
Use footnotes to list and \label plots, diagrams for further reference:
\begin{figure}[h!] \centering \includegraphics[width=10cm]{graph.png} \caption{Sales Growth Chart}\label{fig1} \end{figure}
Cite plots from text via \ref{fig1}. The [h!] argument keeps the figure strictly here in paragraph.
Creating Basic Tables
The tabular environment constructs LaTeX tables:
\begin{tabular}{lll} \textbf{Treatments} & \textbf{Method} & \textbf{Side-effects}\\ A & Improved care & Minimal \\ B & Palliative & Frequent \\ C & Alternative & Unknown \\ \end{tabular}
The {lll} columns argument formats three left-aligned columns. See documentation for advanced multi-row/column tables.
Customizing Captions and Referencing
Table captions and automated numbering \label format just like figures. Refer to them from text using \ref commands.
\begin{table}[h!] \caption{Results Summary}\label{tab2} ..... \end{table} The findings in \ref{tab2} indicate...
This concludes our Getting Started LaTeX guide. Hope you found it helpful! Let the TeX power enhance all your documents...