Sometimes when you include a figure, you dont want to have to edit the picture file itself. In MS Word for instance you can use the crop command. LaTeX being LaTeX doesnt work quite the same way, but it is still possible. The standard way is to use the trim and clip options to \includegraphics -

\begin{figure}
\includegraphics[trim= left bottom right top, clip=true]{file.jpeg}
\end{figure}

This method doesnt always work however. For instance, if you use xelatex, like me.

There is a way around this though – the adjustbox package. It includes, among other handy functions, the function \adjincludegraphics which does the trick. One of the best things about it is that it even seems to use the same coding, so no need to change all of the argument names…just add adj to the function call.

\usepackage{adjustbox}
...
\begin{figure}
\adjincludegraphics[trim= left bottom right top, clip=true]{file.jpeg}
\end{figure}

I think that its even possible to have it export its settings so that \includegraphics is recoded to do the same as \adjincludegraphics. I’ve not yet tried that though. (It should be just a case of using \usepackage[Export]{adjustbox} instead of \usepackage{adjustbox})

Changing how LaTeX prints table or figure numbers is surprisingly easy to do. Say you have a figure and rather that the figure being labelled as Figure 4 you want it to be S1 because its a supplementary figure.

\setcounter{figure}{0}
\makeatletter
\renewcommand{\thefigure}{S\arabic{figure}}

So we have reset the figure counter to zero and have told LaTeX to redefine \thefigure to have an S before the figure number. Easy huh!?!

For documents with chapters it is almost as easy to do:

\setcounter{figure}{0}
\makeatletter
\renewcommand{\thefigure}{\arabic{chapter}.S\arabic{figure}}

To change tables instead of figures, just swap any instances of figure for table. Piece of cake.

Do remember to reset it though, otherwise your next chapter continues to use the changes you’ve made!!

Just a quick post to say that on Saturday Ben Bolker (on behalf of the lme4 authors) announced that lme4 version 1 will be released to CRAN on the 1st August.

https://stat.ethz.ch/pipermail/r-sig-mixed-models/2013q2/020503.html

So to anyone thats written code based on the current lme4 that uses the @ to access parts of the model object…its time to start rethinking those functions…

Much like in the statistical environment R, it is possible to make custom functions (or “macros”) in LaTeX. This is done using the

\newcommand{commandname}[number_of_arguments]{definition}

command, where commandname and definition are replaced with a name (e.g. \mysubscript) and a definition, respectively, and number_of_arguments can be provided or not. For example see my previous post on subscripts.

What do you do if you want to control the behaviour of that macro? Say you want the command to do one of two things, print Fig or Figs for instance.

One way to accomplish this is to use the etoolbox package:

\usepackage{etoolbox}
\newcommand{\fig}[1]{
\ifblank{#1}{Fig.}{Figs.}
}
\begin{document}
\fig{} is singular, while \fig{1} is plural.
\end{document}

All this does is checks to see if there is an argument (#1) and prints its second argument if not (spaces or blank), or its third argument if there is something.

Hope this helps someone!

[UPDATE] A better method seems to be to use the following

\makeatletter
\def\fig{\@ifnextchar[{\@with}{\@without}}
\def\@with{Figs.}
\def\@without{Fig.}
\makeatother

otherwise it sometimes adds in extra space for no apparent (to me) reason. I’m not sure what the \makeatletter and \makeatother lines are about, but the other parts define a command called \fig which if defined as “if the next character is a [ do \@with else do \@without“.  \@with and \@without are then defined separately.

How to produce subscript text in LaTeX without entering “math mode” seems to be a common question on tex.stackexchange.com. There are various reasons for wanting to do so. I wanted to do it to avoid the italics it produces and have the subscript in the same font as the rest of the document.

Heres a little macro which does the job

\newcommand{\mysubscript}[1]{\raisebox{-.4ex}{\scriptsize #1}}

To write something simply write some\mysubscript{thing}.

You could always shorten the macro name to \myss{}.

Updating versions of R can be a pain in terms of getting all those packages you had on the old version onto the new version. Some people suggest copying the library folder from one installation to another and running update.packages(). Other people have other methods.

Here’s my simple method:

On the old installation make a vector of names of packages and write it to a file:

packs <- row.names(installed.packages())
write.table(packs, "packs.txt") # could also use dump or save or....

On the new version of R just read in the file and run install.packages:

packs <- read.table("packs.txt")
install.packages(as.character(packs\$x)) # *



Select the mirror and wait a few minutes.

Simple!

* because this method ends up having packs as a table with 1 column (interpreted as a factor), we have to force R to think its a character

One of the great things about R is that if you use scripts, you have a record of what you’ve done. If you copy the console output into the script then you also have a copy of that. Brilliant…until you forget or get lazy.

But what if you could make a pdf of your work? Using Sweave (S being then language that R is based on and weave being the verb), you can. But it does use LaTeX, so you have to learn a little bit of that too, as well as install it. Check out CTAN – the LaTeX equivalent of CRAN

If you use RStudio, this is really easy though. You open a new “R Sweave” file which already has most of what you need to begin – the bones of the LaTeX document:

\documentclass{article}\begin{document}\SweaveOpts{concordance=TRUE}\end{document}

After the \SweaveOpts line you can start typing any description of the analysis youre doing. To start an “R chunk” (some code for R to interpret) you type

<<>>=

enter your R code and then type

@

to end it. So a short file might look like this:

\documentclass{article}\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}\usepackage[noae]{Sweave} \title{Cars}\begin{document}\SweaveOpts{concordance=TRUE}\maketitleOpen the cars dataset:<<>>=data(cars)@Show a summary of the dataset:<<>>=summary(cars)@Make a figure<<fig=TRUE>>=plot(cars[,1], cars[,2])@\end{document}

I added a couple of lines to the code to make it look a little different – the line with top, bottom etc just alters the margins using the geometry package. I also added \usepackage[noae]{Sweave} because ‘ symbols stop it working…the [noae] allows it to include them.For the figure I included the fig=TRUE between the << and >> to tell LaTeX to include the figure. There are other arguments to tell it to ignore the section or just return the result etc.

Once youve got that, you just hit the “Compile PDF” button on the RStudio tool bar.

If you dont use RStudio, you have to use the Sweave function

help("Sweave", package="utils")

I hope someone finds this helpful!!