UdiB
I want to draw a tikzpicture with nodes positioned relative to text width of the document. To achieve it I am setting a bounding box:
```tex
\documentclass{article}
\synctex=0
\usepackage{tikz,lipsum}
\usetikzlibrary{positioning}
\begin{document}
\lipsum[1]
\vskip 10pt
\hrule
\noindent
\begin{tikzpicture}
\path (0,0) (\textwidth,0);% set bounding box
\node(n1) [draw=black,outer sep=0pt,anchor=north] at (current bounding box.west) {node 1};
\node (n2) [draw=black,outer sep=0pt,anchor=east] at (current bounding box.east) {node 2};
\filldraw (n2.north east) circle (1pt);
\end{tikzpicture}
\end{document}
```
I need help with placing `node 2` flush right with the text area.

Without any node,
```
\noindent
\begin{tikzpicture}
\path (0,0) (\textwidth,0);% set bounding box
\filldraw (current bounding box.north east) circle (1pt);
\end{tikzpicture}
```
I get the small circle flush right with the text area.
Once I add `node 1`,
```
\noindent
\begin{tikzpicture}
\path (0,0) (\textwidth,0);% set bounding box
\node(n1) [draw=black,outer sep=0pt,anchor=north] at (current bounding box.west) {node 1};
\filldraw (current bounding box.north east) circle (1pt);
\end{tikzpicture}
```
The bounding box extends for some reason to the left of the text area.
It seems to me that the bounding box changes its size whenever I add more content into it. Is that true? Explanation about it would be very welcome.
Top Answer
frougon
As far as I know, the `(0,0)` coordinate has nothing special in Ti*k*Z. Typically, you can use it to place things relatively to one another. When you draw the `n1` node, Ti*k*Z chooses to place `(0,0)` somewhere that does not follow your expectations. This can be shown by replacing your `\path` with a `\draw` statement (I added a `\smallskip` to make the horizontal rules easy to distinguish):
```
\documentclass{article}
\usepackage{tikz,lipsum}
\begin{document}
\lipsum[1]
\vskip 10pt
\hrule
\smallskip\noindent
\begin{tikzpicture}
\draw[thick, green] (0,0) -- (\textwidth,0);
\node (n1) [draw=black,outer sep=0pt,anchor=north] at (current bounding box.west) {node 1};
\node (n2) [draw=black,outer sep=0pt,anchor=east] at (current bounding box.east) {node 2};
\filldraw (n2.north east) circle (1pt);
\end{tikzpicture}
\end{document}
```

One can improve things by:
- capturing the bounding box created by drawing the rule that lives in the `tikzpicture`;
- using the `(0,0)` coordinate (or any other one) for positioning the left box relatively to the rule.
```
\documentclass{article}
\usepackage{tikz,lipsum}
\begin{document}
\lipsum[1]
\vskip 10pt
\hrule
\smallskip
\noindent
\begin{tikzpicture}
\begin{scope}[local bounding box=bb]
\draw[thick, green] (0,0) -- (\textwidth,0);
\end{scope}
\node[draw, anchor=west] at (0,0) {node 1};
\node (n2) [draw, anchor=east] at (bb.east) {node 2};
\filldraw (n2.north east) circle[radius=1pt];
\end{tikzpicture}
\end{document}
```

However, this is imperfect: there are overfull boxes due to rule widths, line caps and, obviously, the little disc. My recommendation would be to use the `tikzpagenodes` Ti*k*Z library at least for the horizontal positioning, along with `remember picture` and `overlay` options to:
* allow Ti*k*Z coordinates to be written to and read back from the .aux file;
* avoid taking up space on the page (thus, no overfull box here).
Here, I use `\tikzmark` to obtain the vertical position immediately below your `\hrule`:
```
\documentclass{article}
\usepackage{tikz,lipsum,tikzpagenodes}
\usetikzlibrary{tikzmark}
\begin{document}
\lipsum[1]
\vskip 10pt
\hrule
\vbox to 0pt {\hbox{\tikzmark{belowRule}}\vss}
\begin{tikzpicture}[remember picture,overlay]
\node[draw, anchor=north west]
at (current page text area.west |- {pic cs:belowRule}) {node 1};
\node[draw, anchor=north east]
at (current page text area.east |- {pic cs:belowRule}) {node 2};
\end{tikzpicture}
\end{document}
```

If you want some vertical shift below the rule, you can use for instance:
```
\node[draw, anchor=north west]
at ([yshift=-1ex]current page text area.west |- {pic cs:belowRule}) {node 1};
\node[draw, anchor=north east]
at ([yshift=-1ex]current page text area.east |- {pic cs:belowRule}) {node 2};
```

# Suggested reading
Regarding how Ti*k*Z places a `tikzpicture` relatively to the surrounding TeX material, I suggest you to read in the [Ti*k*Z & PGF manual](https://mirrors.ctan.org/graphics/pgf/base/doc/pgfmanual.pdf) §12.2.1, the documentation of the `/tikz/baseline` option and the preceding text starting with “At the end of the environment, PGF tries to make a good guess at the size of a bounding box (...)”).
(Things are different when the `overlay` option is used, as the `tikzpicture` is supposed to take no space at all for TeX's typesetting algorithm in this case.)
Note: running `texdoc pgfmanual` from a command prompt is likely to bring up the Ti*k*Z & PGF manual without downloading anything if your TeX distribution installed it.
Answer #2
samcarter
- If you anchor the first node `north`, half of it will "stick out" to the left side. This will make your tikz drawing wider by half the width of the node. You can see what's happening more clearly with `\node(n1) [draw=black,outer sep=0pt,anchor=north,overlay] at (current bounding box.west) {node 1};`:

- TikZ normally adds line caps to lines, this means that a line of `\textwidth` is actually a tiny bit longer than the text width.
### Solution 1
```
\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\vskip 10pt
\hrule
\noindent
\begin{tikzpicture}
\path (0,0) (\textwidth-1.4pt,0);% set bounding box
\node[draw=black,outer sep=0pt,anchor=west] (n1) at (current bounding box.west) {node 1};
\node[draw=black,outer sep=0pt,anchor=east] (n2) at (current bounding box.east) {node 2};
\end{tikzpicture}%
\end{document}
```

### Solution 2
In case you just need to position the nodes at the width of the text and not necessarily at the exact position where the text block ends, you could use the `tikzpagenodes` package:
```
\documentclass{article}
\usepackage{lipsum}
\usepackage{tikzpagenodes}
\begin{document}
\begin{tikzpicture}[overlay,remember picture]
\node[draw=black,outer sep=0pt,anchor=north west] (n1) at (current page text area.south west) {node 1};
\node[draw=black,outer sep=0pt,anchor=north east] (n2) at (current page text area.south east) {node 2};
\end{tikzpicture}%
\lipsum
\end{document}
```
