4

I'm using tikz package to make nice schemes presenting my work, and I am confronted to a little problem: manipulating variables.

I can do this the hard way:

For exemple let's say that I want to draw a rectangle (8*4),

%Defining nodes for geometry
\coordinate (left_minus) at (-4,-2) ;
\coordinate (left_plus) at (-4,2) ;
\coordinate (right_minus) at (4,-2) ;
\coordinate (right_plus) at (4,2) ;

%Drawing rectangle
\draw[line width= 1pt, fill=green!30] (left_minus) rectangle (right_plus)

but I would like to do something like this:

\newcommand{\Length}{8}
\newcommand{\Width}{4}
\coordinate (left_minus) at (-Length/2,-Width/2) ;
\coordinate (left_plus) at (-Length/2,Width/2) ;

I browsed the web but wasn't able to find something useful about manipulating variables in LaTeX. Do you guys have any idea about how I should proceed?

Thanks!

Aubry

6

Your approach should work (at least in TikZ) but you must use the macro names with a leading backslash \

\newcommand{\Length}{8}
\newcommand{\Width}{4}
\coordinate (left_minus) at (-\Length/2,-\Width/2) ;
\coordinate (left_plus) at (-\Length/2,\Width/2) ;

The shorter way I use usually is a \def inside of a {tikzpicture}. In this case the “lengthen” are simple macros in the sense of TeX and can be pares by TikZ as if you type them directly, i.e. (0,0.5*\h) would be the same as (0,0.5*2) but also (0,0.5\h) would be read as (0,0.25) so don’t omit the * sign. I use this way since it depends on the current setting of the basis vectors of TikZ. The drawback of \def is that it overwirtes existing macros but if you use it inside of {tikzpicture} the (re)definition is kept local and should be no problem. If you want to use the varaibales globally you may use \newcommand and longer variable names instead.

shorter way

The longer way uses really TeX lengthen, which can be used everywhere you need a length, e.g. in \hspace. But in this case you must add a unit and the lengthen won’t change when the basis vectors change.

longer way

Example:

\documentclass{article}
\usepackage{parskip}

\usepackage{tikz}

\begin{document}

\section{short way}
\begin{tikzpicture}
    \def\l{5}
    \def\h{2}
    \draw (0,0) rectangle (0.5*\l,\h/2);
\end{tikzpicture}

\begin{tikzpicture}[x=2cm,y=2cm]
    \def\l{5}
    \def\h{2}
    \draw (0,0) rectangle (0.5*\l,\h/2);
\end{tikzpicture}

\section{longer way}
\newlength{\mylength}
\setlength{\mylength}{5cm}
\newlength{\myheight}
\setlength{\myheight}{2cm}
\begin{tikzpicture}
    \draw (0,0) rectangle (0.5\mylength,\myheight/2);
\end{tikzpicture}

\begin{tikzpicture}[x=2cm,y=2cm]
    \draw (0,0) rectangle (0.5\mylength,\myheight/2);
\end{tikzpicture}

\end{document}
  • Thanks a lot! It was so obvious that I feel kind of stupid right now. – Aubry Aug 2 '13 at 8:14
  • 1
    This happens to all of use ;-) See my edit for some more information. – Tobi Aug 2 '13 at 8:22
  • 2
    The TeX.SE way to thank someone for an answer is to accept it (and vote it up). And there's no need to feel stupid. – Ethan Bolker Aug 2 '13 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.