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

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

enter image description here

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}

enter image description here


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

Subnode to prevent nesting