ckunte.net

# LaTeX

In order to make it a single source for all things LaTeX, I have updated this post numerous times since it was originally written — primarily as my setup instructions. I’ve added a toc to jump to the section of interest without having to scroll through the whole post.

## Template

See my templates repository.

## Small caps

Press Ctrl+F, select options .* and Aa, and enter regular expression search string [A-Z]{2,}, and press Find All.

Hit Ctrl+K, Ctrl+L for setting all to lowercase, and then type {} to enclose them all in, press Alt+← (left arrow) to go to the front of the word, and type \textsc, resulting in converting a word, say DAF to \textsc{daf}. (For the latter to work, first set the document to LaTeX.)

## Mac install

On the Mac, I download BasicTeX and with TeX Live Utility I install only packages I actually need for my type-setting needs. This in total occupies about 300MB of my disk. Whereas a full MacTeX install occupies 5GB, which I avoid.

The packages I generally need over those included in BasicTeX are as follows, which need to be installed one by one in TeX Live Utility:

datetime
ebgaramond
ebgaramond-maths
enumitem
pdfpages
pgfplots
placeins
pythonhighlight
sectsty
titling
newtx


The TeX Live Utility is really nice, but if I’m rushing, then I run a terminal command instead since it allows me to install them all in one go:

sudo tlmgr install datetime ebgaramond ebgaramond-maths enumitem pdfpages pgfplots placeins pythonhighlight sectsty titling newtx


See my wiki.

## Paper sizes

These are the paper sizes and margins I use in my documents using geometry package:

a4 = [8.3inch, 11.7inch]
b5 = [6.9inch, 9.8inch]
a5 = [5.8inch, 8.3inch]

a4m = 1.5inch
b5m = 1.00inch
a5m = 0.75inch

a4p = map(x - 2 * a4m, a4) => [5.3 inch, 8.7 inch]
b5p = map(x - 2 * b5m, b5) => [4.9 inch, 7.8 inch]
a5p = map(x - 2 * a5m, a5) => [4.3 inch, 6.8 inch]


To a question on why the default LaTeX margins so big, there’s a brilliant answer by Paul Stanley that is worth reading. In a gist, he says:

The real problem is with paper size. As it happens, we have ended up with paper sizes that were never designed or adapted for printing with 10-12 point proportionally spaced type. They were designed for handwriting (which is usually much bigger) or for typewriters.

I’ve unsuccessfully tried switching paper to either A5 or B5, because (a) these sizes are not easy to get, and (b) most printers (and certainly the ones I own) are designed not to print duplex in sizes (mostly) smaller than A4. It’s a shame!

## Syntax-highlighting python code

In notes or papers I write, I am often keen to include the underlying code, so both the final product as well as its underbelly is all there in one document for reference today and for future. Listing code is easy in .tex documents (between \begin{verbatim} and \end{verbatim}, for example), but highlighting code is a different story. For instance, despite overwhelming number of recommendations on the web to use minted, it never worked for me. A web search to highlight python code eventually led me to python-latex-highlighting repository, which works very well for my humble needs — my thanks to Prof. Olivier Verdier for sharing.

% Requires \usepackage{pythonhighlight} in the preamble
% The following ends with {start line no.}{end line no.}
\inputpython{query-motions.py}{1}{35}


This is handy, and keeps my .tex document lean and error free.

## Purge LaTeX compile files

#!/usr/bin/env bash
echo -n "Purge LaTeX compile files.."
find ~/. -name "*.log" -type f -delete
find ~/. -name "*.out" -type f -delete
find ~/. -name "*.aux" -type f -delete
find ~/. -name "*.gz" -type f -delete
echo "done."


## Linux (elementary OS) install

With our iMac dying, I got our tiny backup linux computer out. It is powered by Elementary OS. I got around to prep it for a family use last night — creating user accounts for kids and wife. Today I installed TeX Live 2014 over the internet, which I document here for future use.

sudo ./install-tl --location ftp://ftp.mpi-sb.mpg.de/pub/tex/mirror/ftp.dante.de/pub/tex/

First attempt for net installation failed;
repository url does not contain "tlnet",
retrying with "/systems/texlive/tlnet" appended.
Installing TeX Live 2014 from: ftp://ftp.mpi-sb.mpg.de/pub/tex/mirror/ftp.dante.de/pub/tex/systems/texlive/tlnet
Platform: x86_64-linux => 'x86_64 with GNU/Linux'
Using URL: ftp://ftp.mpi-sb.mpg.de/pub/tex/mirror/ftp.dante.de/pub/tex
Directory for temporary files: /tmp

======================> TeX Live installation procedure <=====================

======>   Letters/digits in <angle brackets> indicate   <=======
======>   menu items for commands or options            <=======

Detected platform: x86_64 with GNU/Linux

