A report template for laboration held at the division of Statistics and Machine Learning at Linköping University.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Template for lab reports used at STIMA
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Sets the document class for the document
% Openany is added to remove the book style of starting every new chapter on an odd page (not needed for reports)
\documentclass[10pt,swedish, openany]{book}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Loading packages that alter the style
\usepackage[]{graphicx}
\usepackage[]{color}
\usepackage{alltt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}
\setlength{\parskip}{\smallskipamount}
\setlength{\parindent}{0pt}
% Set page margins
\usepackage[top=100pt,bottom=100pt,left=68pt,right=66pt]{geometry}
% Package used for placeholder text
\usepackage{lipsum}
% Prevents LaTeX from filling out a page to the bottom
\raggedbottom
% Adding both languages, Swedish and English, so they can be used intermittently in for example abstracts.
\usepackage[swedish, english]{babel}
% All page numbers positioned at the bottom of the page
\usepackage{fancyhdr}
\fancyhf{} % clear all header and footers
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0pt} % remove the header rule
\pagestyle{fancy}
% Changes the style of chapter headings
\usepackage{titlesec}
\titleformat{\chapter}
{\normalfont\LARGE\bfseries}{\thechapter.}{1em}{}
% Change distance between chapter header and text
\titlespacing{\chapter}{0pt}{50pt}{2\baselineskip}
% Adds table captions above the table per default
\usepackage{float}
\floatstyle{plaintop}
\restylefloat{table}
% Adds space between caption and table
\usepackage[tableposition=top]{caption}
% Adds hyperlinks to references and ToC
\usepackage{hyperref}
\hypersetup{hidelinks,
linkcolor = blue} % Changes the link color to black and hides the hideous red border that usually is created
% If multiple images are to be added, a folder (path) with all the images can be added here
\graphicspath{ {images/} }
% Separates the first part of the report/thesis in Roman numerals
\frontmatter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Starts the document
\begin{document}
%%% Selects the language to be used for the first couple of pages
\selectlanguage{english}
%%%%% Adds the title page
\begin{titlepage}
\clearpage\thispagestyle{empty}
\centering
\vspace{2cm}
% Titles
{\large Laboration report in Statistics \par}
\vspace{4cm}
{\Huge \textbf{Laboration X}} \\
\vspace{1cm}
{\large \textbf{Course code} \par}
\vspace{4cm}
{\normalsize Author1 Name \\ % \\ specifies a new line
Author2 Name \par}
\vspace{2cm}
\includegraphics[scale=0.75]{liu_logo.png}
\vspace{2cm}
% Information about the University
{\normalsize Division of Statistics and Machine Learning \\
Department of Computer Science \\
Linköping University \par}
% Set the date
{\normalsize XX-XX-20XX \par}
\vspace{2cm}
\pagebreak
\end{titlepage}
% Adds a table of contents
\tableofcontents{}
\clearpage
% Uncomment the following three rows for a table of figures and/or tables as they are not needed for lab reports
% \listoffigures
% \clearpage
% \listoftables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% The rows above should not be changed except for the title page information
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Text body starts here!
\mainmatter
\chapter{Introduction}
Introduce the laboration; the data set used, the overall goal of the exercises and add any preparatory work, for instance reading data sets and loading packages. Any output should be hidden unless specifically asked for.
% Start of R-code chunk where preparatory work can be conducted, for example loading packages and/or data.
% include = FALSE specifies that nothing will be included in the report, but the code will still run
<<include = FALSE>>=
@
\chapter{Exercises}
% A * after the section/chapter command indicates an unnumbered header which will not be added to the table of contents
\section*{Example of knitR}
% In order to add an unnumbered section/chapter the following code needs to be used
\addcontentsline{toc}{section}{Example of knitR}
% Creates an unnumbered subsection and does not manually add it to the TOC
\subsection*{Example on how to run R-code directly in LaTeX}
% Note that in Overleaf a misplaced error message is seen when using \$ in the code chunks. It is not an error although the editor states such. The error can be seen on row 188 (\subsection*{Figures}) in this template.
I run some code:
<<echo=TRUE>>=
# Some code
library(xtable) # Loads package xtable
data(iris)
mean(iris$Sepal.Length)
x <- 10
x
@
The output is a bit ugly so I hide some aspects of the code with:
<<echo=FALSE>>=
# Some code
library(xtable)
data(iris)
x <- 10
x
@
% References are created with the \cite{key} command where 'key' is the identifier used in the BiBTeX file
I write my report on the dataset iris.\cite{anderson1935irises} I use references when needed. If I want to reference a specific page I can do that inside the citation. \cite[p. 2-4]{anderson1935irises} Still only one reference will be added to the bibliography.
% We don't have to use code chunks to run R-code. Inline formulas are used as follows.
I have read the data set iris which consists of \Sexpr{nrow(iris)} observations. The first observations can be seen here: \label{my_data} % The command \label{name} creates a label that kan be used for different types of cross-referencing in the document
Showing output directly from R looks very ugly:
<<echo=FALSE>>=
# Print the data
head(iris)
@
It is much nicer (and efficient) to use for example the package \textit{xtable} to produce structured output.
<<echo=FALSE, results='asis'>>=
# Print a xtable table
print(xtable(head(iris,n=20), caption="A table with data"),
include.rownames = FALSE, # Removes the row numbering
table.placement = "H") # Specifies the placement of the table, note that these two commands need to be placed inside the print() and not the xtable()
@
If I use the caption argument in the xtable() function, a caption is automatically generated. This caption is automatically added to a list of tables (if that is used). This process is called floating in LaTeX and is done automatically by the xtable() function.
\subsection*{Figures}
% Here is an example of a cross-reference to a defined label. \pageref gives the page the label is located in and \ref gives the chapter, section, figure or table numbering of the label.
I have used my data, which can be seen on page \pageref{my_data}, to create the nice looking figure \ref{fig:a-figure} located on page \pageref{fig:a-figure}.
% \begin{verbatim} is a way to input unformatted text directly into the document. This is put here to show the code required for the floating environment in the document.
% Note that for R-code the recommendation is to use a code chunk with the option echo = TRUE instead.
Figures created in R are not automatically created in a floating environment (like tables), so this needs to be done manually in LaTeX with:
\begin{verbatim}
\begin{figure}
\centering
\includegraphics{} % R-code can be added here instead of the \includegraphics{}
\caption{Caption}
\label{fig:my_label}
\end{figure}
\end{verbatim}
% Begins a figure float with the relevant code from above
\begin{figure}[H]
\centering
<<echo=FALSE, fig.height = 4, fig.width = 7>>=
with(data=iris,plot(Sepal.Length,Sepal.Width,main="My figure",col="Red", las = 1))
@
\caption{A figure} % Specifies the figure caption
\label{fig:a-figure} % Specifies the label, that has already been referenced
\end{figure}
\section{Exercise 1 - Linear regression}
I have done an awesome analysis with linear regression on all \Sexpr{nrow(iris)} observations from the iris data set:
<<echo=FALSE, results='asis'>>=
analys <- lm(Sepal.Length ~ Sepal.Width, data = iris)
xtable(analys, caption="Some output from the regression analysis")
@
And a corresponding ANOVA table.
<<echo=FALSE, results='asis'>>=
print.xtable(xtable(anova(analys), caption = "ANOVA"))
@
If a list of tables was included in the document, all of these tables will be automatically listed there.
\pagebreak
% Adding a bibliography if citations are used in the report
\bibliographystyle{plain}
\bibliography{BiBTeXexempel.bib}
% Adds reference to the Bibliography in the ToC
\addcontentsline{toc}{chapter}{\bibname}
\end{document}