[Tex/LaTex] How to place nodes in an absolute coordinate system in tikz

tikz-pgf

Edit: The example I originally gave was quite flawed – I'll leave it there but the second part is the bit I'm really struggling with.

Original Question
A similar question to this thread but I'm not using pgfplotsset so the solution didn't work for me.

I'm developing a tool to help me generate diagrams. For some reason, to draw the below diagram:

enter image description here

I have to generate the tikz:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}

\title{Diagram Practice}
%\author{Jack Turner}
\date{}

\begin{document}

\tikzstyle{box}=[minimum size = 1.25cm,right=10mm, rectangle, draw=black, fill=white, thick]

\begin{tikzpicture}
    \draw[black]    (1.,0.) -- (2.,0.);
    \node[box] (f)  at (1.,0.)      {$f$};

    \draw[black]    (3.25,-0.208333333333) -- (3.5,-0.208333333333);
    \draw[black]    (3.25,0.208333333333) -- (3.5,0.208333333333);
\end{tikzpicture}
\end{document}

It seems silly to me that I have to place a path from (1,0) — (2,0) for the wire coming in from the left when the box is placed at (1,0). Why is it not a path from (0,0) — (1,0)? Edit: this was because of the right = 10mm in my styling

Moreover, why doesn't the box get placed at the origin? Why does the path hit the box in the centre instead of being aligned to the bottom?

The scaling seems to get more complicated as the diagrams get larger – so if I have three boxes put next to each other and I try to connect them it's almost impossible to get the wires connecting them to actually touch the edge of the box.

Is there a way to stop tikz from making these relative adjustments?

Question 2

If I do:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}

\title{Diagram Practice}
%\author{Jack Turner}
\date{}

\begin{document}

\tikzstyle{box}=[minimum size = 1.25cm,right=10mm, rectangle, draw=black, fill=white, thick]

\begin{tikzpicture}
    \node[box] (f)  at (1.,0.)      {$f$};
    \node[box] (h)  at (7.,0)       {$h$};
\end{tikzpicture}
\end{document}

The nodes draw nicely:

enter image description here

But if I remove the left node:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}

\title{Diagram Practice}
%\author{Jack Turner}
\date{}

\begin{document}

\tikzstyle{box}=[minimum size = 1.25cm,right=10mm, rectangle, draw=black, fill=white, thick]

\begin{tikzpicture}
    \node[box] (h)  at (7.,0)       {$h$};
\end{tikzpicture}
\end{document}

The node shifts to where the f node used to be. How do I get h to stay in exactly the same spot? Is that something that LaTeX is doing to the diagram or is that a tikz thing?

Best Answer

tikzadjusts the image's bounding box automatically to fit all elements in your picture. If your tikzpicture contains just one element - no matter which coordinates it has - the bounding box gets reduced to this element. See the following examples - the document is structured as followed:

\documentclass[tikz, border=5mm]{standalone}
\usetikzlibrary{backgrounds}
\tikzset{
  box/.style={draw, minimum size=1.25cm},
  background rectangle/.style={draw, red}
}

\begin{docment}
  \begin{tikzpicture}[show background rectangle]
    <code here>
  \end{tikzpicture}
\end{document}

The background rectangleis used to show the bounding box of the tikzpicture.

Here your example of two nodes with the resulting bounding box:

\node [box] at (0,0) {a};
\node [box] at (7,0) {b};

first example

If you have just one element in your tikzpicture you get the following bounding box:

\node [box] at (7,0) {b};

second example

If you really need to customize your bounding box - i don't really see the need for it - you can use one of the following examples. But to get it working properly you have to do some work by yourself, like calculating the size of the bounding box and the right placement of the nodes in it.

For expanding your bounding box, you can add additional elements (like a rectangle) and set their opacity to 0:

\draw [opacity=0] (0,0) rectangle (7,1.25);
\node [box, anchor=south west] at (7,0) {b};

third example

Another possibility is to use the command \useasboundingbox, which lets you use a tikz-element as your custom bounding box:

\useasboundingbox (0,0) rectangle (8.25,1.25);
\node [box, anchor=south west] at (7,0) {b};

fourth example

Edit: Some additional ideas to drawing graphs

As you are using nodes to draw your image you can use them to connect them with paths. You don't have to specify every coordinate of a path by hand if you already got your node's coordinates. See the following example:

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

\tikzset{
  box/.style={draw, minimum size=1.25cm},
  double distance=12pt,
}

\begin{document}
  \begin{tikzpicture}
    \node (a) [box] at (0,0) {$a$};
    \node (b) [box] at (3,0) {$b$};
    \node (c) [box] at (3,3) {$c$};

    \draw [double] (a) -- (b);
    \draw [double] (a) |- (c);
    \draw [-<] (a) -- ++(-1,0);
    \draw (b) -- (c);
    \draw [->] (c) -- ++(1,0);
  \end{tikzpicture}
\end{document}

fifth example

Edit Example for nodes in a chain

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

\tikzset{
  box/.style={draw, minimum size=1.25cm},
  double distance=12pt,
}

\begin{document}
  \begin{tikzpicture}
    \node (a) [box] at (0,0) {$a$};
    \node (b) [box] at (3,0) {$b$};
    \node (c) [box] at (6,0) {$c$};

    \draw [double] (a) -- (b);
    \draw [double] (a) |- +(2,2) -| (c);
    \draw [-<] (a) -- ++(-1,0);
    \draw (b) -- (c);
    \draw [->] (c) -- ++(1,0);
  \end{tikzpicture}
\end{document}

sixth example