# Tikz Understand white space between node and draw

I am new to Tikz and during some experiments I ended doing something like the example below but I can' quite understand why is there a space left between my two lines...

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[every node/.style={inner sep=0,outer sep=0}]
\node at (0,0) (test) {\tikz \draw (0,0) -- (1,0);};
\draw (test.east) --++ (1,0);
\end{tikzpicture}
\end{document}


It is very simple example of what I wanted to do and maybe I took it the wrong way and should not proceed this way but I would love to know if it possible to eliminate this space or not.

• For future reference, it is much easier for people to hep you if you post a minimal working example that, in particular, compiles. Although it is "legal" to put \tikz{...} inside a node it is certainly unusual. You problem here is that the node has a border as you can see using \node[draw]... which is why there is a small gap. – Andrew Aug 24 '18 at 12:39
• Welcome to TeX.sx! Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. Additionally, you don't need to sign off with your name as this is already at the bottom right hand side of your post. – Milo Aug 24 '18 at 12:42
• I will take your remark into account for the next post ! For the border I saw it but I thought that adding inner and outer sep to zero would do the job... In fact in my real case I wanted to put a pic I created with tikzset inside the node to be able to connect it rather easily to other objects... But maybe I am on the wrong direction – Gautier Bureau Aug 24 '18 at 12:49
• You are nesting tikzpictures, what one should not do. That's the reason why this happens. – user121799 Aug 24 '18 at 12:59
• You shouldn't need to nest a pic inside a node. There is almost certainly an alternative solution (but without knowing why you think you should nest a pic in a node then I don't know what to propose). Ask another question on that. (Don't edit this one - it's a good question to ask.) – Loop Space Aug 24 '18 at 13:03

This is an interesting case! I originally thought it was due to the fact that, as @Andrew says in a comment, \node has a border. However, that border can be removed by setting inner sep and outer sep to 0 and this is exactly what you do. So the node does not have a border.

What is going on is that the inner \tikz picture has a border. Exactly why this is so is quite subtle. When TikZ builds a picture then internally it keeps track of the size of that picture. The main way of doing so is to keep track of a rectangle that is big enough to contain every coordinate that has been used, this is enough to ensure that every path is inside the picture (there are some questions about this with relation to Bezier curves which are an interesting read). However, when a line is drawn then it is drawn with a thickness and so if a path goes between, say, (0,0) and (1,0) then its maximum height will actually be a half linewidth above 0. So when TikZ adds a point to the bounding box, it actually adds a half linewidth beyond it. So when you create a picture with coordinates (0,0) and (1,0) and (for ease of explanation) line width 2mm, the bounding box will have corners (-.1,-1.) to (1.1,.1). And this extends a bit beyond the line.

When this is embedded in another node (I would go one stronger than @Andrew and say that nesting TikZ pictures should be avoided), the outer TikZ doesn't know anything about what the inner TikZ drew and just knows about the box size. So it creates a node big enough to contain that box. Even though you make that node as tightly fitting as possible (via inner sep=0, outer sep=0) you don't make it touch the inner line.

The solution proposed by @AboAmmar gets round this by making the line in the inner TikZ picture extend to the boundary of the inner picture since line cap=rect ensures that the line extends by half a line width beyond the coordinate. This works in the case of horizontal/vertical lines as then the overreach of the line exactly matches the excess added to the box, but the situation with a diagonal line might need more adjustment.

(I'm actually going to remember this one as another example of why nesting tikz pictures can cause strange effects and therefore is Not A Good Idea. If you have a situation where you think you need to nest tikz pictures, please ask here first and someone will come up with an alternative solution!)

In this particular case, you can add line cap=rect and that tiny gap will disappear.

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[every node/.style={inner sep=0,outer sep=0}, line cap=rect]
\node at (0,0) (test) {\tikz\draw (0,0) -- (1,0);};
\draw [inner sep=0,outer sep=0](test.east) --++ (1,0);
\end{tikzpicture}

\end{document}


At 1200% zoom:

As written in my comment and Loop Space's nice answer, nesting tikzpictures should be avoided. The standard way to do that is to work with \saveboxes. Of course, a node has a border given by the line width.

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\newsavebox\TikzBar
\sbox\TikzBar{\tikz \draw (0,0) -- (1,0);}
\begin{document}
\begin{tikzpicture}[every node/.style={inner sep=0,outer sep=0}]
\node at (0,0) (test) {\usebox\TikzBar};
\draw (test.east) --++ (1,0);
\end{tikzpicture}
\begin{tikzpicture}[every node/.style={inner sep=0,outer sep=0}]
\node at (0,0) (test) {\usebox\TikzBar};
\draw ([xshift=-\pgflinewidth/2]test.east) --++ (1,0);
\end{tikzpicture}
\end{document}


As you see, once you avoid the nesting of tikzpictures, the gap is almost gone, and if you correct for the border by moving the line closer by -\pgflinewidth/2 it disappears completely.

• Note that the \pgflinewidth/2 needs to refer to the line width that is in effect when the box is created. If you put, say, line width=5mm on the inner picture (in the box) then you'll see what I mean. – Loop Space Aug 24 '18 at 13:12
• @LoopSpace Absolutely. I am aware of this. In this case, this is the right dimension, though, and I thought it would be easier to understand. Thanks! – user121799 Aug 24 '18 at 13:15
• I know you know. It's just about making sure it's clear for others. – Loop Space Aug 24 '18 at 13:31