Showing posts with label LaTeX. Show all posts
Showing posts with label LaTeX. Show all posts

Saturday, 8 June 2013

PhD week 66: LaTeX fonts

As part of my research, I will be making labels that indicate the type status of a number of weevil specimens. To create these, I have adapted my previous method of creating specimen labels in LaTeX to include a coloured background. The result of this is that I need to make the font on the labels bigger and bolder.

Unfortunately, because of the idiosyncratic way that I had established my fonts in the document, this ended up being not as trivial as checking out the TeX font catalogue. Instead I wanted to get an idea of what fonts were available on my system, and set about trying to create a font sampler of my very own.

While it wasn't difficult to get a list of the fonts on my computer (using the advice given at StackExchange), it became somewhat more tricky to get an idea of what they looked like. Attempting to compile a test document revealed two errors: The first was that some fonts could not be loaded:

"Font xxxx not loadable: Bad metric (TFM) file"
In addition, some that passed the first test, threw a second error when compiled by themselves:
"mktexpk: don't know how to create bitmap font for xxxx"

A quick email to the texhax mailing list quickly elicited some useful responses, including a very useful code fragment that gets around the first error. The second one was a little harder to overcome. Updating my map file (as suggested by another StackExchange post) didn't seem to do the trick, and messing around in the man pages of mktexpk and related programs didn't suggest any possibilities to one as unfamiliar with the programs as I am.

The breakthrough came when I had the realisation that I could extract the names of the fonts directly from the map file itself. Using this in conjunction with the code fragment mentioned above, I was able to get a document that compiled correctly when broken into three parts of c. 3000 fonts to get around size and space limitations.

I did encounter the error

"pdfTeX error: (file xxxxx.pfb): cannot open Type 1 font file for reading"
which I solved by manually removing the offending lines from the .tex document. There (thankfully!) weren't many of these, so this was not a particularly arduous step. If you try and replicate this though, you have been warned! If you figure out how to get around this, please let me know.

I used R (via Sweave) to extract the names of the fonts from the map file and to create the tex file. Undoubtedly other languages could do the same thing, but I chose to stick with what I'm familiar with. The file is available from gitHub


Websites:
PILN Soundbites
FAO report: Edible insects. Future prospects for food and feed security
Where to buy tango music
RSPB
NHBS: Relentless Evolution by John Thompson

Monday, 17 September 2012

LaTeX command with more than 9 argments

Upon my return from my field trip over the weekend, I have a number of samples to deal to, including a bunch of soil samples. With these samples, the opportunity exists to give a substantial amount more information than is possible on insect labels. Some of this information includes details of the landform and management practises of the area that the sample was collected.

To create the labels, I have enlisted the power of LaTeX (as one would probably expect). As I want the same headings on all samples, I wanted to create a command that includes all these details. However, I want more than 9 arguments, which is not allowed in general LaTeX commands. Searching for options to get around this problem, I found that it was a FAQ on the UK TeX list: How to break the 9-argument limit. Their helpful suggestion of using keyval wasn't backed up by an explicit example of how to implement it. The following code shows how to implement their example:

\usepackage{keyval}

