knitr
Templates! You can embed R code in any of the above document types, and have R parse that code and auto-format the code and output in your LaTeX document! To see some examples, download knitr-templates.zipI recommend using the TeXStudio editor because it's free and it runs on Windows, Mac, and Linux operating systems -- that is, you and your collaborators or students can all use the same software! Alternatively, you can use a cloud-based LaTeX editor and storage space like OverLeaf.com (formerly WriteLaTeX.com) or ShareLaTeX.com, which both offer free accounts.
Instructions for software installation: See www.pauljhurtado.com/teaching/software.html for the most current information.
The instructions at the link above will help you install and configure the following software:
Also, if you're working on a document with a bibliography I recommend using BibTeX and the BiBTeX file manager JabRef (many people also like BibDesk). See the manuscript template above for an example.
Jabref note: To use Jabref to make an annotated bibliography using the IEEEannot.bst style file, it helps to customize the layout of fields. It helps to configure JabRef to read the extra Annotation fields. To do this, run JabRef then click on File, then Preferences, then Entry Editor, then you can enter Custom editor tabs by pasting something like the following into the provided text box:General:doi;url;crossref;keywords;file;groups;owner;timestamp
Annotation:annote;abstract;comment
Here is an example article and some related files to get you started. If you have LaTeX software installed on your computer, open up a new document and save it as "examples.tex" in a new project directory, so you can compile the document and changes below to see how things look.
A few tips for new users:
\ref{}
to refer to labeled figure and table numbers, \eqref{}
for equation numbers, etc. Never type in figure or table numbers directly! LaTeX manages those reference numbers for you, and updates them when new figures, equations, etc. get added to the document.LaTeX files include a header followed by the main body of the document. The header includes instructions to load additional packages, set default formatting parameters, etc. The body contains a mix of document text and LaTeX commands for formatting that text. Here's an example:
\documentclass{article} % Specify article, report, etc. to set format defaults % All text after a "%" until the end of the line are comments. % Packages: add-on routines for specific formatting needs. For example, \usepackage{graphicx} % if we want to include figures. \usepackage[margin=0.5in]{geometry} % an easy way to setup more compact margins % This is the end of the HEADER -- where we set document-wide format defaults. % Start the main content we'll see in the finished document: \begin{document} % First, let's set up the title page. \title{\LaTeX{} Example} % \LaTeX{} returns fancy text. \author{John Q. Public} \maketitle % Use the above information and format the first page. % \begin{abstract} ... \end{abstract} % If you're writing a paper \section{Introduction} This text appears as the first sentence in my document. \begin{equation} \label{eq:ex1} % label equation using the eq:NAME and fig:NAME convention \dot{x} = f(x) \end{equation} The nice thing about using labels for equations, figures, citations, etc. is that \LaTeX{} will take care of all the numbering for you! If I moved equation \ref{eq:ex1}, all my references will still be numbered correctly. \end{document}
Next, let's focus on editing the body, e.g. as if we wanted to update a draft manuscript, or create a set of typed course notes using an existing template file. We'll skip discussion of how to customize your document formatting by modifying the header: using packages, style sheets, bibliograpy tools, etc. Look online for those formatting examples.
\begin{document} % ... \section{Introduction} This text appears as the first sentence in my document. \begin{equation} \label{eq:ex1} \dot{x} = f(x) \end{equation} The nice thing about using labels for equations, figures, citations, etc. is that \LaTeX{} will take care of all the numbering for you! If I moved equation \ref{eq:ex1}, all my references will still be numbered correctly. \end{document}
A basic category of LaTeX elements are commands, which take the form
\name[modifier]{input}There may be multiple
{}
and []
options, or none at all. A good LaTeX editor, like TeXStudio, will help you remember these by including common commands in the drop-down menus. Text in braces {like this}
is often referred to as a block. Many commands act on the next block (the next character, by default), but it is best to use braces and not rely on defaults: \textbf{Which part is bold? All!}
might (or might not) yield the same results as \textbf Which part is bold?
.
\usepackage{amssymb, amsmath}
as these packages are required for some of the commands and environments covered in the guide.
\begin{document} % ... \section{Introduction}\label{Intro} This text appears as the first sentence in my document. These are: \textbf{bold}, \textit{italicized}, \emph{also italicized}, \texttt{courier new / typewriter}. \begin{equation} \label{eq:ex1} \dot{x} = f(x) \end{equation} The nice thing about using labels for equations, figures, citations, etc. is that \LaTeX{} will take care of all the numbering for you! If I moved equation \ref{eq:ex1} out of section \ref{Intro}, all references will still be numbered correctly. \end{document}
One of the key strengths (and big differences) of LaTeX over editors like MS Word are the wonderful referencing capabilities. References to section numbers, figure numbers, appendices, equations, etc. can all be done by marking them with \label{name}
, where the name
often includes a prefix to indicate whether it's a fig
ure, eq
uation, etc. References in the text can then be written in terms of these labels, and LaTeX will fill in the numbers for you! Including those numbers in the text is done with \ref{name}
or more specialized versions that allow specific formatting, like \eqref{name}
. We'll discuss bibliographic citations in the relevant section below.
\begin{document} % ... \section{Introduction}\label{Intro} Here we have a new equation \begin{equation} \label{eq:new} a=b+c \end{equation} which appears before our original equation \begin{equation} \label{eq:ex1} \dot{x} = f(x) \end{equation} All locations where I had referred to equation \ref{eq:ex1} in the text are now automatically updated to point to the right equations! The same holds true for numbered citations when you add a reference and update your bibliography, etc. \end{document}
There are two ways to include this information: by writing it all into the end of the main document, or by including it in a separate BibTeX file and using the cite
or natbib
package (use the latter, as it is an extension of the former) to format things for you. Most prefer the BibTeX approach, but we'll briefly look at modifying documents of either type.
... and so the last citation in this document is \cite{lamport94}. In practice, I tend to specify whether I want an in-line text citation, like \citet{lamport94} or a parenthetical one \citep{lamport94}. %% Here's our bibliography information. \begin{thebibliography}{9} \bibitem{lamport94} Leslie Lamport, \emph{\LaTeX: A Document Preparation System}. Addison Wesley, Massachusetts, 2nd Edition, 1994. \end{thebibliography} \end{document}
To add a new reference, just copy the current format (see the above link if you need more help).
The more popular approach keeps all this in an external file and uses the package natbib
, which allows more flexible formatting. Most journal websites and citation management software allows for exporting things in this (BibTeX) format! In this case, your document ends with just one line, pointing to the BibTex file that contains your citation info:
\bibliography{./myBibTeXfile} % which is actually named myBibTeXfile.bib \end{document}Entries in myBibTeXfile.bib look like
@article{Hurtado2012, title = {{Within-host dynamics of mycoplasma infections: Conjunctivitis in wild passerine birds}}, author = {Paul J. Hurtado}, year = {2012}, journal = {Journal of Theoretical Biology}, volume = {306}, number = {0}, pages = {73 - 92}, doi = {10.1016/j.jtbi.2012.04.018}, url = {http://www.sciencedirect.com/science/article/pii/S0022519312001993} } @book{Murdoch2003, title = {{Consumer-Resource Dynamics}}, author = {Murdoch, William W. and Briggs, Cheryl J. and Nisbet, Roger M.}, year = {2003}, publisher = {Princeton University Press}, series = {Monographs in population biology; 36}, address = {Princeton, NJ}, isbn = {13: 978-0-691-00657-4}, url = {http://press.princeton.edu/titles/7569.html} } @phdthesis{HurtadoThesis, author = {Paul Joseph Hurtado}, title = {{Infectious disease ecology: Immune-pathogen dynamics, and how trophic interactions drive prey-predator-disease dynamics}}, school = {Cornell University}, year = {2012}, type = {PhD Thesis}, month = {January} }To update, or add references, it's best to use a BibTeX manager like JabRef (windows) or BibDesk (OS X), however you can also easily edit the *.bib file with your regular text editor once you know what you're doing.
knitr
:knitr
to do the same thing with other languages, e.g., python! For examples, see https://yihui.name/knitr/demo/engines/.knitr
document that includes a chunk of R code. This would add a nicely formatted version of the code and the corresponding figure, to your compiled document (i.e. the PDF or DVI).
\begin{document}
add the following knitr code block (this just modifies default formatting):
<<setup, include=FALSE>>= # smaller font size for chunks knitr::opts_chunk$set(size = 'footnotesize', concordance=TRUE) options(scipen=1, digits=4) @Here's how these code blocks work: everything between the line
<<...>>=
and the line with just a '@' on it is going to be interpreted by the knitr
package in R as R code. Code blocks each need a unique name (the first argument, 'setup', in this example) and can be modified in various ways by additional arguments (e.g. include=FALSE
tells the knitr package to run this code, but not show this code in the final document). Here the details of the R code are just arguments to setup formatting, so read up on knitr to make sense of those.
<<sinplot, fig.height=3, fig.width=6>>= # This is a regular old R comment 1+1 curve(sin(x),-10,10) @
natbib
package instead of cite
.