<B> binary platforms: 1 out of 21

<S> set installation scheme (scheme-full)

<C> customizing installation collections
47 collections out of 48, disk space required: 3734 MB

<D> directories:
TEXDIR (the main TeX directory):
/usr/local/texlive/2014
TEXMFLOCAL (directory for site-wide local files):
/usr/local/texlive/texmf-local
TEXMFSYSVAR (directory for variable and automatically generated data):
/usr/local/texlive/2014/texmf-var
TEXMFSYSCONFIG (directory for local config):
/usr/local/texlive/2014/texmf-config
TEXMFVAR (personal directory for variable and automatically generated data):
~/.texlive2014/texmf-var
TEXMFCONFIG (personal directory for local config):
~/.texlive2014/texmf-config
TEXMFHOME (directory for user-specific files):
~/texmf

<O> options:
[ ] use letter size instead of A4 by default
[X] allow execution of restricted list of programs via \write18
[X] create all format files
[X] install macro/font doc tree
[X] install macro/font source tree

<V> set up for portable installation

Actions:
<I> start installation to hard disk
<H> help
<Q> quit

Enter command: S


At this point, it’s really important to change the set installation scheme. Change from the default a — full scheme to c — small scheme (basic + xetex, metapost, a few languages) as below:

Select scheme:

a [ ] full scheme (everything)
b [ ] medium scheme (small + more packages and languages)
c [X] small scheme (basic + xetex, metapost, a few languages)
d [ ] basic scheme (plain and latex)
e [ ] minimal scheme (plain only)
f [ ] ConTeXt scheme
g [ ] GUST TeX Live scheme
h [ ] teTeX scheme (more than medium, but nowhere near full)
i [ ] XML scheme
j [ ] custom selection of collections

Actions: (disk space required: 3734 MB)
<Q> quit

Enter letter to select scheme: R


Then continue the install:

Actions:
<I> start installation to hard disk
<H> help
<Q> quit

Enter command: I


This would install about 253 packages in all, instead of 2875 packages (in full install), which can take a long time to install, and still miss important parts like xdvipdfmx, which are only available via option c. Then select C to include Additional fonts, which among other fonts includes EB Garamond, which is wonderful.

Once installed, TeX Live needs to be added to the path, and the best way to do it is to add it to .zshrc as below:

PATH=/usr/local/texlive/2016/bin/x86_64-linux:$PATH; export PATH MANPATH=/usr/local/texlive/2016/texmf-dist/doc/man:$MANPATH; export MANPATH
INFOPATH=/usr/local/texlive/2016/texmf-dist/doc/info:$INFOPATH; export INFOPATH  If one needs to do the same on a Mac, then the paths are slightly different as below: PATH=/usr/local/texlive/2016/bin/x86_64-darwin:$PATH; export PATH
MANPATH=/usr/local/texlive/2016/texmf-dist/doc/man:$MANPATH; export MANPATH INFOPATH=/usr/local/texlive/2016/texmf-dist/doc/info:$INFOPATH; export INFOPATH


Now reload the path with the following:

. ~/.zshrc


Now to add some packages I need:

sudo env PATH="\$PATH" tlmgr install fontspec marginnote realscripts sectsty xltxtra ulem


To compile a LaTeX file into a pdf:

xelatex somefile.tex


## Original post

The default MacTeX distribution is massive to get going for people on download limits or poor internet connections. Thankfully though, there’s a smaller distribution that can do nearly everything; and I found it handy for typesetting some personal documents at home.

But upon my first attempt at compiling my type set document (hit ⌘+R in Textmate), I got errors complaining about some missing packages. So, I wrote the following script to install these. With this, my typeset document now compiles.

#!/usr/bin/env bash
echo -n "Updating self, then all.."
sudo /usr/texbin/tlmgr update --self
sudo /usr/texbin/tlmgr update --all
echo "Installing packages.."
sudo /usr/texbin/tlmgr install fontspec marginnote realscripts sectsty xltxtra ulem
echo "done."


Trivia. In 2009, I saw one of our interns use a markup language to write her project documentation that I did not recognize. Curious, I asked her what it was, and why she took so much effort to write in it with so much markup when she could use any available GUI based advanced word processor software. Then, Anne showed me what it was and how it works. Until then, I knew nothing about TeX.

If it motivated a time-crunched intern to write in this software despite wide availability of other most used tools, then it said something about its superiority. This fact was interesting enough for me to dive-in to understand it better. And the best and most concise article I read about TeX was by Dario Taraborelli, which got me hooked completely. What an amazing and incredibly powerful tool; and how did I miss this in my nearly twenty years of computing life?

I now write everything worthwhile — offline documents, papers, notes, articles, and even letters to post in TeX in raw markup, thanks to Sublime Text editor’s amazing snippet support.