Graphics, Figures & TablestikZ | Using Variables

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
ElWray
Posts: 3
Joined: Sun Mar 04, 2012 5:23 pm

tikZ | Using Variables

Post by ElWray »

I've just started using tikZ and I wonder if there is possibility to use variables in tikzpicture environment. In the pgf manual it's hard to find any information about this.

In my opinion variables create possibility to make more flexible programs.

For example we can declare a "point" variable:

Code: Select all

\coordinate (A) at (1,2);
But I can't change my A. For example I want my A point to be shifted 2cm to the right: A=(3,2). And my A point should have new coordinates after this:

Code: Select all

\coordinate (A) at (A) [xshifted=2cm];
But A is still the same: A=(1,2)

In MetaPost using variables was very easy if we compare it to tikZ.
I hope you know what I mean.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

tikZ | Using Variables

Post by cgnieder »

It's not entirely clear to me what you want to achieve. Does this help?

Code: Select all

\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
 \draw[help lines] (0,0) grid (3,2) ;
 \coordinate (A) at (1,2);
 \node at (A) {x} ;
 \coordinate (B) at ([xshift=2cm]A);
 \node at (B) {y} ;
\end{tikzpicture}

Even with the same coordinate name:
\begin{tikzpicture}
 \draw[help lines] (0,0) grid (3,2) ;
 \coordinate (A) at (1,2);
 \node at (A) {x} ;
 \coordinate (A) at ([xshift=2cm]A);
 \node at (A) {y} ;
\end{tikzpicture}

\end{document}
Regards
site moderator & package author
ElWray
Posts: 3
Joined: Sun Mar 04, 2012 5:23 pm

tikZ | Using Variables

Post by ElWray »

Yes, this is what I meant. Thanks a lot!

How about numeric variables? Is there a simple way to define them in tikzpicture environment?

For example:

Code: Select all

x=1; %i know it's not correct
y=2;

\coordinate (A) at (x,y); %now it should be A=(1,2)
\coordinate (A) at (x+2,y-1); %now A=(3,1)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

tikZ | Using Variables

Post by cgnieder »

I'm not at home right now so I can't look myself but in the pgfmanual is a section about calculating coordinates.
site moderator & package author
Post Reply