Gediminas Lelešius

Typesetting in LaTeX (Eng & Lt)

2021-12-13, U: 2022-05-22

Short guide how to prepare Computer Science or Physics homework using \LaTeX.

Setup

Windows: install MiKTeX
Linux: install texlive or similar using your packet manager.

User TeXWorks or text editor to edit .tex files.

Compile using pdflatex --shell-escape --output-directory=out --aux-directory=out file.tex to build. Last two options put all auxiliary and output files to out folder.

Quick previews can be generated using LaTeX JS Playground

Getting started

Just write text between \begin{document} and \end{document}.
Write comments (will not be outputed to pdf) after % (until end line).
Multiple spaces are outputed as one, new line is outputed as space.
Empty line starts new paragraph. Insert line break with \\.

Sections

Start sections with \section{section title}.
Further sectioning:

\section{section title}
\subsection{subsection}
\subsubsection{subsubsection}
\paragraph{paragraph}

To add a (sub)section without a number, use \section*{title}, \subsection*{title}, etc.

Math

Inline math: $x^{2.2} = a_1$ x^{2.2} = a_1.
Math in new paragraph: $$ \sum_{i=0}^n i = \dfrac{n(n+1)}{2}$$.

\sum_{i=0}^n i = \dfrac{n(n+1)}{2}

Quotation Marks

English: `` (backticks) '' (single quotes)

Lithuanian: ,, (commas) `` (backticks)
Alternatively, you can use " everywhere. Add this to preamble (Lithuanian and german quotes are the same):

\usepackage[style=german,german=quotes]{csquotes}
\MakeOuterQuote{"}

Dashes

About dashes - VLKK. I believe rules are similar in British English.

Hyphen (brūkšnelis) -: used between number and suffix (e.g. 3-iąją), between two words that describe a name of some thing or feature (lopšelis-darželis). Should NOT be surounded by spaces.
En-dash (brūkšnys) --: used to show ranges (e.g. 100--150), as a punctuation mark (ruduo -- blogiausias metų laikas). Should be surounded by spaces.
Em-dash (skyrybos brūkšnys) ---: not used in Lithuanian.

Spaces

~ inserts non-breaking space.
\;, \:, \, spaces from wider to narrower.
\[SPACE] forces space to be normal. Useful after a dot because LaTeX assumes that all dots end sentences and makes following spaces larger: e.g.\ banana.

Basics

Fonts

Italics: \textit{text} text
Bold: \textbf{text} text
Small caps: \textsc{text}
Monospace: \texttt{text} text

Lists

Indexed:

\begin{enumerate}
    \item First
    \item Second
\end{enumerate}
  1. First
  2. Second

Bullet points:

\begin{itemize}
    \item Point
    \item Point
\end{itemize}
  • Point
  • Point

Custom indexed:

\begin{enumerate}[label=\alph*] % a b c
\begin{enumerate}[label=\roman*)] % i) ii) iii)
\begin{enumerate}[label=\textit{(\Alph*)}] % (A) (B) (B) in italics

Aligned math

