latex

a little bit of tikz/pgf

Sat, 2012-05-26 15:32

I've been working with tikz/pgf, a system for creating figures in LaTeX. I've been encouraged to do this partly as a result of my frustration with Inkscape, which is excellent in places, but crashes often and frequently fails to be able to do exactly the thing I need.

Today, I figured out the use of variables in tikz. Here is an example:

\documentclass[12pt]{report}
\usepackage{tikz}
\begin{document}

%%% this is the tikz code
\begin{tikzpicture}[auto,scale=1]
\def\n{10}; % the number of circles
\def\wid{5}; % the width of the drawing
\def\r{2.0}; % radius of each circle
\foreach \j in {1,2,...,\n} {
\foreach \i in {1,2,...,\n} {
\pgfmathparse{1.0*\i/(\n)*360}\let\ang\pgfmathresult;
\pgfmathparse{ 0.5*\wid*(\j)/(\n)*cos(\ang) }\let\xx\pgfmathresult;
\pgfmathparse{ 0.5*\wid*(\j)/(\n)*sin(\ang) }\let\yy\pgfmathresult;
\draw (\xx,\yy) circle (\r);
}
}

\end{tikzpicture}
%%% end of tikz code

\end{document}

This example shows the use of variables, nested for loops, and mathematical calculations.

Here is the output:

latex shell script

Sun, 2008-08-17 15:53

Okay, so I should have learned this about 20 years ago, but I didn't figure it out until today, and thought others might be helped by it.

I use latex for all "word processing" since I write a lot of math. Most of the time I work on my Mac OS X laptop in "terminal", so I have a command line. To "tex" a latex file, I would type

latex file.tex

and then

dvipdf file

to turn it into a pdf, which I then view with Acrobat Reader. This works really well, and even for large documents, is very fast. The down side is I have to type two commands. In addition, I often need to "tex" the file more the once to get references correct. This means more commands to type. Finally, I decided that I should write a shell script to do this for me. I'd never written a shell script before, but I correctly figured, "How hard could it be?". Here's the result:

#! /bin/sh
FIRST_ARGUMENT="$1"
SECOND_ARGUMENT="$2"
for ((i=1; i<=$FIRST_ARGUMENT; i++))
do
latex $SECOND_ARGUMENT
done
echo ""
echo "Now making pdf..."
dvipdf ${SECOND_ARGUMENT%%.tex}

It's pretty self-explanatory except perhaps the last line. This removes the ".tex" extension from the tex file before executing the dvipdf command.

I put this script (I called it "texit") in /sw/bin (you would want to do a "sudo mv" to that location if you do not automatically have admin privileges) and made it executable. It works like this, for example:

texit 2 file.tex

The action is to "tex" file.tex twice and then create a pdf from the dvi file. It works great! Hooray! One command instead of three!