Feeds:
Posts
Comments

If you are using PGF/TikZ (or any drawing language for (La)TeX such as PSTricks), you will soon find yourself either:

  • Re-compile an entire long LaTeX document just to see the effect of a small change you have made in a figure; or
  • Maintain a simplified master LaTeX file for every figure file (that’s 2 files for each figure) so that you can compile the figure quickly; or
  • Keep changing the preamble template for a tool like QtikZ to incorporate your custom definitions and packages for your figures.

Any of the above methods is cumbersome, time-consuming and inefficient. A much better way is to use the standalone package. For example, you are creating a figure in TikZ for your long paper. You put the figure code in a TeX file with a full preamble & the usual document environment, as if you are writing a small document with only one figure in it. But you don’t use the usual document class (e.g. article).  You use the class standalone instead, as follows:

% File myfigure.tex
\documentclass{standalone}
\usepackage{tikz}
\usepackage{mypackage}
\input{mydefs}
\begin{document}
\begin{tikzpicture}
  \draw [color=red] (0,0) rectangle (2,1) node [midway] {Cool};
\end{tikzpicture}
\end{document}

If you compile this file, you will get the same result as if you used the preview package. You can make small changes and re-compile only the figure quickly.

In you main LaTeX file, you must use the standalone package at the very beginning of the preamble, and input the figure file as usual. For example:

\documentclass{article}
\usepackage{standalone}
\usepackage{tikz}
\usepackage{mypackage}
\input{mydefs}
\title{Test of the \texttt{standalone} package}
\begin{document}
\maketitle
This is a figure.
\begin{figure}
  \centering
  \input{subfile}
  \caption{A figure.}
\end{figure}
\end{document}

You can compile this main file as usual. When being input, the preamble of the figure file (i.e. everything between \documentclass and \begin{document}) is skipped.

This method is more flexible and more convenient than the above methods. You maintain only one file per figure, and can edit and re-compile graphic code quickly.

To create a new custom command in LaTeX, the command \newcommand is well-known. Its full usage is:

\newcommand{\commandname}[n][d]{definition}

where

  • \commandname is the name of the new command, with backslash; this name must have never been defined.
  • n is the number of arguments of the new command; if ignored, there will be no argument.
  • d is the default value of the first argument; if not provided, the first argument will be required.
  • definition is the definition of the new command.

If the command name has already been defined, the above command will cause an error. To re-define a command, use \renewcommand instead.

Conditioning

In the definition of the new command (or in your LaTeX document in general), you may want to generate content differently based on certain conditions. For example, if the first argument of the command is empty, you want to do “this”, otherwise you want to do “that.” You can use standard TeX commands (TeX programming) for this task, but it is easier to use the package xifthen (an extended version of the package ifthen, you can also use the latter if you want). These packages define commands like


\ifthenelse{condition}{code if true}{code if false}

and commands for checking conditions, for example \isempty{} to check if something is empty.

As an example, the following command generates content differently based on its optional first argument:


