# Latex

This isn’t really a specific question, but I needed to make a bibliography in latex with the following requirements:

• The citations take up as little space as possible, so should probably be superscript
• The citations should be correctly grouped (i.e. 1-3, 6 not 6, 2, 3, 1)
• The bibliography can take up any amount of space
• The citations should be linked to their bibliography entry (i.e. hyperref compatible)
• bib entries contain unicode characters
• I want the entries to look like the Elsevier standard, though Nature is also fine
• I want DOIs, properly displayed and hyperlinked, not monospaced

The style=nature option supplied to biblatex in the preamble (see http://ctan.org/pkg/biblatex-nature) achieves most of this but you don’t get DOIs, there seemed to be some problems with unicode characters (particularly Polish names, see http://www.terminally-incoherent.com/blog/reference/latex-reference/) and there were some problems displaying URLs well

Rather than trying to hack together a biblatex.cfg based on the nature style which I didn’t understand/couldn’t be bothered to read through I instead was able to use a standard biblatex style with some options when loading the package:

\usepackage[style=numeric-comp,
maxcitenames=2,
maxnames = 5,
firstinits=true,
uniquename=init,
sorting=none,
url=false,
isbn=false,
eprint=false,
texencoding=utf8,
bibencoding=utf8,
autocite=superscript,
backend=biber
]{biblatex}

This gets pretty close, but I also needed to use the following biblatex.cfg (create this file in the same directory as the .tex file):

% Number in parenthesis
\renewbibmacro*{volume+number+eid}{%
\printfield{volume}%
\iffieldundef{number}
{}
{\bibopenparen
\printfield{number}%
\bibcloseparen}
\printfield{eid}}

% Field formats for the bibliography environment (get rid of square brackets)

%Get rid of in:
\renewbibmacro{in:}{}

%Get rid of pp.
\DeclareFieldFormat[article,inproceedings,incollection]{pages}{#1}

%Make volume number emboldened
\DeclareFieldFormat[article,inproceedings,incollection]{volume}{\textbf{#1}}

%Journal name in non-italics
%\DeclareFieldFormat[article,inbook,incollection,inproceedings,patent,thesis,unpublished]{journaltitle}{#1}

%No quotes around article name
\DeclareFieldFormat
[article,inbook,incollection,inproceedings,patent,thesis,unpublished]
{title}{#1\isdot}

%Bibliography in smaller font size, and unjustified
\renewcommand{\bibfont}{\normalfont\small\raggedright}

%Hyperlinks in serif font
\def\UrlFont{\normalfont}

%DOI lower case, normal font
\renewcommand*{\mkbibacro}[1]{%
\ifcsundef{\f@encoding/\f@family/\f@series/sc}
{#1}
{\MakeLowercase{#1}}}

%Colon after author names
\renewcommand{\labelnamepunct}{\addcolon\space}

Which got me what I wanted:

Using chemfig I was able to represent DPPC (Dipalmitoylphosphatidylcholine) and other lipids in Latex by using the following code

\newcommand\setpolymerdelim[2]{\def\delimleft{#1}\def\delimright{#2}}
\def\makebraces[#1,#2]#3#4#5{%
\edef\delimhalfdim{\the\dimexpr(#1+#2)/2}%
\edef\delimvshift{\the\dimexpr(#1-#2)/2}%
\chemmove{%
\node[at=(#4),yshift=(\delimvshift)]
{$\left\delimleft\vrule height\delimhalfdim depth\delimhalfdim width0pt\right.$};%
\node[at=(#5),yshift=(\delimvshift)]
{$\left.\vrule height\delimhalfdim depth\delimhalfdim width0pt\right\delimright_{\rlap{$\scriptstyle#3$}}$};}}
\setpolymerdelim[]

\begin{figure}
\small
\setatomsep{1.5em}
\chemfig{N^+(-[:180,1.1]H_3C)(-[:90,1.3]CH_3)(-[:270,1.3]CH_3)(-[:-30]-[:30]-[:-30]O-[:30,1.3]P^+(<[:50,1.5]O\rlap{${}^-$})(<:[:130,1.5]O\rlap{${}^-$})(-#(1pt,)[:330,1.3]O-[:30]-[:-30](-[:270]O-[:-30](=[:270]O)(-[@{downleft,0.8}:30]CH_2-#(1pt,1pt)[@{downright,0.3}:-30,1.2]CH_3))(-[:30]-[:-30]O-[:30](=[:90]O)(-[@{upleft,0.8}:-30]CH_2-#(1pt,1pt)[@{upright,0.3}:30,1.2]CH_3))))}
\makebraces[10pt,13pt]{n}{downleft}{downright}
\makebraces[6pt,15pt]{n}{upleft}{upright}
\label{fig:lipids}
\end{figure}

The crucial line is:
\chemfig{N^+(-[:180,1.1]H_3C)(-[:90,1.3]CH_3)(-[:270,1.3]CH_3)(-[:-30]-[:30]-[:-30]O-[:30,1.3]P^+(<[:50,1.5]O\rlap{${}^-$})(<:[:130,1.5]O\rlap{${}^-$})(-#(1pt,)[:330,1.3]O-[:30]-[:-30](-[:270]O-[:-30](=[:270]O)(-[@{downleft,0.8}:30]CH_2-#(1pt,1pt)[@{downright,0.3}:-30,1.2]CH_3))(-[:30]-[:-30]O-[:30](=[:90]O)(-[@{upleft,0.8}:-30]CH_2-#(1pt,1pt)[@{upright,0.3}:30,1.2]CH_3))))}

You’ll also need to include the following in the preamble

\usepackage{chemfig}

Which produces something that looks like this:

I couldn’t find how to do this easily, but perhaps this is because I used rubbish search terms.

I eventually found my answer on http://en.wikibooks.org/wiki/LaTeX/Labels_and_Cross-referencing (which ended up telling me lots of useful things about the hyperref package I didn’t know)

First source the hyperref package in the preamble

\usepackage{hyperref}

You’ll probably want to provide some options to make it look nicer. See the manual linked from the ctan page: http://www.ctan.org/pkg/hyperref

You can then add references choosing the text yourself with a command of the format

\hyperref[label-name]{link-text}

It helps to illustrate this with an example. In my case I have a figure 4, composed of 3 sub-figures 4a, 4b and 4c (though these are simply part of the same image, not specified as separate figures in latex). My figure is labelled ‘SEM’ and I want to reference figure 4c including a hyperlink to the figure it appears in. I can do this using:

\hyperref[fig:SEM]{\ref*{fig:SEM}c}

This sends the link to the SEM figure, and puts as the hyperlinked text ‘4c’. Using \ref in the curly brackets ensures the figure number is updated if it changes from 4, which is the usual behaviour we desire.

Another thing I came across on the wikibooks page was the \autoref command provided by hyperref. This looks like a better idea than using \ref and constantly typing figure, and could straightforwardly be included in the above example by changing \ref to \autoref

Fedora provides a texlive package (http://fedoraproject.org/wiki/Features/TeXLive), however it is incomplete, usually out of date and I haven’t been able to easily install new latex packages through it. In theory new packages can be installed by issuing the command:

yum install 'tex(epsfig.sty)'

However this never worked for me, and despite some searching I couldn’t work out what was going wrong.

Personally, as someone with plenty of free disk space, I’ve found the best solution is to install the full version of texlive. Certainly, ever since doing so I’ve never had any problems compiling my latex files and haven’t had to think about the install ever since.

This excellent post on the tex StackExchange describes in detail how to do this with Ubuntu:
http://tex.stackexchange.com/a/95373
which I would recommend reading before following any of the advice here

For fedora it may be slightly different (especially in faking packages, see step 1 below), but in summary what I did was as follows:

1. Install the official package from fedora using ‘yum install texlive’ (so that software with tex as a dependency can be installed)
2. Download the installer for the full texlive from http://www.tug.org/texlive/acquire-netinstall.html
3. Run the install-tl script
4. Make sure the install path is /opt (or /opt/texlive if you’d like)
5. Add /opt/texlive/2012/bin/x86_64-linux (with the correct year) to the path (see e.g. http://askubuntu.com/questions/60218/how-to-add-a-directory-to-my-path if unsure how to do this) making sure it’s added before /usr/bin so the correct latex programs are used rather than the ones from fedora’s texlive package
6. If using some software such as texmaker to edit and compile your latex, make sure it is correctly configured to run pdflatex, biber etc. from /opt/texlive/2012/bin/x86_64-linux (e.g for texmaker follow the instructions at http://www.xm1math.net/texmaker/doc.html#SECTION02)