This is not related with tikz, but with how TeX make paragraphs. Latex use the rubber length \baselineskip
to set the distance between lines (plus \parskip
if it is the beginning of a paragraph). But if you add a tikzpicture, or other box that make the line higher, then, if the lines would be too close together, closer than \lineskiplimit, then TeX instead uses \lineskip as the interline glue (see here), and this interline glue takes into account the depth of the previous line.
An example:
\documentclass[openany]{book}
\usepackage{tikz}
\setlength{\fboxsep}{-0.5pt}
\setlength{\fboxrule}{0.5pt}
%\setlength{\lineskip}{0cm}
%\setlength{\parskip}{1pt}
\newcommand{\tikzmark}[1]{\tikz[overlay, remember picture, baseline={(#1.base)}]
\node[inner sep=0pt, line width=0pt] (#1) {\phantom{Tg}};}
\begin{document}
\fbox{Test}\fbox{g} \tikzmark{A}
\fbox{%
\begin{tikzpicture}[y=\baselineskip]
\draw (0, 0) -- (1, 4);
\end{tikzpicture}} \tikzmark{B}
\fbox{Test} \tikzmark{C}
\fbox{\begin{tikzpicture}[y=\baselineskip]
\draw (0, 0) -- (1, 4);
\end{tikzpicture}}
\fbox{Test} \tikzmark{D}
\fbox{Test}\fbox{g} \tikzmark{D2}
\fbox{Test} \tikzmark{D3}
\fbox{Test}
\begin{tikzpicture}[overlay, remember picture, font=\tiny]
\draw[red] (A.south) -- node[right] {lineskip} ++(down:\lineskip);
\draw[red, transform canvas={xshift=-0.43cm}] (B.base) -- node[right] {baselineskip} ++(down:\baselineskip);
\draw[red] (C.base) -- node[right] {lineskip} ++(down:\lineskip);
\draw[red] (D.base) -- node[right] {baselineskip} ++(down:\baselineskip);
\draw[red] (D2.base) -- node[right] {baselineskip} ++(down:\baselineskip);
\draw[red] (D3.base) -- node[right] {baselineskip} ++(down:\baselineskip);
\end{tikzpicture}
\end{document}
You can try remove the depth from the previous line (see here):
\documentclass[openany]{book}
\usepackage{tikz}
\setlength{\fboxsep}{-0.5pt}
\setlength{\fboxrule}{0.5pt}
%\setlength{\lineskip}{0cm}
%\setlength{\parskip}{1pt}
\newcommand{\tikzmark}[1]{\tikz[overlay, remember picture, baseline={(#1.base)}]
\node[inner sep=0pt, line width=0pt] (#1) {\phantom{Tg}};}
\newcommand{\zerodepth}[1]{\raisebox{0pt}[\height][0pt]{#1}}
\begin{document}
\fbox{Test}\fbox{\zerodepth{g}}
\fbox{%
\begin{tikzpicture}[y=\baselineskip]
\draw (0, 0) -- (1, 4);
\end{tikzpicture}}
\fbox{Test}
\fbox{\begin{tikzpicture}[y=\baselineskip]
\draw (0, 0) -- (1, 4);
\end{tikzpicture}}
\end{document}
but this solution is not going to work with whole paragraphs.
EDIT: Another option is add a \strut
at the end of the line of paragraph avobe the tikz picture.
\phantom{g}
paragraph before the environment and then use a\vspace{-1\baselineskip}
to position over it. Feels a bit like a hack though.