\newcommand{\mycommand}[2][]{%

\ifthenelse{\isempty{#1}}%

{\ensuremath{{#2}_{\mathrm{on}}}}%

{\ensuremath{{#2}_{\mathrm{on}, {#1}}}}%

}

Usafe examples:

  • \mycommand{f} produces f_{\mathrm{on}}
  • \mycommand[i]{f} produces f_{\mathrm{on}, i}

Typesetting numerical data (with units) in LaTeX correctly can be difficult and time-consuming. Note that I emphasized “correctly.”

Fortunately, siunitx comes to the rescue. From its description:

Typesetting values with units requires care to ensure that the combined mathematical meaning of the value plus unit combination is clear. In particular, the SI units system lays down a consistent set of units with rules on how they are to be used. However, different countries and publishers have differing conventions on the exact appearance of numbers (and units). A number of LaTeX packages have been developed to provide consistent application of the various rules: SIunits, sistyle, unitsdef and units are the leading examples. The numprint package provides a large number of number-related functions, while dcolumn and rccol provide tools for typesetting tabular numbers.

The siunitx package takes the best from the existing packages, and adds new features and a consistent interface. A number of new ideas have been incorporated, to fill gaps in the existing provision. The package also provides backward-compatibility with SIunits, sistyle, unitsdef and units. The aim is to have one package to handle all of the possible unit-related needs of LaTeX users.

Some examples stolen from its manual:

  • \num{1+-2i} produces 1 \pm 2\text{i}
  • \num{.3e45} produces 0.3 \times 10^{45}
  • \si{kg.m.s^{-1}} or \si{\kilogram\metre\per\second} both produce \text{kg}\,\text{m}\,\text{s}^{-1}
  • \si[per-mode=symbol]{\kilogram\metre\per\second} produces \text{kg}\,\text{m} / \text{s}

These are just a few examples. The possibility is much larger and the package is very useful. So keep my words: “Always use siunitx when possible.”

Today I got an weird error:

! TeX capacity exceeded, sorry [input stack size=1500].

The error text is not very helpful (not at all), but the compiler stopped at a section command of the form:

\section{Some text $\bm{d}$}

So I searched on Google and found the cause of the error. It was caused by hyperref trying to convert the mathematical expressions and symbols in the section title to a PDF link in the table of contents. Somehow hyperref does not like mathematical text in section titles, especially bold symbols. There are several solutions, but the following two are the easiest:

  • Specify an alternative title for the section, usually called short title, and hyperref will use that for the link, like this:

    \section[Some text d]{Some text $\bm{d}$}

  • Specify pure text alternatives for the special symbols in the title, like this:

    \section{Some text \texorpdfstring{$\bm{d}$}{d}}

Note that the same error can happen with not only mathematical symbols but also other special fonts, such as \verb (as some people reported).

References:

Two LaTeX packages are very popularly used to typeset algorithms/pseudo-code (actually one is a set of related packages). The first is the algorithms package and its related packages. The second is the algorithm2e package.

The algorithms package provides two basic environments: algorithm for creating floats that contain algorithms, and algorithmic for typesetting actual algorithms. It is very straightforward and easy to use, however it is not flexible and does not provide ways to customize the output easily. The algorithmicx package provides the same algorithmic environment, but with greater flexibility.

The algorithm2e package provides environments for typesetting algorithms with ultimate flexibility. By default, the output looks very much like actual code. It is very customizable. However, it is more difficult to use, and its syntax is not very intuitive.

My advice:

  • If you don’t need to customize the output of your algorithms too much, don’t have very special needs, or just want to typeset your algorithms quickly, go with the algorithms family.
  • If you need very professionally looking algorithms, or you want to customize the output in a very special way, then you may want to go with algorithm2e (just MAY).

P.S: google the package name on CTAN and you will find the package. I didn’t put the links here.

The original article is at stackexchange: http://tex.stackexchange.com/q/10122/3048

The blkarray package can be found on CTAN.

The CV Inn is a very excellent resource for writing CV’s with LaTeX.

cool is a package that helps mathematical equations in LaTeX more meaningful. Also, it makes writing equations easier, especially those involve a lot of (high-order) derivatives, partial derivatives, etc. The name cool stands for COntent Oriented LaTeX. The only concern is that this package is not very popular, so it should be avoided if you want to share your LaTeX files with many people, or if you want to submit your files to a publisher who forbids non-standard packages.

The Arbitrary LaTeX Reference.

I wrote this post on my main blog a while ago. I copied it here because I think it’s more appropriate.

SyncTeX is a method for synchronization between (La)TeX source and its DVI/PDF file.  It is well and officially supported in TeXLive 2008. More information on using SyncTeX on Mac OS can be found here. This post targets Emacs+AucTeX (excellent LaTeX editor) and Skim (excellent PDF viewer) on Mac OS. Carbon Emacs (i.e. Emacs for Mac OS) can be downloaded from its website or from Apple’s website. Skim is located here.

Assuming that you already have Skim and Emacs+AucTeX up and running, this is how you enable SyncTeX in your LaTeX files and synchronize between them and their PDF files:

  • To enable SyncTeX in your LaTeX files, first you must have TeXLive 2008 (or any TeX distribution that supports SyncTeX) installed in your system. When you compile your LaTeX files, use the switch -synctex=1 in your call to latex. Alternatively, you can add a line \synctex=1 in the preamble of the master TeX file. The PDF file compiled from your LaTeX source will be accompanied by a SyncTeX file of the same name, which contains information necessary for the synchronization.
  • In Emacs, you must start the Emacs Server by issuing command server-start (M-x server-start <ENTER>).
  • You must configure Skim to synchronize with Emacs: menu Preferences/Sync/Preset to Emacs.
  • Open the PDF file in Skim. In Skim, hold Shift-Apple and click on any text in the PDF to go directly to the corresponding LaTeX source in Emacs.

That’s easy!

Older Posts »

Follow

Get every new post delivered to your Inbox.