howto

Tip of the day: Log tweek for Apache+Squid

I recently reconfigured my server to run Squid in front of Apache in hopes that it will hold up under load a little better now (Polvi's site is just too damn popular with web crawlers). To keep people who like reading their Apache logs happy I wanted to make Apache log the original client instead of 127.0.0.1. Here's the best I've come up with so far:

# Set using the normal REMOTE_ADDR value first
SetEnvIf Remote_Addr "(.*)" TRUE_REMOTE_ADDR=$1
# Pick the last ip address off the proxy list if we can
SetEnvIf X-Forwarded-For "([0-9\.]+)$" TRUE_REMOTE_ADDR=$1

LogFormat "%{TRUE_REMOTE_ADDR}e %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

Seems to be working so far :-)

Tip of the day: Using GPG trasparently in vim

Vim's built in encryption feature is pretty handy but it's algorithm is only strong enough to protect against a casual observer. For good encryption something else is required but I still want something that is fairly transparent. I've come up with a little solution using GnuPG, just add the following to your .vimrc to automatically encrypt and decrypt *.gpg files. (Replace my key id with yours of course)

au BufNewFile,BufReadPre *.gpg :set secure viminfo= noswapfile nobackup nowritebackup history=0 binary
au BufReadPost *.gpg :%!gpg -d 2>/dev/null
au BufWritePre *.gpg :%!gpg -e -r CB06CE30 2>/dev/null
au BufWritePost *.gpg u

A Basic LaTeX HOWTO

Introduction

LaTeX is a typesetting language that is particularly well suited for math, computer science, and other technical documents. It can be used for building anything from an entire book to small homework assignments. As a computer science student with horrible handwriting, I like to type up most of my homework. For many things plain text will work great, but when equations and graphs are involved LaTeX is a good tool. LaTeX is also a useful replacement for writing in a word processor.

This HOWTO will focus on using LaTeX on a typical Linux system. LaTeX is also available for Windows and OS X. A great deal of the information here will still apply to those systems, but I am not covering them.

Required Software

All you need is a plain text editor and a LaTeX package. The most common package is tetex, and your Linux distribution should have packages for it. If not, it probably includes it in some other version such as ptex. Just install it like any other program:

apt-get install tetex
  OR
emerge tetex
  OR
[Your package manager command here, you get the idea.]

Many text editors include extra features, such as syntax highlighting and macros to make working with LaTeX easier. One such set of macros for vim is the vim latex suite. Any extra features are of course optional, but I highly recommend enabling syntax highlighting at the very least.

Getting Started

Let's start off with a very basic document:

% Example #1
% by LaTeX newbie
\documentclass[letterpaper]{article}

\author{\LaTeX Newbie}
\title{A Quick Example}

\begin{document}
\maketitle

\section{Big Important Things}
\subsection{My point really is that pigs can fly}
Flying pigs are pretty darn awesome. Therefore they must exist!

\subsection{Cows cannot fly}
Cows are just not that cool, so to the pasture with them!

\end{document}

Paste the above into your favorite editor and save the file as example1.tex. The code can then be compiled and viewed with the commands:

latex example1.tex
xdvi example1.dvi

The latex command generates example1.dvi along with a few extra temporary files. The dvi format is the default compiled form for LaTeX, but nothing other than LaTeX uses it so it is not all that useful. Instead pdflatex can be used to generate PDF files:

pdflatex example1.tex
xpdf example1.pdf

You may substitute xpdf for your favorite PDF viewer. Generating PDF files also has the added advantage converting very well to PostScript, which is the standard format most printers use. I recommend only using pdflatex.

Basic Structure and Syntax

Let's take a closer look at the first example above. At the very top, I have included the little header:

% Example #1
% by LaTeX newbie

Any text after a '%' to the end of that line is a comment and will be ignored by the LaTeX processor. Comments are generally useful for marking what a document is and making notes to yourself within the text. If you need to write the real % symbol in your text use '\%'.