Aligns & symbols (they aren't displayed).

\begin{align*}
    A &= \sum_i b_i\\
    &= c+d+e+\sin{x}\\
    \sin{\prod_{j} y_j^{10}}&<10
\end{align*}
\begin{align*} A &= \sum_i b_i\\ &= c+d+e+\sin x\\ \sin{\prod_{j} y_j^{10}}&<10 \end{align*}

URLs

Insert urls with \url{https://example.com}.

Figures

Inline: \includegraphics[width=5cm]{FILENAME}

On its own ([H] inserts in the same place as in source, remove it to allow LaTeX to pick the location automatically, width can be in cm, fraction 0.5\textwidth, etc.):

\begin{figure}[H]
    \centering
    \includegraphics[width=\textwidth]{FILENAME}
    \caption{CAPTION}\label{LABEL}
\end{figure}

Subfigures

\begin{figure}
    \centering
    \begin{subfigure}{0.5\textwidth}
        \includegraphics[width=\textwidth]{FILE A}
        \caption{CAPTION A}
    \end{subfigure}
    \begin{subfigure}{0.5\textwidth}
        \includegraphics[width=\textwidth]{FILE B}
        \caption{CAPTION B}
    \end{subfigure}
    \caption{CAPTION}
\end{figure}

Wrap Figures

Wraps text around the figure. I don't recommend using unless absolutely needed. Ignores page margins, so check position carefully.
Side should be left or right.

\begin{wrapfigure}{SIDE}{0.5\textwidth}
    \includegraphics[width=0.5\textwidth]{FILENAME}
\end{wrapfigure}

Highlighted code

Install Python3, run pip install Pygments. Embed code using \inputminted{LANGUAGE}{CODEFILE} or

\begin{minted}[tabsize=4]{ocaml}
let knot (f: (int -> int) -> int -> int) : int -> int =
    let r = ref (fun n -> 0) in
    let recur = fun n -> !r n in
    let () = r := fun n -> f recur n in
    recur
\end{minted}

Tables

\begin{tabular}{l c r}
    A & B & C \\
    D & E & F \\
    left & center & right \\
\end{tabular}
A B C
D E F
left center right

l c r specify that there will be three columns aligned left, center, and right respectively. Spaces indicate that columns will be separated by spaces.
c|c: two centered columns separated with single border line.
& separate columns, \\ end rows.

\begin{table}[H]
\begin{tabular}{|c|c|}
    \hline
    \textbf{text text} & \textbf{more text} \\ \hline
    $x^2$ & E \\ \hline
\end{tabular}
\caption{CAPTION}
\end{table}
text text more text
x^2 E

table works similarly to figure.
\hline adds horizontal lines, in this case before the first row, after the first row and after the second row.

Multiple Files

You can move part of the tex file to another tex file and include it using \input{filename.tex}. That will simply paste contents of filename.tex.

References

Create labels to (sub)sections, enumerate items, figures, tables, etc. using \label{UNIQUE ID} after \section, \item, or \caption command.

\section{My section}\label{mylabel}

Footnotes

Insert a footnote by using \footnote{text}.
Alternatively, mark footnote position using \footnotemark{} and add text later (but before next footnote mark) with \footnotetext{text}.

Table of Contents

\tableofcontents{}, \listoffigures{}, \listoftables{}.

Multiple Columns

You can have some part of the document in multiple columns by using multicols environment:

\begin{multicols}{COUNT}
  BLABLA
\end{multicols}

Useful Resources

Find math symbol name (Detexify)
Latex word count (TeXcount)
Table generator

Template

% template.tex
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
% Lithuanian
\usepackage[lithuanian]{babel}
\usepackage[style=german,german=quotes]{csquotes}
\MakeOuterQuote{"}
% recomended
\usepackage{hyperref}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{multicol}
% \usepackage[sorting=none]{biblatex}

Main document

\documentclass[12pt,a4paper]{article}
% preamble
\input{template}
% preamble ends
\begin{document}

%% YOUR WORK

\end{document}

Explanations:

Document Class

article is good for small to medium documents.
For larger ones you might use report (has \chapter, \appendix).
You can also create presentations using beamer.

UTF-8

Should be used no matter the language.

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

Headers

Table, Picture, Table of Contents, Biobliography and others are in English by default.

\usepackage[lithuanian]{babel}

Common Mistakes

Copying text from PDF and pasting results in unknown symbol errors.
Solution: replace symbols to proper ones or add to preamble: \DeclareUnicodeCharacter{symbolcode}{latexsymbol} where symbolcode is 4 digit symbol code and latexsymbol is latex symbol to typeset.

BibTeX error when using TeXWorks and no \cite is used.
Solution: use \nocite{*} or disable BibTeX.

Citation references not found.
Possible cause: forgot to rerun BibTeX.

Error inside align: ! Paragraph ended before \align* was complete. <to be read again> \par.
Cause: empty line inside align (empty lines are replaced by \par).
Remove empty line. Line breaks are insert using \\.

Other

Bibliography

Use biblatex package.
Create filename.bib file, add references here.
Use file by adding \addbibresource{filename.bib} to preamble.
Cite items using \cite{REFNAME}.
Insert bibliography section: \printbibliography{}

For urls use:

@misc{REFNAME,
  author = {{AUTHOR}},
  title   = {TITLE},
  howpublished = "\url{URL}",
  note = "Visited on DATE"
}

Spell checking

Install pandoc.

First option: convert to docx using pandoc filename.tex -t docx -o filename.docx.
Use Word spell checker.

Second option: Create Lua filter file filter.lua with code given below (or remove --lua-filter argument).
Convert to text using pandoc filename.tex -t markdown_github --wrap=none --lua-filter filter.lua -o filename.txt.
Use any text spell checker.

function Image () return {} end
function Link (link) return link.content end

local function to_remove(el)
    return el and (el.t == 'Cite' or el.t == 'Image' or el.t == 'Math' or el.t == 'Span')
end

function Inlines (inlines)
    for i = #inlines, 1, -1 do
        if to_remove(inlines[i]) then
          inlines:remove(i)
        end
    end
    return inlines
end

Tracking ToDos

Add this to preamble.

%TC:macro \pending 1
%TC:ignore
\newwrite\listofpending
\immediate\openout\listofpending=\jobname.pending
\newcounter{pendingcounter}

\newcommand{\pendingonline}[3]{\item\textbf{\hyperref[todo:#1]{#1} on #2}: #3}

\def\pending#1{\refstepcounter{pendingcounter}%
{\color{red}#1}\label{todo:\thependingcounter}%
\immediate\write\listofpending{%
\string\pendingonline{\thependingcounter}{\currfilename{}:\the\inputlineno}{#1}}}

\def\pendinglist{{\color{red}\section*{TODOs}\addcontentsline{toc}{section}{\textcolor{red}{TODOs}}}%
{\color{red}%
\immediate\closeout\listofpending%
\begin{itemize*}%
  \input{\jobname.pending}%
\end{itemize*}%
}}

Use \pending{text} to insert ToDo items in text, \pendinglist{} to insert section of all ToDos.

List with referencable names

\newlist{refitemize}{enumerate}{1}
\setlist[refitemize]{label=\refitemval:~, ref=\refitemval, wide=0cm}
\makeatletter
\let\refitemval\relax
\newcommand{\refitem}[1]{%
    \def\refitemval{#1}%
    \item\relax
  }
\makeatother

Create a list with \begin{refitemize}\end{refitemize}.
Insert elements with \item{Item name}\label{LABEL} text.
Reference items later with \ref{LABEL}. Item name will be shown as reference.

Missing:

  • page layout
  • title
  • page numbering style
  • custom commands