Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying something like this:

\begin{tikzpicture}[x=1cm,y=1cm]
    \newcommand{\mw}[1]{#1+1}
    \node [minimum width=\mw{1} cm] {};
\end{tikzpicture}

My problem is that this is expanded to 1pt + 1cm, but I want 2cm. The command \mw is simplified, the calculations are in fact more complex.

Any idea how i can achieve this?

EDIT: added parameter to command

EDIT2: What i actually want to do is some easy calculations for positioning

i defined two commands:

\newcommand{\col}[1]{#1 * 1.5}
\newcommand{\row}[1]{#1 * 1.1}

% and use it like this:
\node [minimum width=1.3cm] at (\col{2}, \row{3}) {};

It works like expected with the coordinates, but fails with minimum width.

share|improve this question

4 Answers 4

up vote 8 down vote accepted

Not 100% sure that I understand the intended application but the following (which defines alternative minimum width keys) may be of use:

\documentclass[tikz,border=5]{standalone}
\tikzset{%
  minimum width'/.code={%
    % Only advisable when x and y are orthogonal
    \pgfpointxy{#1}{0}%
    \tikzset{minimum width/.expanded=\the\csname pgf@x\endcsname}%
  },
  minimum width''/.style={minimum width=(#1)*1cm}
}
\begin{document}
\begin{tikzpicture}
\node [minimum width=3+1,   draw] at (0,0) {A};
\node [minimum width'=3+1,  draw] at (0,1) {B};
\node [minimum width''=3+1, draw] at (0,2) {C};
\end{tikzpicture}
\end{document

enter image description here

share|improve this answer
    
Yes! the second one did exactly what i wanted. –  Absurd-Mind Aug 18 at 10:44

Use pgfmath and do like this:

\documentclass{article}

\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}[x=1cm,y=1cm]
    \pgfmathsetmacro{\mw}{1+1}
    \node [draw,minimum width=\mw cm] {};
    \pgfmathsetmacro{\mw}{2+2}
    \node [draw,minimum width=\mw cm,yshift=1cm] {};
  \end{tikzpicture}

\end{document}

enter image description here

This is for the edit:

\documentclass{article}

\usepackage{tikz}
\newcommand{\col}[1]{#1 * 1.5}
\newcommand{\row}[1]{#1 * 1.1}
\begin{document}
  \begin{tikzpicture}[x=1cm,y=1cm]    
    \pgfmathsetmacro{\mw}{\col{2}+\row{3}}
    \node [draw,minimum width=\mw cm,anchor=west] at (0,0){};
    \draw[yshift=-0.5cm,|-|] (0,0) -- node[midway,below]{6.3} (6.3,0);
  \end{tikzpicture}

\end{document}

enter image description here

share|improve this answer

One option is \edef\mw{\number\numexpr 1+1\relax} as in:

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture}[x=1cm,y=1cm]
    \edef\mw{\number\numexpr1+1\relax}
    \node [minimum width=\mw cm] {};
\end{tikzpicture}

\end{document}
  • \edef stands for expandable macro definition, therefore the contents is expananded as \mw is defined.
  • \number tries to convert the following expression to a number, and it's necessary because:
  • \numexpr evaluates the following math expression, the expression should be with no spaces and ended by \relax (this is only one possibility, but the simplest one IMHO). The problem is that \numexpr itself wouldn't expand without being processed by something else, in this case, \number processes it.
share|improve this answer
    
I would also need parameters, \newcommand{\mw}[1]{#1+1}, is this also possible? –  Absurd-Mind Aug 18 at 8:38
    
No, \edef\mw#1{\number\numexpr #1+1\relax} is the way to go then. `\newcommand can't be used, well, it can, but it doesn't seem safe. As well, this will work only with integers, so I suppose Harish's solution is better. –  yo' Aug 18 at 8:40
    
@HarishKumar i've edited my question –  Absurd-Mind Aug 18 at 8:51

After Mark Wibrows answer i found 3 possible (easy) solutions for my problem:

% 1.
\node [minimum width=(\col{1})*1cm] {};

% 2.
\newcommand{\helper}[1]{(#1)*1cm}
\node [minimum width=\helper{\col{1}}] {};

% 3.
\tikzset{%
    minimum widthCM/.style={minimum width=(#1)*1cm}
}
\node [minimum widthCM=\col{1}] {};
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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