The beginning of a document must start with something like:

\documentclass[letterpaper]{article}

This sets the document type to 'article' which is useful for most documents. For longer documents the style and features can be altered slightly by switching to the 'report' or 'book' document class. For presentations there is also a 'slides' class. We will be sticking to the 'article' class for this HOWTO. The text in square brackets lists options for the document class. In this example 'letterpaper' sets the paper size to US Letter. If you live anywhere in the world other than the US, the option 'a4paper' will be more useful. Other options include things like '11pt' for setting the font size. For someone who likes big fonts and lives in Europe the following would be useful:

\documentclass[a4paper,20pt]{article}

The current type of text is defined by '\begin' and '\end' commands. These commands are used to mark the beginning and end of the body of the document:

\begin{document}
[All the text...]
\end{document}

For general articles the standard document header can be created with the commands:

\author{\LaTeX Newbie}
\title{A Quick Example}
\maketitle

Adding numbered section headers to the document is simple:

\section{Big Important Things}
\subsection{My point really is that pigs can fly}

The rest is just plain text. Paragraphs are separated by a blank line and line breaks can be inserted without creating a new paragraph with '\\', for example:

This is the first paragraph.
This is more of that first paragraph, but there is no forced new line.

Here we start a new paragraph. \\
This is a new line, but still part of the same paragraph.

By default LaTeX indents paragraphs, uses single spacing for lines, and has no extra space between paragraphs. The next section covers how to change this behavior.

Formatting Tweaks for Homework

The default style is designed for published documents but looks a bit odd for small assignments. The most obvious change is to simplify the document header a bit, replacing the default '\maketitle' style with:

\noindent
\LaTeX Newbie \\
AA201 -- Winter 07 \\
Homework \#4 \\

The first real paragraph begins here!

Note the use of '\noindent' at the beginning to tell LaTeX not to indent the name LaTeX Newbie as it would for a normal paragraph.

When section headings don't need to be numbered change '\section' to '\section*' like the following:

\section*{Big Important Things}
\subsection*{My point really is that pigs can fly}

Written assignments are typically expected to be double spaced, with extra spacing between paragraphs. To set this up place the following commands in the body of the document following the '\begin{document}' line:

\setlength{\baselineskip}{1.6\baselineskip}
\setlength{\parskip}{4ex}

Also, if paragraphs should not be indented at all use the following:

\setlength{\parindent}{0pt}

Typesetting Math

LaTeX is very good at typesetting math. I will only cover some basics here, but when it comes to typing up a pretty math equation LaTeX will be able to do anything you want.

Inline equations are marked with the symbol '$'. For example:

The equation $E = mc^2$ is pretty famous.

As with '%', if you need a real $ character use '\$' instead.

To make an equation stand out by centering it on its own line use '\begin{displaymath}':

The following equation is pretty famous:
\begin{displaymath}
  E = mc^2
\end{displaymath}

If you have a series of equations they can be aligned nicely with an array:

\begin{displaymath}
  \begin{array}{rcl}
    a^2 + b^2 & = & c^2 \\
    \sqrt{a^2 + b^2} & = & \sqrt{c^2} \\
    \sqrt{a^2 + b^2} & = & c \\
  \end{array}
\end{displaymath}

The array argument '{rcl}' creates three columns, the text in the first one is aligned to the right, the next one is centered, and the last one is aligned to the left. In the text itself the column edges are marked by '&' and new rows are started with '\\'. Laying out the equations in this way aligns them to the '=' signs which are in a vertical line down the middle.

Further Reading

LaTeX is a very powerful typesetting language. We have only covered the basics to get started here but there is a lot more that can be done. An excellent reference for doing almost anything is the "Not So Short Introduction to LaTeX 2e" available at: http://www.ctan.org/tex-archive/info/lshort/english/lshort.pdf

Syndicate content