\newcommand{\flowerinstance}[1]{\setkeys{flwr}{#1}}

\makeatletter
\define@key{flwr}{species}{\textbf{Species:} \emph{#1}\\}
\define@key{flwr}{family}{\textbf{Family:} #1\\}
\define@key{flwr}{location}{\textbf{Location:} #1\\}
\define@key{flwr}{locationtype}{\textbf{Location type:} #1\\}
\define@key{flwr}{date}{\textbf{Date:} #1\\}
\define@key{flwr}{numplants}{\textbf{Number of plants:} #1\\}
\define@key{flwr}{soiltype}{\textbf{Soil type:} #1\\}
\makeatother

\begin{document}

\flowerinstance{species=Primula veris,
family=Primulaceae,
location=Coldham's Common,
locationtype=Common grazing land,
date=1995/04/24,
numplants=50,
soiltype=alkaline
}

\end{document}
For more details regarding soil sampling which can be easily extended to other sorts of samples, see the Soil Description Handbook published by Manaaki Whenua Press.

Sunday, 17 June 2012

PhD week 15: Proposal

This week I gave my proposal seminar to the department. This is an opportunity for starting postgraduate students to present their research intentions to their colleagues. They are a formal part of the process of starting into postgraduate research, but they tend not to be too scary. In my case I was able to enjoy a talk time of around 25 minutes, followed by a 15 minute discussion about various details of my research. It was a good experience, but one that I'm pleased to have behind me as now I can concentrate on other, more fun things (like looking at weevils!).

My presentation (the title slide is shown above) was formatted with the beamer package for LaTeX, using a customised style that incorporates the navigation bar on top of a coloured sidebar. This theme emulates the Bio-Protection Research Centre's presentation template, while allowing it to be created with LaTeX. A zip file containing the style files is available on gitHub.

In other news, I started using git as the version control tool for my thesis writing activities. I will be using this in conjunction with Dropbox to achieve the multiple aims of: 1) keeping track of the changes I make to my thesis and associated files, allowing me to go back to previous versions if necessary, 2) having a backup of my work "in the cloud", and 3) being able to access my thesis writing from multiple computers. I was assisted in this task by the documentation for git that guides one through making a repository from an existing folder, and StackOverflow questions regarding using git for writing a thesis and using git with dropbox.


Read:
   McCulloch D. 2010. A History of Christianity: The First Three Thousand Years London: Penguin
   Psalms 63–64,

Websites:
New Zealand Plant Radiation Network
Wikipedia—Liturgy of the hours
Wikipedia—Triangles
Hadley Wickham's R style guide
LibriVox—acoustical liberation of books in the public domain
XeTeX and LaTeX blog—Newcommand with an optional argument
booksshouldbefree.com—out-of-copyright audiobooks
Antweb.org

Listened:
Facedown Records Summer Sampler 2012
Kyrie eleison
Gotan Project—Tango 3.0

Watched:
Eagle Eye

Wednesday, 15 February 2012

Missing \endcsname error in LaTeX

One of the most annoying errors when compiling LaTeX documents is "! Missing \endcsname inserted." It seems to occur at random, and means little or nothing to people such as myself who haven't got into the guts of TeX.

Today I faced it as I tried to create a new function that used a counter I had created. Writing the function was fine, using it in the text was fine, but when I came to put it into a \paragraph{} or \subsection{}. Thankfully, I didn't have to struggle with it too long before some lucky searching brought me across this LaTeX Community forum page. Changing \newcommand{\newname}[1]{...} to \DeclareRobustCommand{\newname}[1]{...} did the trick. My new command worked like a charm in the sectioning commands.

Wednesday, 2 November 2011

Using Sweave with Beamer: A note on fonts

Recently, I've been preparing a poster using the LaTeX packages Beamer and beamerposter. The poster discusses a bunch of R stuff that I've been doing lately, so I successfully used Sweave to incorporate R code into the poster. However, I had some trouble with package names that I wanted to typeset as small caps. The package names looked the same as the rest of the text. A bit of sleuthing around revealed Sweave as the culprit. In the Sweave.sty package, on line 20, it calls the deprecated LaTeX package "ae", which for some reason doesn't recognise small caps. Changing the package to "lmodern" made small caps turn up correctly within the text.

Unfortunately, my problems had not yet been solved completely. I had mentioned the package names in the titles which were set in bold. I thus had created a fairly commonly encountered bugbear with LaTeX—the non-compatibility of bold and small caps in computer modern font. While there are workarounds for this (see this Stack Exchange and TeX FAQ for more details), unfortunately these solutions did not work with Beamer.

So, I set about finding a font that both looked alright, and would accomodate bold and small caps. I found that using helvetica would be fine, but had to be loaded after Sweave to work. As my Sweave block kept placing the \usepackage{Sweave} immedediately before \begin{document}, I resorted to calling helvetica in the Sweave file itself.

The relevant lines in of the Sweave.sty (lines 18-21) file. Before:
\ifthenelse{\boolean{Sweave@ae}}{%
\RequirePackage[T1]{fontenc}
\RequirePackage{ae}
}{}%
After:
\ifthenelse{\boolean{Sweave@ae}}{%
\RequirePackage[T1]{fontenc}
\RequirePackage[scaled]{helvet}
}{}%

Tuesday, 20 September 2011

Beamer themes

I enjoy using Beamer for my presentations. Initially, the limitations it imposes (particularly on graphics placement) are irksome, but after a while you realise that they actually help you create better presentations faster. I've tried 'quickly' putting together something in PowerPoint since starting to use Beamer and found it so fiddly that I went straight back and did it all in Beamer. However: I still find the themes and colour schemes provided in the base distribution of beamer to be less than ideal, and disobey the university's guidelines for presentations. As I garner the skills to start preparing my own themes, a list of customised Beamer themes will provide inspiration and guidance. In particular, I like the look of the Torino theme.

Sunday, 11 September 2011

Alternately coloured line environment with fancyvrb

Recently, while typing up an R tutorial, I used the LaTeX fancyvrb package to create two environments—one coloured blue for R commands, and one coloured red to display R output. This worked well for large blocks of each type. Then I decided I wanted to display a number of one-line commands and output alternately. Looking up the fancyvrb package documentation, it was easy to get alternate colours. The problem was, it did it globally. My environments for input and output also went psychedelic.

After a bit of messing around, I discovered the following solution. My red environments remain red, my blue environments stay blue, and alternate colours only turn up when I want them to!


\documentclass{article}
\usepackage{fancyvrb}
\usepackage{color}

\newcommand{\ChangeLine}[1]{%
\ifodd\value{FancyVerbLine}%
\textcolor{blue}{#1}\else\textcolor{red}{#1}\fi}

\DefineVerbatimEnvironment{blueEnv}{Verbatim}{formatcom=\color{blue}}{}
\DefineVerbatimEnvironment{redEnv}{Verbatim}{formatcom=\color{red}}{}
\DefineVerbatimEnvironment{alternate}{Verbatim}{formatcom=\renewcommand{\FancyVerbFormatLine}{\ChangeLine}}{}


\begin{document}

\begin{Verbatim}
aaaaaaaaaaa
bbbbbbbbbb
cccccccccccc
ddddddddddddd
\end{Verbatim}

\begin{blueEnv}
aaaaaaaaaaa
bbbbbbbbbb
cccccccccccc
ddddddddddddd
\end{blueEnv}

\begin{redEnv}
aaaaaaaaaaa
bbbbbbbbbb
cccccccccccc
ddddddddddddd
\end{redEnv}

\begin{alternate}
aaaaaaaaaaa
bbbbbbbbbb
cccccccccccc
ddddddddddddd
\end{alternate}

\end{document}

Monday, 8 August 2011

tlmgr not available for Ubuntu

One of the hardest things about LaTeX is the way it manages packages. Doing it manually is (in a word) annoying. When I was on windows I loved MikTeX for the ease by which it downloaded and installed extra packages, and I was disappointed when this functionality wasn't available on Ubuntu. Lately though I discovered that TeXlive had a similar package manager called tlmgr, and I started getting excited. When installing TeXlive though, I was dismayed to find that tlmgr did not work. A bit of a google search later I found that this was reason:
There is no way that a second package manager independent of the normal packaging infra structure (apt here, or rpm, or whatever) can work, because it will break the main system.

TeX Live Manager is currently only for system trees. THere is a patch in the dev repository for activating user mode, so that tlmgr can be used to manage TEXMFHOME, but it has not been worked on since quite some time (Norbert Preining on launchpad.net)

I take this to mean that there's only room in Ubuntu-town for one package manager, and synaptic is it. This is fair enough I guess, but it is still unfortunate.

Norbert goes on to say "get your hands dirty and help coding!" Unfortunately, my perl is non-existent, so I'll have to give it a miss until such a time as I actually have some idea what I'm doing.

Wednesday, 10 November 2010

Using pdfpages to rotate odd pages only

Today I scanned a document to PDF. It was a large document, and I scanned it in stages. No worries about putting the pieces together—that's what the LaTeX package pdfpages is for. What did cause a bit of a problem was that the odd and even pages were oppositely orientated. When I scanned the pages I had to turn the book around, meaning that all the odd pages were upside down.

To rectify this problem I had to delve into the dark world of LaTeX programming. It was an adventure, but thankfully it wasn't too difficult. What I came up with was the following tex file:
\documentclass[a4paper,twoside]{memoir}

\newcounter{number@}

\usepackage{pdfpages}

\begin{document}

\setcounter{number@}{0}

\loop\ifnum \value{number@} < 6 %CHANGE for each document
\stepcounter{number@}
   \ifodd \value{number@}
      \includepdf[pages=\arabic{number@}, angle=180]{document}
   \else
      \includepdf[pages=\arabic{number@}]{document}
\fi
\repeat

\end{document}

To illustrate, I've made an example PDF file to test it on. This test file is an open access paper from Zootaxa, the original of which is available here.

Do remember to change the number that \value{number@} is being compared to. This number is the total pages in "document.pdf", and I haven't yet figured out how to automatically retrieve it. Doing so would've consumed more time than I can afford just now.

Particularly helpful in this adventure was the Tralics site that contains documentation on all TeX commands, and this site on counters.

Monday, 8 March 2010

LaTeX insect labels

No, not labels made out of the sap of Hevea brasiliensis, but labels typeset using the LaTeX typesetting language---a very versatile and powerful language that typesets rather nice looking documents. I wrote my thesis using it and found the experience to be very enjoyable and rewarding.

Up until now, I've printed labels for pinned insect specimens using a Word template. This has been adequate, however I find there are two really annoying aspects of this method. The first is that you've got to try and count how many carriage returns you enter to centre a single-lined habitat label, and if you get it wrong it messes up the rest of the document. The other is making duplicate labels, the only way which I know how being to copy the label data and "Ctrl-V" it as many times as required, once again ensuring there's neither too much or not enough space between labels.

So, with the thesis out of the way, I've turned my attention to creating a LaTeX template to create insect labels from an A4 sheet of paper. It is available here: labels.tex. Hopefully it is relatively straightforward to work out when you read the file and comments contained therein. The size of the labels is based on those in the classic New Zealand guide to insect curation (Walker and Crosby, 1988). I just found out that this very useful reference is freely available online here. Thanks Landcare Research!

The guts of the file is as follows:

To get a really small font suitable for insect labels, the following commands are made:

\def\supertiny{\font\supertinyfont = cmss10 at 3.75pt \relax \supertinyfont}

\def\supertinyitalic{\font\supertinyfont = cmssi10 at 3.75pt \relax \supertinyfont}

\newcommand{\scinm}[1]{\supertinyitalic{#1}}
\supertiny is declared after \begin{document} to make the text of the entire document miniscule. Unfortunately, \emph doesn't work correctly when you've modified the size in this way, so \supertinyitalic is created and turned into a function \scinm which converts text (usually scientific names) into italics.

Next up is creating an environment to make duplicate labels:

\newcounter{speclabel@}
\newcounter{speclabel@@}
\newsavebox{\TMPspeclabel}
\newenvironment{speclabel}[1][1]{
\setcounter{speclabel@@}{#1}
\begin{lrbox}{\TMPspeclabel}
\begin{minipage}{\columnwidth}
\vspace{0.5ex}
\raggedright}
{
\end{minipage}
\end{lrbox}
\setcounter{speclabel@}{0}
\loop\ifnum \value{speclabel@} < \value{speclabel@@}
\stepcounter{speclabel@}
\usebox{\TMPspeclabel}
\endgraf\repeat}
The above code is modified from that available in etiketka.cls in the shipunov package. Each set of labels (both locality and habitat labels) are prefaced with \begin{speclabel}[5] where 5 in this case indicates that the contents of the environment should be duplicated five times.

Finally, we want to have each label of a consistent height. I achieved this by employing the tabularx package. However, the vagaries of the tabularx package made it easier to program it using two environments rather than working out how to make a single one do it all:

\newsavebox{\BoxB}

\sbox{\BoxB}{\begin{tabularx}{0.1mm}{Y}
\rule[-8mm]{0pt}{8mm}
\end{tabularx}
}

\newenvironment{lab_height}{\begin{tabular}{cc}}
{& \makebox{\usebox{\BoxB}}
\end{tabular}
}

\newenvironment{hablabel}{\tabularx{11.9mm}{@{}Y@{}}}
{\endtabularx}
This is a bit clunky, but it works. Reasonably well at least. BoxB creates a table of fixed width that contains a strut which fixes the height of the label. Currently this is 8 mm. When hablabel is nested within lab_height the contents of hablabel is put into a cell in a table the height of which is defined by BoxB. When the contents of hablabel takes up less height than BoxB, the result is a whole bunch of nicely spaced labels. When it takes up more height, things are pushed around a little, but it doesn't break labels.

Here's an example of a fully typeset label:

\begin{speclabel}[3]
\begin{lab_height}
\begin{hablabel}
NEW ZEALAND BR\\
Nina Valley Track\\
Lewis Pass\\
1 Jan 2009\\
SDJ Brown\\
\end{hablabel}
\end{lab_height}

\begin{lab_height}
\begin{hablabel}
{\scinm{Weinmannia}} leaves close to road.
\end{hablabel}
\end{lab_height}
\end{speclabel}


You can see the end results here.

I'm not much of a LaTeX programmer and undoubtedly there are more elegant, powerful and useful methods of programming the above. However, this provides for my needs adequately enough. Hopefully others might find it of use also.

While trying to figure how to do all this, a search on Google for "latex insect labels" gave this humble site as the first on the list. I figure that the least I can do is actually make sure that it delivers on its promise...

EDIT:
labels.tex is now available on gitHub. The PDF showing the results can be downloaded from Dropbox.

References:
Walker AK, Crosby TK. 1988. The preparation and curation of insects. DSIR Information Series 163. DSIR; Wellington.

Monday, 12 October 2009

LaTeX tips and tricks

I have been using LaTeX for the past 10 months or so and have been really enjoying it. It's making the thesis writing process a delight for the most part, and produces some really nice output. Too nice sometimes as it can be easy to forget that the content is the important part!

Unfortunately, I have found installing packages on Linux to be a bit of a challenge. I installed the package manager MikTeX, but that's been playing up for some strange reason, so I'm going to try doing it manually. Thankfully there's some help available here.

Here's a couple of other resources on MikTex for linux and an introduction to table of contents formatting. I've also found the LaTeX Community Forum to be very helpful.