4

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.

8

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

  • Yes! the second one did exactly what i wanted. – Absurd-Mind Aug 18 '14 at 10:44
5

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

3

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.
  • I would also need parameters, \newcommand{\mw}[1]{#1+1}, is this also possible? – Absurd-Mind Aug 18 '14 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 '14 at 8:40
  • @HarishKumar i've edited my question – Absurd-Mind Aug 18 '14 at 8:51
1

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}] {};

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.