4

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?

5
  • 4
    Welcome to TEX.SE! Please provide a full minimal working example starting with \documentclass{...} and ending with \end{document} instead of code snippets. This makes our lives easier and increases the chance of people helping you.
    – campa
    Feb 20, 2017 at 15:37
  • 3
    Hi, I don't know exactly what you've done, because the code you show can't produce the image you show. If I were to guess, I'd assume you have an xshift in the box style, but please don't make us reverse engineer your code, it makes it harder and more time consuming to answer, and it's a pointless exercise to start with. As for the box placement, nodes are by default placed quite logically, with the center point in the specified coordinate. You can change that by specifying an anchor for the node though. Feb 20, 2017 at 18:10
  • 1
    I couldn't find a box shape. Circuitiz has a box option, but it still doesn't look like the image provided. Feb 20, 2017 at 18:16
  • Sorry guys! I've added the minimal working example now. And thanks Torbjørn - the xshift was the problem!
    – J. Turner
    Feb 21, 2017 at 13:51
  • 1
    Just a quick note about question 2: The coordinates in a tikzpicture relate by default only to the tikzpicture itself, they have no relation to the page. The tikzpicture is a rectangular box that fits around its content, and LaTeX just sees a box, that it places on the current line just as any other box, e.g. an \includegraphics, or the letter X. You can place stuff relative to the page though, see e.g. tex.stackexchange.com/questions/89588/… Feb 21, 2017 at 14:25

1 Answer 1

8

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

2
  • Thank you so much for this! I have a question about your extra edit: is there any way to get tikz to detect nodes that block paths/re-route them? For example, if I have boxes a, b and c in a row and I want to connect a to c, with the path going over b, will I have to compute the path myself? Thanks!
    – J. Turner
    Feb 21, 2017 at 16:52
  • Yes, you need to do the computing on your own. As far as i know there is no collision detection in tikz. See update of the answer
    – moospit
    Feb 21, 2017 at 16:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .