Positive and Negative thin spaces in LaTeX

At times in LaTeX you want to introduce a small space between words. This is especially important in mathematical typesetting. Just adding a little bit of space hairspace, can alter the aesthetic look of the equation or mathematical entity substantially. For example consider this expression:


$|a|^{2}$

This produces the following

Screenshot 2020-04-04 at 15.47.51

The | braces are too close to the character “a”. To create a better version of this we introduce a small hairspace between the two. The code for hairspace is \,.

$|\,a\,|^{2}$

This produces a much nicer looking expression.
Screenshot 2020-04-04 at 15.47.55

This feature of hairspace was known to me for some time. But the problem came when I had to create a negative space. Take for example this expression:

 $F_{eff} = kx$

The result of this is not so nice looking as you can see:

Screenshot 2020-04-04 at 16.06.38

But then I came to know about a negative hairspace. The code for it is \!. Now let us introduce this in the above expression

$F_{e\!f\!\!f} = kx$

The result is much better visually:

Screenshot 2020-04-04 at 16.10.33

Sidenotes and label in LaTeX

Recently I have been using the sidenotes package in LaTeX. It provides many options which I find aesthetically pleasing. For example, instead of footnotes, it gives sidenotes. This particular typesetting has been used beautifully by Tufte in his books. But even before Tufte’s books this option has been used, for example, see the beautifully designed book The Evolution of Culture in Animals by John. T. Bonner.

bonner

Not the recent reprint, some idiots at Princeton University Press have completely killed the aesthetically pleasing landscaped typesetting to portrait one (and a not very good one)! In general Bonner’s book are a visual treat in terms of designing and of course the content as well. His meticulous detail to the images, and use of log-scales to depict biological scale in time and space it something that I have not seen very often.

The Evolution of Culture in Animals uses a landscape mode with sidenotes and figures in margins extensively. This is the exact functionality that the sidenotes package provides. Even the tufte-book and tufte-handout classes use this package in a modified manner.

When using sidenotes package, I found that the marginfigure and margintable environments worked without a problem when using a label and referring them to in the document. But when it came to the sidecaption option for wider figures and tables, somehow I could not get the referencing to work. The hyperref showed the correct page and even the captions had the correct numbers for tables and figures. But the label and \ref{} to it didn’t work.

For example, if a figure was created as such:

\begin{figure}
\includegraphics[width=0.8\textwidth]{eagle.jpg}
\sidecaption[][1cm]{This is an eagle.}
\label{eagle1}
\end{figure}

The first [] controls the text that appears in the List of Figures, while the second [] controls the vertical placement of the caption.

Now when I referred to this figure in the document using Figure~\ref{eagle1}, it gave an error and typeset it as Figure ??, instead of Figure 1.1 or something similar. The same problem was there for a sidecaption used for tables as well. A common mistake which usually causes this error is placing the label before the caption. So the golden rule seems to be:

Always place your \label{} after the \caption{}.

But it was not the case in my example. A bit of digging in the log file showed this error:


Package caption Warning: \label without proper reference on input line xxx.
See the caption package documentation for explanation.

