## Handwriting to LaTeX

This site allows me to convert what I write to LaTeX or MathML codes. I think it’s conversion or translation is quite accurate after a quick test.

## Embed all fonts in a pdf file

I recently had to embed all fonts in a pdf file for electronic submission of my dissertation. Embedding of fonts is usually required for publishing academic articles as well.

I generate my pdf files mainly with LaTeX using the pdflatex command. This post shows how one can embed fonts generated by the tex file; this option was turned on (default?) on my Ubuntu 11.04 laptop with TeX Live (2009?). However, this does not embed fonts from pdf files (figures) that are included in the tex file. This post and this post shows how to embed all fonts used in a pdf file:

 <pre class="src src-sh">pdf2ps myfile.pdf


ps2pdf13 -dPDFSETTINGS=/prepress myfile.ps myfile_embedded.pdf

or

 <pre class="src src-sh">gs -dSAFER -dNOPLATFONTS -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sPAPERSIZE=letter -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dCompatibilityLevel=1.4 -dMaxSubsetPct=100 -dSubsetFonts=true -dEmbedAllFonts=true -sOutputFile=myfile_embedded.pdf -f myfile.pdf


My embedFontsPdf.sh script:

 <pre class="src src-sh"><span style="color: #ff4500;">#</span><span style="color: #ff4500;">! /bin/</span><span style="color: #00ffff;">bash</span>


# http://colinm.org/tips/latex for file in “$@” do bn=basename "$file" NameNoExt=${bn%.} ## no extension Ext=${bn/./} ## extension http://www.linuxforums.org/forum/programming-scripting/128625-how-get-file-extension-without-dot.html gs -dSAFER -dNOPLATFONTS -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sPAPERSIZE=letter -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dCompatibilityLevel=1.4 -dMaxSubsetPct=100 -dSubsetFonts=true -dEmbedAllFonts=true -sOutputFile=“${NameNoExt}_FontsEmbedded.pdf” -f “$file” done

Use pdffonts to check that all fonts are embedded.

## background image in LaTeX Beamer presentation

This post shows how one can change the background image in a Beamer presentation:

 <pre class="example">%% all slides


\usebackgroundtemplate{ \includegraphics[width=\paperwidth, height=\paperheight]{my_bg_image} }

%% single slide { \usebackgroundtemplate{\includegraphics[width=\paperwidth]{my_bg_image}} \begin{frame} \frametitle{Frame with nice background} \begin{itemize} \item 1 \item 2 \item 3 \end{itemize} \end{frame} }

The above method spans the image to cover the entire page size. To include a smaller image and center it, do this:

 <pre class="example">\usebackgroundtemplate{%


\vbox to \paperheight{\vfil\hbox to \paperwidth{\hfil\includegraphics[width=1.5in]{name.jpeg}\hfil}\vfil} }

(I asked this question on the Tex.SX since \hspace{4.5cm} works but \vspace{4.5cm} did not.)

## Vietnamese in LaTeX

I recently had a need to create a Beamer presentation with Vietnamese characters. After many tries, the simple outline from this post works for me:

 <pre class="example">\documentclass{report}


\usepackage[T5]{fontenc} \usepackage[utf8]{inputenc} \usepackage[vietnam]{babel} \begin{document} Tiếng Việt \end{document}

## sideway or landscape table over multiple pages in LaTeX

Most of my results table are huge. To get them situated in a document, the sidewaystable environment from the rotating is helpful in getting a wide table to fit on one page. To get a long table to span multiple pages (regular orientation), one can utilize the longtable environment; see this example.

What if both solutions don’t work? That is, you have a wide and long table that needs to inserted in landscape mode and have the table span multiple pages? This post shows one how to do so by utilizing the landscape environment from the lscape package with longtable. It looks something like:

 <pre class="example">\begin{landscape}


\begin{longtable}{|c|c|c|c|c|} … \end{longtable} \end{landscape}

This was just what I needed.

## Web-based collaboration of LaTeX documents

This post brought LaTeX Lab to my attention (again). It is a service based on the Google Docs platform for writing and collaborating LaTeX documents. I also became aware of ScribTeX, another similar service. I’m not sure how often I’ll use these services as I’m more (counter?) productive in emacs. Moreover, my interdisciplinary collaborators don’t use LaTeX. As for the collaborators that do use LaTeX, we just email the tex file back and forth as usually only one person edits the file at a time. This post describes how I view changes to a tex file.

I’m not too concerned about the privacy issues (they have my trust). I guess I can see myself using these services to demonstrate how to write LaTeX and for real-time collaboration across the web.

## Some custom emacs keybindings for LaTeX to assist in the writing process

Here are some habits I think will assist in the writing of LaTeX documents:

1. Always encapsulate your superscripts and subscripts with curly braces, even if they consist only of 1 characters. For example, it should x_{1} rather than x_1. The reason is that lot’s of time, I go back to changing my subscripts, and I often use a command like replace-string in emacs to help do it for the entire document. If I change the subscript from 1 character to multiple characters, thing gets messed up. For example, x_1 to x_i1 where it should really be x_{i1}. Using curly braces just help prevent things from going wrong and having to go back to the errors.
2. As mentioned on this post, it is best to keep each sentence on its own line to facilitate version controlling the document.

