# TikZ inside TikZ-node with given text width does wrong positioning

This code

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[text width = 3cm]{
a a a a a a a a a a a a a a a a a a a a\\
\tikz{\draw (0,0) node {A} -- (1,0) node {B};}\hspace*{10cm}\\
a a a a a a a a a a a a a a a a a a a a
};
\end{tikzpicture}
\end{document}


gives this result:

It places the line between A and B on the right of B and not between A and B. Somehow specifying text width in the outer node confuses TikZ when it places the inner line. How can I make it act normally such that the line is between A and B where it should be?

Apparently the inner tikz inherits the text width setting. It is visible if you draw the nodes:

The line goes from the center of node A to the center of node B, but the nodes are so wide that the text of node B ends up being left of the line.

If you reset the text width option, you recover the expected behavior.

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[text width = 3cm]{
a a a a a a a a a a a a a a a a a a a a\\
\tikz[text width={}]{\draw (0,0) node {A}
-- (1,0) node {B};}
\hspace*{10cm}
\\
a a a a a a a a a a a a a a a a a a a a
};
\end{tikzpicture}
\end{document}


As Torbjørn says in the comment, nesting tikzpictures doesn't work well. Here's an alternative using the \subnode command from the tikzmark package (which was designed precisely for this sort of thing).

\documentclass{article}
%\url{https://tex.stackexchange.com/q/387792/86}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
\begin{tikzpicture}
\node[text width = 3cm]{
a a a a a a a a a a a a a a a a a a a a\\
\tikz{\draw (0,0) node {A} -- (1,0) node {B};}\hspace*{10cm}\\
a a a a a a a a a a a a a a a a a a a a
};
\end{tikzpicture}

\begin{tikzpicture}[remember picture]
\node[text width = 3cm]{
a a a a a a a a a a a a a a a a a a a a\\
\mbox{\subnode{a}{A}\hspace{1cm}\subnode{b}{B}} \\
a a a a a a a a a a a a a a a a a a a a
};
\draw (a) -- (b);
\end{tikzpicture}
\end{document}


(The \mbox is needed because otherwise the line gets broken after the A; I'm not sure why that is so.)