LaTeX Warning: Reference `eagle1′ on page 7 undefined on input line 415.

Now back to caption package documentation, it had this explanation for this error (page 44):

\label without proper \caption

Regarding \label the floating environments behave differently than its non-floating counter-parts: The internal reference will not be generated at the beginning of the environment, but at \caption instead. So you have to place the \label command either just after or inside the caption text (mandatory argument of \caption).

So that was it. You have to place the \label{} inside the \caption{} environment and the issue was solved. Placing it just after the caption did not work for me.

\begin{figure}
\includegraphics[width=0.8\textwidth]{eagle.jpg}
\sidecaption[][1cm]{This is an eagle.\label{eagle1}}
\end{figure}

Multiple citations, biblatex and APA

Issues with multiple citations of an author and biblatex-apa

Recently I had to refer an author multiple times in a document. I had recently shifted to biber and biblatex. The American Psychological Association (APA) style was the recommended style.

And in the final references

The author name was, let us, was Wilbur-Markus Rowland, and he had about 10-11 articles. The author had many co-authors, in some cases this author was the first author in other cases he was the second or even third one.
Now the issue that I was facing was this: The same author name was getting cited differently at different places.
For example, in some cases it appeared like
W.-M. Rowland, in other cases Wilbur-Markus Rowland, and in some cases even as Rowland. This was very confusing. And on top of all that the final references had entries like

Rowland W.-M. (W. M.)
Rowland W.-M. (Wilbur-Markus)
Rowland W. M. (Wilbur-Markus)
Rowland W. M. (W.-M.)

I checked and rechecked the bibtex entries in hope of finding some error but it was not to be found. I must have done a Clean All a dozen times, after editing the bib file.. I tried adding same name entries for Rowland as first author to be the same, but it didn’t work. This was really frustrating.

Similarly a couple of references with two different authors with a common surname were giving intials in the main text. For example, D. R. Cook and M. P. Cook. Now all other refernces (except the Rowland one) were coming as per the APA requirement, which is (Author, Year), so ideally they should have been (Cook & Weston, 1999) and (Cook, 2004). But rather they were being displayed as (D. R. Cook & Weston, 1999) and (M. P. Cook, 2004). Now no change in the bib file was changing any of this. I thought there was some issue with the bib entries. I deleted the entries and entered them again manually, but no avail.

Then I chanced upon this link

 even though the year of publication differs in the two Campbell (Cook in our case) references, the lead author’s initials should be included in all text citations, regardless of how often they appear.

So the Cook mystery was solved. biblatex was compiling correctly as per the APA guidelines, the initials for the two different Cook entries must be there. This is because

 Including the initials helps the reader avoid confusion within the text and locate the entry in the reference list.

Now for the Rowland entry, the next part of the blog gave me a hint towards the possible problem:

Although this rule seems straightforward, one thing that trips up some writers is how to proceed when different lead authors with the same surname are also listed in other references in which they are not the lead author.

After this I checked the entries where Rowland was second or third author. These entries differed from the entries where he was the lead author. And this was causing the problem. For each different entry style of his name, biblatex was considering him as a separate author. Hence the initials and the different references entries with names in brackets. Once all the entries for Rowland were made consistent the problem disappeared. Phew!

This issue bugged me for almost a couple of working days, to find the cause and subsequent addressing was most rewarding.

Note that the bibtex references that I had were taken from google scholar, hence differing styles in author name. Please make them consistent. This is a warning for future me and others who are reading this post.

 

Lists with LaTeX

While writing documents one needs lists. Usually the lists are either numbered or with bullet. The standard enumerate option in LaTeX by default provides Arabic-Hindu numbers for the list.

The standard syntax is as under:

\begin{enumerate}
\item First item
\item Second item
.
.
\end{enumerate}

This will produce a list with Arabic-Hindu numbers with the items at each head.

In case one wanted a list with bullets, we can use the itemize environment.

\begin{itemize}
\item First item
\item Second item
.
.
\end{itemize}

This will produce a list with with bullets
There is yet another environment description which can take user supplied options for list headings.
For example:

\begin{description}
\item[First] First item
\item[Second] Second item
.
.
\end{description}

In this case the descriptors in square brackets after the \item will be used as the item titles. So when I required any alphabetical list, I used to make list in the description environment and put the alphabets/ descriptors manually.

So far so good.

Recently I had to make a list with Roman numerals instead of Arabic ones. The list was fairly long so manual option seemed to be a very un-LaTeX kind of thing to do. Just a little googling and I found a treasury of options that can be used with the standard enumerate environment. This was the enumitem package.

The package provides various options for the enumerate environment like label and its formatting, style, alignment,  indent, vertical and horizontal spacing etc.

The label options that are available are \alph, \Alph, \arabic, \roman and \Roman,
These can be intialised by using

\begin{enumerate}[label=\emph{\alph*})]

After this the regular \item will produce list with alphabets, numbers or roman numerals.

Please see the documentation for more details.

Suppose you have a list which is split in many parts. You can use resume function to continue with numbering left off in the last part of the list. The resume function can be named and you can have different lists to resume.

Citation inside a Caption – LaTeX

Recently while writing my thesis, I came across a strange error while compiling with LaTeX. The problem arose when I gave a cite command inside a caption environment. The caption was for a figure. For example

\caption{This is the caption for a figure. From  \cite{friend2005}}

This gave the following error:


! Argument of \Hy@tempa has an extra }.
<inserted text>
\par
l.89 ...sity estimate. From  \cite{friend2005}}

?
Runaway argument?
\@captype {\@firstoftwo {\@ifstar {\HAR@acite }{\HAR@fcite }}}\def \reserved@b
\ETC.
! Paragraph ended before \Hy@tempa was complete.
<to be read again>
\par
l.89 …sity estimate. From  \cite{friend2005}}

?

A little googling around I found this site which solved the problem in a second.

So the command should read like:


\caption{This is the caption for a figure. From  \protect\cite{friend2005}}

And the problem is solved. In case you are wondering what \protect does look here.

Latex Tufte class in org-mode

Edward Tufte is known for graphical excellence in his famous books. Some enthusiasts combined his design principles into LaTeX and you have the tufte-book and tufte-handout classes for excellence in typesetting. This has support for sidenotes, margin figures, full width figures etc.

Now, since I have shifted to org-mode on Emacs for most of my writing work including that of LaTeX, it was but natural to take this in org-mode output.

For this a small addition to your .emacs file and you are done. Of course after installing the dependencies. I also came to know about another nice package nicefrac for using in the documents.

For Fedora #yum install texlive-tufte-latex should do the job. Also some font problems may arise which can be solved by running updmap and enabling the needed font.

 ;; tufte-book class for writing classy books
(require 'org-latex) 
(add-to-list 'org-export-latex-classes
'("tuftebook"
"\\documentclass{tufte-book}\n
\\usepackage{color}
\\usepackage{amssymb}
\\usepackage{gensymb}
\\usepackage{nicefrac}
\\usepackage{units}"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}")))

 ;; tufte-handout class for writing classy handouts and papers
(require 'org-latex) 
(add-to-list 'org-export-latex-classes
'("tuftehandout"
"\\documentclass{tufte-handout}
\\usepackage{color}
\\usepackage{amssymb}
\\usepackage{amsmath}
\\usepackage{gensymb}
\\usepackage{nicefrac}
\\usepackage{units}"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}")))

Once you have added these to .emacs, in the org-mode you have to define #LaTeX_CLASS: tuftehandout or #LaTeX_CLASS: tuftebook to invoke this style in the tex output.

Enjoy the Tuftesque typesetting in your own work! Some snippets from my work in progress, no figures so far.

tufte-latex-book

Title Page

tufte-latex-book-2Table of contents

tufte-latex-book-3Main text

LaTeX Notes

This is a post for self-reference.

There are many LaTeX packages that I come across, and forget about them.

This page will keep a track of those packages. It will be updated with new hacks and packages that are useful.

\usepackage{libgreek}

\usepackage{pgfgantt}

  • For including video/audio files in Beamer presentations

\usepackage{media9}