Typography with TeX and LaTeX

## Decorate a TikZ path

November 5th, 2011 by Stefan Kottwitz

How to decorate a \path?

My question is about drawing a “commutative-type diagram” in mathematics. I would like to integrate an arrow of the type \rightsquigarrow, which is described how to be obtained here: Squiggly arrows in TikZ.

The problem is that I use \path to connect the entries of my matrix in TikZ (because this part comes integrated in a bigger diagram), and I get an error:

“Package PGF Error: I cannot decorate an empty path.”

The code is

\documentclass{article} \usepackage{tikz} \usetikzlibrary{decorations.pathmorphing} \usepackage{amssymb} \begin{document} \begin{tikzpicture} \matrix (m) [matrix of math nodes, row sep=3em, column sep=4em, text height=2ex, text depth=0.25ex] { U & U \\}; \path [->, font=\scriptsize, line join=round, decoration={zigzag,segment length=4, amplitude=.9,post=lineto,post length=2pt}, decorate] (m-1-1) edge node[auto] {F} (m-1-2); \end{tikzpicture} \end{document}

where the decorate part does not work.

You could specify the decoration to the path, and give the edge the decorate option:

\path [->, decoration={zigzag,segment length=4,amplitude=.9, post=lineto,post length=2pt},font=\scriptsize, line join=round] (m-1-1) edge[decorate] node[auto] {F} (m-1-2);

Category: TikZ | No Comments »

## Matrix multiplication with LaTeX

April 18th, 2011 by Stefan Kottwitz

How can I draw a diagram for matrix multiplication with LaTeX?

I’m trying to make a PDF manual for matrix multiplication with LaTeX. I’d like to generate a graphic similar to this one (from Wikipedia/Matrix multiplication):

I guess this will be quite a bit of work, but is there another way than using the eepic package? I have read into some examples and the manual, and it seems very complex to generate a graphic like this with that package.

If there is no other way, I’ll have to use eepic, but I hope someone here could point me to a package or another way.

I recommend to use TikZ and its matrix library, specifically the matrix of math nodes feature. TikZ offers a great and comprehensive manual.

Have a look at this example and its source code, written by Alain Matthes, in the TikZ example gallery: Matrix multiplication. It deals exactly with your subject.

Category: TikZ | No Comments »

## Circled numbers in text

Dezember 13th, 2010 by Stefan Kottwitz

What is a good way to make circled numbers in text?

I’m trying to make some good-looking numbers in a circle, using the simple command:

\textcircled{1}

However, the circle is misaligned vertically with the number, and look a bit sloppy:

Any suggestions how can I adjust the vertical alignment so that the numbers look more centered w.r.t to the circles? Or, if that’s not an option, what other ways to achieve the same results are possible?

I will use those only for numbers, and in very few places, so manual adjustment per glyph is an option.

I’m using pdfLaTeX with Computer Modern, if that matters.

Here’s a TikZ solution:

