Tikz Understand white space between node and draw

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
usepackagetikz

begindocument
begintikzpicture[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);
endtikzpicture
enddocument



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.



enter image description here





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 at 12:39



tikz...


node[draw]...





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 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 at 12:49






You are nesting tikzpictures, what one should not do. That's the reason why this happens.
– marmot
Aug 24 at 12:59



tikzpictures





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 at 13:03




3 Answers
3



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.


node


inner sep


outer sep


0



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.


tikz


(0,0)


(1,0)


0


(0,0)


(1,0)


(-.1,-1.)


(1.1,.1)



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.


inner sep=0, outer sep=0



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.


line cap=rect



(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.


line cap=rect


documentclassarticle
usepackagetikz

begindocument

begintikzpicture[every node/.style=inner sep=0,outer sep=0, line cap=rect]
node at (0,0) (test) tikzdraw (0,0) -- (1,0);;
draw [inner sep=0,outer sep=0](test.east) --++ (1,0);
endtikzpicture

enddocument



At 1200% zoom:



enter image description here



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.


tikzpictures


saveboxes


documentclass[tikz,border=10pt]standalone
usepackagetikz
newsaveboxTikzBar
sboxTikzBartikz draw (0,0) -- (1,0);
begindocument
begintikzpicture[every node/.style=inner sep=0,outer sep=0]
node at (0,0) (test) useboxTikzBar;
draw (test.east) --++ (1,0);
endtikzpicture
begintikzpicture[every node/.style=inner sep=0,outer sep=0]
node at (0,0) (test) useboxTikzBar;
draw ([xshift=-pgflinewidth/2]test.east) --++ (1,0);
endtikzpicture
enddocument



enter image description here



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.


-pgflinewidth/2





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 at 13:12


pgflinewidth/2


line width=5mm





@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!
– marmot
Aug 24 at 13:15





I know you know. It's just about making sure it's clear for others.
– Loop Space
Aug 24 at 13:31






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)