Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 38 ยท Projects and Practice

LaTeX Practice Exercises: References, Packages, TikZ, Beamer

Test your LaTeX skills with graded exercises on cross-references, bibliographies, macros, TikZ, Beamer and debugging, each with a worked solution.

You learn LaTeX by writing it and by fixing it when it breaks. These exercises cover the second half of the course: cross-references, bibliographies, packages, macros, layout, TikZ, Beamer and builds. Try each one before opening the solution. Compile real files in your own editor or on Overleaf, because the compiler's messages are the best teacher.

Exercises are graded: (A) warm-up, (B) standard, (C) challenge.

Part 1: cross-references and bibliographies

1.1 (A) The following figure is referenced as "Figure 2" although it is the first figure. What is wrong, and how do you fix it?

LATEX
\section{Results}
\begin{figure}
  \centering
  \includegraphics[width=0.7\textwidth]{plot.pdf}
  \label{fig:plot}
  \caption{Measured response.}
\end{figure}

Solution: the label is before the caption, so it records the section counter (here 2 if this is the second section). Put \label{fig:plot} after \caption{...}. See labels and cross-references.

1.2 (A) A document shows "(?)" for every citation after one compile with BibTeX. List the steps that fix it.

Solution: run pdflatex, then bibtex main, then pdflatex twice (or latexmk -pdf). The first run writes the .aux, BibTeX writes the .bbl, the later runs read it.

1.3 (B) Convert a document from natbib with plainnat to biblatex with author-year style. What changes in the preamble and at the end?

Solution:

LATEX
% before
\usepackage[round]{natbib}
...
\bibliographystyle{plainnat}
\bibliography{references}

% after
\usepackage[backend=biber, style=authoryear]{biblatex}
\addbibresource{references.bib}
...
\printbibliography

\citep becomes \parencite (or \autocite) and \citet becomes \textcite. Build with Biber. See biblatex and Biber.

1.4 (C) Write a glossary entry and an acronym, use both, and list the build commands. Hint: the glossaries package and makeglossaries; the solution is in table of contents, index and glossaries.

Part 2: packages and macros

2.1 (A) You see "Option clash for package xcolor". Give two ways to fix it.

Solution: load xcolor with the options before the package that loads it implicitly, or write \PassOptionsToPackage{dvipsnames}{xcolor} before that package (using packages).

2.2 (B) Define \vect{x} (bold), \norm{x} (with automatically sized bars) and an operator \argmin.

Solution:

LATEX
\newcommand{\vect}[1]{\mathbf{#1}}
\newcommand{\norm}[1]{\left\lVert #1 \right\rVert}
\DeclareMathOperator*{\argmin}{arg\,min}

2.3 (B) Create an environment note that prints "Note." in bold, then the content in italics, with space above and below.

Solution:

LATEX
\newenvironment{note}
  {\par\medskip\noindent\textbf{Note.}\ \itshape}
  {\par\medskip}

2.4 (C) Make a header with the chapter name on even pages and the section name on odd pages, and the page number on the outside, in a book.

Solution: \fancyhead[LE,RO]{\thepage}, \fancyhead[RE]{\nouppercase{\leftmark}}, \fancyhead[LO]{\nouppercase{\rightmark}} (layout and headers).

Part 3: TikZ

3.1 (A) Draw a square of side 2 with a diagonal in red.

Solution:

LATEX
\begin{tikzpicture}
  \draw (0,0) rectangle (2,2);
  \draw[red, thick] (0,0) -- (2,2);
\end{tikzpicture}

3.2 (B) Draw a three-step flowchart "Start", "Process", "End" with arrows, using the positioning library.

Solution:

LATEX
\usetikzlibrary{positioning, arrows.meta}
\begin{tikzpicture}[box/.style={draw, rounded corners, minimum width=2cm}, >=Stealth]
  \node[box] (s) {Start};
  \node[box, right=of s] (p) {Process};
  \node[box, right=of p] (e) {End};
  \draw[->] (s) -- (p);
  \draw[->] (p) -- (e);
\end{tikzpicture}

3.3 (B) Using \foreach, draw twelve tick marks around a circle like a clock face.

Solution:

LATEX
\draw (0,0) circle (2);
\foreach \a in {0,30,...,330}
  \draw ({1.8*cos(\a)},{1.8*sin(\a)}) -- ({2*cos(\a)},{2*sin(\a)});

3.4 (C) Plot $f(x) = x^3 - 3x$ on $[-2.5, 2.5]$ with PGFPlots, mark the zeros and add a legend. Hint: domain, samples, \addplot and \addlegendentry; check pgfplots.

Part 4: Beamer

4.1 (A) Make a frame with three bullets that appear one at a time.

Solution: \begin{itemize}[<+->] ... \end{itemize} or \pause after each item (Beamer overlays).

4.2 (B) A frame containing lstlisting produces an error. What option does it need?

Solution: \begin{frame}[fragile] (Beamer basics).

4.3 (C) Produce the slides and a four-per-page handout from the same source.

Solution: compile with the class option handout and \usepackage{pgfpages}\pgfpagesuselayout{4 on 1}[a4paper,landscape,border shrink=5mm]; use \mode<handout>{...} for handout-only content.

Part 5: debugging

Find the errors in the snippets below. The stand-in is a mini-linter that reports unbalanced braces, mismatched environments and unescaped special characters. Try it on the snippets, and add your own.

JS Mini LaTeX linter

Press Run (or Ctrl + Enter) to see the console output here.

No output.

5.1 (A) Why does file_name cause "Missing $ inserted"? Solution: _ is a subscript operator in math. Write file\_name or \texttt{file\_name}.

5.2 (A) The text "50% of R&D" loses everything after the percent sign and gives an alignment error. Solution: write 50\% of R\&D.

5.3 (B) After renaming a section the table of contents still shows the old name. Solution: compile again (the .toc file is read on the next run) or run latexmk (engines and latexmk).

5.4 (B) A paper's references list is empty with biblatex, but there is no error. Solution: check that \printbibliography is present, \addbibresource has the right file name with extension, there is at least one \cite or \nocite{*}, and that Biber ran.

5.5 (C) A document that compiled yesterday fails on a colleague's computer with "File 'siunitx.sty' not found". Give three possible fixes. Solution: install the package (tlmgr install siunitx or MiKTeX auto-install), update the TeX distribution, or compile on Overleaf; record packages with \listfiles and put a build note in the README.

Self-check: what to do next

If you solved at least ten of these without hints, you can write a paper, a thesis and a talk in LaTeX. Good next steps: write a real document in the area you know best, try an actual journal template, join a LaTeX community Q&A site and answer questions, and read one package manual a week (texdoc).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A citation shows [?] after one compile with BibTeX. What is the most likely missing step?

    The citation data goes from .aux through BibTeX to .bbl and is read in later LaTeX runs.
  2. 2. Which frame option allows verbatim code in Beamer?

    fragile lets Beamer read verbatim content in a frame.
  3. 3. What does \foreach \a in {0,30,...,330} produce?

    The dots continue the step (30) up to the last value, giving twelve angles.
  4. 4. How do you write 50% safely in LaTeX text?

    An unescaped percent sign starts a comment, so it must be written %.

Summary

  • Most reference problems come from label placement and missing build runs.
  • Option clashes and missing packages are solved by loading order, \PassOptionsToPackage or installation.
  • TikZ, PGFPlots and Beamer reward small, tested steps and reusable styles.
  • Debug by reading the first error, checking braces, environments and special characters.

Next: Back to the LaTeX course overview