\documentclass{article} \usepackage{tikz} \newcommand*\circled[1]{\tikz[baseline=(char.base)]{ \node[shape=circle,draw,inner sep=2pt] (char) {#1};}} \begin{document} Numbers aligned with the text: \circled{1} \circled{2} \circled{3} end. \end{document}

It’s just a node. TikZ options are used to align the base line, to adjust the size and to get the circle shape. You’re free to choose further options regarding size or circle thickness (option thick). There’s more: for example you could even name the nodes by another argument to connect them by arrows later.

If one like to use it for an enumerated list, for example, it’s easy but has to be protected:

\usepackage{enumitem} ... \begin{enumerate}[label=\protect\circled{\arabic*}] \item First item \item Second item \item Third item \item Fourth item \end{enumerate}

Category: TikZ | 1 Comment »

## Harpoons in TikZ

Dezember 11th, 2010 by Stefan Kottwitz

Can I have any harpoons in TikZ?

I refer to my previous question on harpoons: “How to draw protocol interaction” using amsmath. The closest thing I have found is this code:

\begin{tikzpicture}[decoration={bent,aspect=.3}] \node[circle,draw] (A) at (.5,.5) {A}; \node[circle,draw] (B) at (3,1.5) {B}; \draw[->,decorate] (A) -- (B); \draw[->,decorate] (B) -- (A); \end{tikzpicture}

There are so called partial arrow tips in TikZ. Use for instance left to, right to, left hook or right hook. They are described in section 23.7 of the pfg 2.10 manual (22.6 in the pgf 2.0 manual).

\documentclass{article} \usepackage{tikz} \usetikzlibrary{arrows} \begin{document} \begin{tikzpicture} \node[circle,draw] (A) at (.5,.5) {A}; \node[circle,draw] (B) at (3,1.5) {B}; \draw[-left to] (A.north east) -- (B.west); \draw[-left to] (B) -- (A); \end{tikzpicture} \end{document}

Category: TikZ | No Comments »

## Scaling and fitting a box

Dezember 3rd, 2010 by Stefan Kottwitz

Why does fit not scale?

I have a problem with the fit library in TikZ.

When I scale the figure (with the TikZ scale command), the fit box doesn’t!

Why is that? Could I somehow change it?

Example:

\documentclass{article} \usepackage{tikz} \usetikzlibrary{fit} \begin{document} \begin{tikzpicture}[state/.style={circle,draw,thick, loop above,inner sep=0,minimum width=10}] \node[state] (a) at (0,1) {} edge [loop above, thick] coordinate (al) (); \node[state] (b) at (1,1) {} edge [loop above, thick] coordinate (bl) (); \node[draw,rectangle,fit=(a) (b) (al) (bl)] {}; \end{tikzpicture}   \begin{tikzpicture}[scale=2,state/.style={circle,draw,thick, loop above,inner sep=0,minimum width=10}] \node[state] (a) at (0,1) {} edge [loop above, thick] coordinate (al) (); \node[state] (b) at (1,1) {} edge [loop above, thick] coordinate (bl) (); \node[draw,rectangle,fit=(a) (b) (al) (bl)] {}; \end{tikzpicture} \end{document}

Add the option transform shape to scale also the box resp. shapes:

\begin{tikzpicture}[scale=2,transform shape, state/.style={circle,draw,thick,loop above,inner sep=0, minimum width=10}]

Output:

Category: TikZ | No Comments »

## Trees in TikZ

August 26th, 2010 by Stefan Kottwitz

How to make a three level deep tree with TikZ?

I am busy making a full binary tree of three levels deep.

The code I am having now is (using the trees library):

\begin{tikzpicture} \node {root} child {node {left} child {node {lleft}} child {node {rleft}} } child {node {right} child {node {lright}} child {node {rright}} }; \end{tikzpicture}

The problem is that rleft and right are printed over each other.

Preferably I would like TikZ to figure this out by itself, for example if I give a minimum distance between nodes on the same level. Is this possible? (My final nodes will not have text but will be fixed size shapes)

Off course it can be that the solution is: “Don’t use ‘trees’”. In that case, what is the best way to do this?

You could specify options for each level, for instance silbling distance but also level distance.

Example:

\documentclass{article} \usepackage{tikz} \usetikzlibrary{trees} \begin{document} \begin{tikzpicture}[level distance=1.5cm, level 1/.style={sibling distance=3cm}, level 2/.style={sibling distance=1.5cm}] \node {root} child {node {left} child {node {lleft}} child {node {rleft}} } child {node {right} child {node {lright}} child {node {rright}} }; \end{tikzpicture} \end{document}

Category: TikZ | No Comments »

## Checkers board with TikZ

August 18th, 2010 by Stefan Kottwitz

What is the best way to draw a checkers board in TikZ?

I’m interested in what is the best way to draw many circular pieces on board. The checker background is not important.

I suggest to use a matrix of nodes from the TikZ matrix library.

\documentclass{article} \usepackage{tikz} \usetikzlibrary{matrix} \begin{document} \tikzstyle{ball} = [circle, shading=ball, ball color=black!80!white, minimum size=1cm] \begin{tikzpicture} \matrix (m) [matrix of nodes,nodes=ball] { {} & & {} & & {} & & {} & \\ & {} & & {} & & {} & & {} \\ {} & & {} & & {} & & {} & \\}; \end{tikzpicture} \end{document}

Output:

Category: TikZ | No Comments »

## Aligned equations inside of a TikZ node

August 8th, 2010 by Stefan Kottwitz

How can I create a tree node with aligned math equations inside?

\begin{align} \end{align}

doesn’t work and

\begin{minipage}{100} \begin{align} ... \end{align} \end{minipage}

gives a lot of margin and I don’t want to manually tune.

\nodepart from shapes tikz library seems to be overkill and doesn’t do the alignment.

Any ideas?

\documentclass{article} \usepackage{tikz} \usepackage{amsmath} \begin{document} \begin{tikzpicture}[every node/.style={rectangle,draw}] \node {Example:} child {node {% \begin{aligned} a &= bx + c\\ a+b &= d +1 \end{aligned}}}; \end{tikzpicture} \end{document}