Node or coordinate does not matter at the point when you use three \tikz
macros.
NB: If I say “TikZ picture” I mean either a solely \tikz
macro or the tikzpicture
environment.
Further reference on remember picture
and overlay
can be found in the PGF manual in section 16.13 “Referencing Nodes Outside the Current Pictures”, pp. 199f.)
As I already suggested in the comments, draw
the nodes and you will find that those at (0,0)
and at (\linewidth,0)
are just sitting together. (Depending on the line-ending %
s with a little space between them.)
Take a look at the following example:
Code I
\documentclass{article}
\usepackage{tikz}
\pagestyle{empty}
\usepackage[pass,showframe]{geometry}
\tikzset{every node/.style={
draw,
% inner sep=0pt,
}
}
\begin{document}
\noindent
\tikz \node (tkzlefta) at (0,0) {};% where am I?
\tikz \node (tkzrighta) at (\linewidth,0) {};% where am I?
\tikz \node[green] (bignumber) at (40cm,-1cm) {};% not on paper you say?
\noindent\tikz\draw (tkzlefta) -- (tkzrighta) -- (bignumber);
\end{document}
Output I
What happens here?
The whole TikZ picture will be cropped (“bounding box”) and put in one (TeX) box. The coordinates (x,y)
are not related to the physical paper.
Also take a close look at the line end. The horizontal line and the line to bignumber
are not connected, because the node is there.
If you add remember picture
to the \tikz
and without %
at the end of line (to have at least a little space between those nodes).
Code II
\documentclass{article}
\usepackage{tikz}
\pagestyle{empty}
\usepackage[pass,showframe]{geometry}
\tikzset{every node/.style={
draw,
% inner sep=0pt,
}
}
\begin{document}
\noindent
\tikz[remember picture] \node (tkzlefta) at (0,0) {};
\tikz[remember picture] \node (tkzrighta) at (\linewidth,0) {};
\tikz[remember picture] \node[green] (bignumber) at (40cm,-1cm) {};
\noindent\tikz\draw[blue] (tkzlefta) -- (tkzrighta) -- (bignumber);%
\tikz[overlay, remember picture]\draw[green] (tkzlefta) -- (tkzrighta) -- (bignumber);
\end{document}
Output 2
This looks better as it uses the actual boxes (that TikZ remembers) to connect.
What have we learned?
- TikZ outputs boxes, nothing else. The actual placement is up to TeX. (There is a special node
current page
, though.)
- Without
remember picture
TikZ only remembers the coordinates but not the picture (picture is either \tikz
or the tikzpicture
environment).
- With
remember picture
TikZ remembers the actual (comparatively) placement on TeX paper but still, the box is just typeset by TeX.
- With
remember picture
and overlay
TikZ finally connects the actual boxes!
- Do not use nodes.
But that is not what you are after anyway?
There are a few ways to get what you want.
The first solution uses only one TikZ picture. This is typeset by TeX.
So you will need an otherwise empty paragraph and \noindent
.
Here you can see that TikZ’s way to set the correct bounding box is not optimal as it apparently pads the line additional by .5\pgflinewidth
on both ends.
The yellow line solves this with \hspace
trickery but as \pgflinewidth
is only known inside a TikZ picture we need to use -.2pt
manually. (The standard line width (thin
) in TikZ is .4pt
.)
The green line uses TikZ \clip
path that is in fact a rectangular (the line has a width!).
The second solution actually places coordinates at the start and the end of the line (hfill
); this is also known as tikzmark (Google Search); see the third solution. These solutions need to be compiled at least twice!
The TikZmark macro I provide is a very rudimentary one.
There is a TikZ library that can deal with marks on different page, provides an extra coordinate system for “remembered” coordinates and and and …
Solution 1 (one TikZ picture)
\documentclass{article}
\usepackage{tikz}
\pagestyle{empty}
\usepackage[pass,showframe]{geometry}
\begin{document}
\noindent\hrulefill
\noindent\tikz\draw[red] (0,0) -- (\linewidth,0);% gives Overfull \hbox (0.4pt too wide)
\noindent\hspace{-.2pt}\tikz\draw[yellow] (0,0) -- (\linewidth-.5\pgflinewidth,0);%
\noindent\tikz{% do a line width change in the \tikz macro: \tikz[line width=1pt]
\clip (0,-.5\pgflinewidth) rectangle ++(\linewidth,\pgflinewidth);
\draw[green] (0,0) -- (\linewidth,0);
}
\end{document}
Output (solution 1)
Solution 2 (“TikZmark” but without the actual macro)
\documentclass{article}
\usepackage{tikz}
\pagestyle{empty}
\usepackage[pass,showframe]{geometry}
\begin{document}
\noindent\hrulefill
\noindent\tikz[remember picture]\coordinate (tikzleft);%
\hfill%
\tikz[remember picture]\coordinate (tikzright);%
\tikz[remember picture, overlay]\draw (tikzleft)--(tikzright);
% The following is not pictured in the image below.
\noindent\tikz[remember picture]\coordinate (tikzleft2);%
\hfill%
\tikz[remember picture]{
\coordinate (tikzright2);
\draw[overlay] (tikzleft2) -- (tikzright2);
}%
\end{document}
Solution 3 (\tikzmark
)
\documentclass{article}
\usepackage{tikz}
\pagestyle{empty}
\usepackage[pass,showframe]{geometry}
\newcommand*{\tikzmark}[2][]{%
\tikz[remember picture, #1]\coordinate (#2);%
}
\begin{document}
\noindent\hrulefill
\noindent\tikzmark{tikzleft}\hfill\tikzmark{tikzright}%
\tikz[remember picture, overlay]\draw (tikzleft)--(tikzright);
\end{document}
Output (solution 2 and 3)
coordinates
but now I'd like to know what happening in the other places where things are still not as I would like. What's making the difference between round I and round II? Why is round IV still not quite right?tikzpicture
', by putting the two nodes/coordinates and the\draw
command into a\begin{tikzpicture}
...\end{tikzpicture}
environment, or using the\tikz{...}
form (with braces) containing all three directives. Perhaps consult the manual to see how to do this.\noindent\tikz\draw (0,0) -- (\linewidth-\pgflinewidth,0);