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.

Hi I'm trying to place same nodes relative to each other. I'm trying to place a node in the middle between two other nodes, but also half a cm to the right.

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{tikzpicture}
\node (p4) {$\hat{4}$} ;
\node[below=1cm of p4] (p3) {$3$} ;
\node[right=3cm of p4] (p1) {$\hat{1}$} ;
\node[below=1cm of p1] (p2) {$2$} ;
\node[below right={1/sqrt(2)}cm of p4] (v1) {$v_1$} ;
\end{tikzpicture}

I don't get why this isn't working... I get the following error

Package PGF Math Error: Unknown operator c' orcm' (in '{1/sqrt(2)}cm ').

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You must:

  • Add the positioning library.
  • Use \pgfmathparse to calculate the 1/sqrt(2) expresion.
  • Use \pgfmathresult where is the result of calculation.

    \documentclass{article}
    \usepackage{tikz}
    \usetikzlibrary{calc,positioning}
    \begin{document}
    \begin{tikzpicture}
    \node (p4) {$\hat{4}$} ;
    \node[below=1cm of p4] (p3) {$3$} ;
    \node[right=3cm of p4] (p1) {$\hat{1}$} ;
    \node[below=1cm of p1] (p2) {$2$} ;
    \pgfmathparse{1/sqrt(2)}
    \node[below right=\pgfmathresult cm of p4] (v1) {$v_1$} ;
    \end{tikzpicture}
    \end{document}
    

Or if you don't use some unit like cm, you don't need \pgfmathparse.

Edit

To avoid problems with \pgfmathparse{} and \pgfmathresult you can use your own macro with \pgfmathsetmacro{...}. This change avoids the problem showed by @percusse.

\begin{tikzpicture}
\node (p4) {$\hat{4}$} ;
\node[below=1cm of p4] (p3) {$3$} ;
\node[right=3cm of p4] (p1) {$\hat{1}$} ;
\node[below=1cm of p1] (p2) {$2$} ;
\pgfmathsetmacro{\myroot}{1/sqrt(2)} % <- 
\node[line width=3mm,below right=\myroot cm of p4] (v1) {$v_1$} ;
\end{tikzpicture}

Although this is not relevant to this specific problem, it may be useful for other cases.

share|improve this answer
    
Use [line width=3mm,below right=\pgfmathresult cm of p4] as the option and you'll see a nice problem with this. Use pgfmathresult as soon as possible before any other math computation intervenes. –  percusse Aug 21 at 11:45
    
@percusse Ah, i see, do you have an explanation for this? it seems better don't put units and to avoid using of \pgfmathresult. –  skpblack Aug 21 at 14:11
    
I tried to explain here a bit tex.stackexchange.com/a/191252/3235 TikZ can understand the math as in the other answer but you need to multiply it by 1cm or 1mm or 1pt at the end. So you don't need \pgfmathparse at all. –  percusse Aug 21 at 14:15

You have to use the positioning-library of tikz to position nodes relative to each other.

Try the follwing:

\documentclass[tikz, border=5mm]{standalone}
\usetikzlibrary{calc, positioning}

\begin{document}
 \begin{tikzpicture}
  \node (p4) {$\hat{4}$};
  \node[below=1cm of p4] (p3) {$3$};
  \node[right=3cm of p4] (p1) {$\hat{1}$};
  \node[below=1cm of p1] (p2) {$2$};
  \node[below right={1/sqrt(2)} of p4] (v1) {$v_1$};
 \end{tikzpicture}
\end{document}

Rendered Image

Update: If you want to use another unit than cm (in, pt, ...)

The above code uses tikz's default of 1cm as base unit. If you want to use another base unit for your picture you can set this in the tikzpicture's options. This way you don't need to set the unit after the calculation of sqrt() and inhibit the error.

\begin{tikzpicture}[x=1in, y=1in]
 \node[below=1cm] ... % use of cm
 \node[below=1]   ... % use of global unit (e.g. in)
 ...
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.