To help with these two tasks, I’ve binded my “.”, “\^”, and “_” keys to smart functions that will do what I want. Pressing the keys again will go back to the plain character.

 <pre class="example">;; following for latex, adapted from ess-smart-underscore


;; can also be implemented using sequential command http://www.emacswiki.org/emacs/SequentialCommand (defun tex-smart-underscore () “Smart \”\” key: insert \”{}\”. If the underscore key is pressed a second time, \”{}\” is removed and replaced by the underscore.” (interactive) (let ((assign-len (length “{“))) (if (and (>= (point) (+ assign-len (point-min))) ;check that we can move back (save-excursion (backward-char assign-len) (looking-at “{}”))) ;; If we are currently looking at ess-S-assign, replace it with _ (progn (forward-char) (delete-backward-char (+ 1 assign-len)) (insert ““)) (delete-horizontal-space) (insert “_{}”) (backward-char))))

(defun tex-smart-caret () “Smart \”\^\” key: insert \”\^{}\”. If the caret key is pressed a second time, \”\^{}\” is removed and replaced by the caret.” (interactive) (let ((assign-len (length “\^{“))) (if (and (>= (point) (+ assign-len (point-min))) ;check that we can move back (save-excursion (backward-char assign-len) (looking-at “\\^{}”))) ;; looking-at reads regexp, so need to escape the caret character ;; If we are currently looking at ess-S-assign, replace it with \^ (progn (forward-char) (delete-backward-char (+ 1 assign-len)) (insert “\^”)) (delete-horizontal-space) (insert “\^{}”) (backward-char))))

(defun tex-smart-period () “Smart \”.\” key: insert \”. \n\”. If the period key is pressed a second time, \”. \n\” is removed and replaced by the period.” (interactive) (let ((assign-len (length “. %%\n”))) (if (and (>= (point) (+ assign-len (point-min))) ;check that we can move back (save-excursion (backward-char assign-len) (looking-at “\. %%”))) ;; If we are currently looking at ess-S-assign, replace it with _ (progn (delete-backward-char assign-len) (insert “.”)) (delete-horizontal-space) (insert “. %%\n”))))

;; http://stackoverflow.com/questions/5500035/set-custom-keybinding-for-specific-emacs-mode ;; eval-after-load didn’t fix this for me ;; http://www.emacswiki.org/emacs/LaTeX (add-hook ‘LaTeX-mode-hook (lambda () (define-key LaTeX-mode-map (kbd “_”) ‘tex-smart-underscore) (define-key LaTeX-mode-map (kbd “\^”) ‘tex-smart-caret) (define-key LaTeX-mode-map (kbd “.”) ‘tex-smart-period) ) )

## Modify spacings in listing (itemize/enumerate) environments for LaTeX Beamer

I don’t usually worry about display options in LaTeX since it does a good job most of the time. However, when I write presentations using LaTeX Beamer, I make use of the itemize and enumerate environments quite often to list ideas, and the default setting in most of the templates lack generous spacing between items and between nested lists. The enumitem is the preferred method to modify spacing properties in list environments for LaTeX documents. However, Beamer has its own definitions for these environments for use with overlays, etc.

I posted questions regarding how to do so in Beamer here and here. Here is the complete solution. Place the following in the preamble of the document:

 <pre class="src src-sh">%% SPACING BETWEEN ITEMS FOR BEAMER



Similar modifications for Beamer are illustrated here.

## Version control and collaborating with LaTeX files

This post finally pushed me to explore ways to version control and collaborate with others using LaTeX files. I’m assuming the collaborators also use LaTeX, which is rare in itself when your primary collaborators are scientists that work mainly with WYSISWG editors, in particular, MS Word. I will outline how I collaborate with non-LaTeX users in a future post.

One issue arises with LaTeX files. When writing prose, I tend to write continuously on a line until a line break is necessary, usually when a new paragraph begins or when I introduce long math formulas. When diff is used to compare two LaTeX files, it is hard to see where a change occured in long line since diff looks at changes in lines; this problem is well documented.

The first obvious solution is to break up your lines. A fill-type solution will probably not work too well with diff with that 80 character restriction since a small change might affect more than one line. Comments from the original post suggests using a single line for each sentence. I think this is reasonable for readability and for diff to work. However, when collaborating with others, they might not abide by this preference.

The utility wdiff is another suggested solution that looks at differences in words instead of lines. The only problem I see with it is that the entire file will be printed, with words that are added and removed “highlighted” in the text using braces and brackets; its hard to see all the changes immediately with long files. This site shows how to add additional LaTeX markup that will highlight the changes visually on a generated dvi/ps/pdf file. The utility latexdiff also carries this out, and appears to be very popular among users. The output document is marked up similar to the “Track Changes” feature of your typical WYSIWYG editors.

latexdiff install on ubuntu:

<pre class="src src-sh">sudo apt-get install latexdiff


Moving forward, I will abide by the one sentence per line principle. However, I cannot force collaborators to do this, so will use wdiff and latexdiff to compare their changes. To assist in the first task, I can insert a new line after period using with the help of regexp-replace or replace-string on a region (two spaces to new line C-q C-j).

## Natbib reference sheet for citations

I use natbib in conjunction with BibTeX for LaTeX documents to facilitate the citing of articles in text. This reference is quite useful in illustrating all the features of natbib in a concise matter (I only used cite, citet, and citep before).