6

I'm drawing a line between 2 nodes and I'd like to avoid to touch the nodes, I have been able to do it with the first node but not with the second.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}


\tikzstyle{every picture}+=[remember picture]



\tikz[baseline] \node[fill opacity=0.1,circle,label=left:2016,fill=black,inner sep=3pt] at (0,0) (n1) {};
\tikz[baseline] \node[draw=none,fill=none,align=left] {
  \vbox{
    bar
  }
};
\tikz[baseline] \node[draw=none,align=left] {
  \hspace*{20mm}\parbox{10cm}{
    foo
  }
};

\tikz[baseline] \node[fill opacity=0.1,circle,label=left:2016,fill=black,inner sep=3pt] at (0,0) (n2) {};
\tikz[baseline] \node[draw=none,fill=none,align=left] {
  \vbox{
    bar
  }
};
\tikz[baseline] \node[draw=none,align=left] {
  \hspace*{20mm}\parbox{10cm}{
    foo
  }
};
\begin{tikzpicture}[overlay]
  \draw (n1)++(0, -0.25) -- (n2)++(0, 0.25);
\end{tikzpicture}
\end{document}

https://www.overleaf.com/project/5c00283f4365993602702d4f

Rod
  • 383
  • Try \draw ([yshift=-0.1cm]n1.south) -- ([yshift=0.1cm]n2.north);. (And replace \tikzstyle{every picture}+=[remember picture] by \tikzset{every picture/.append style={remember picture}}) Not sure if I would draw this picture in the way you do, why don't you just draw one picture? –  Nov 29 '18 at 18:25
  • It works nicely, thanks again @marmot. I also replaced the section you mentioned. I'm not using one picture because I'm mixing text and a few nodes, if I used \begin{tikzpicture} It would not allow me to use some commands. Probably because I do not know Latex – Rod Nov 29 '18 at 18:35

3 Answers3

6

In your setting, you'd only need to use \draw ([yshift=-0.1cm]n1.south) -- ([yshift=0.1cm]n2.north);. However, one can draw your picture in a single picture without remember picture.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}


\begin{tikzpicture}
\node[fill opacity=0.1,circle,label=left:2016,fill=black,inner
sep=3pt,label={[xshift=5mm]right:bar},label={[xshift=2mm,yshift=-5mm]right:foo},] (N1){};
\node[below=1cm of N1,fill opacity=0.1,circle,label=left:2016,fill=black,inner
sep=3pt,label={[xshift=5mm]right:bar},label={[xshift=2mm,yshift=-5mm]right:foo}] (N2) {};
\draw ([yshift=-0.1cm]N1.south) -- ([yshift=0.1cm]N2.north);
\end{tikzpicture}
\end{document}

enter image description here

ADDENDUM: Using Zarko's way to define a universal style, one could use outer sep to produce the gaps. I would rather not use shorten as this can have unwanted side-effects if you consider using curved paths.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[pfft/.style={circle,fill opacity=0.1,fill=black,inner
sep=3pt,outer sep=1mm}]
\node[pfft,label=left:2016,,label={[xshift=5mm]right:bar},label={[xshift=2mm,yshift=-5mm]right:foo},] (N1){};
\node[below=1cm of N1,pfft,label=left:2016,label={[xshift=5mm]right:bar},label={[xshift=2mm,yshift=-5mm]right:foo}] (N2) {};
\draw (N1) -- (N2);
\end{tikzpicture}
\end{document}
  • When did shorten get deprecated? It is still used in the examples in the 3.0.1a manual. – Peter Grill Nov 29 '18 at 19:52
  • @PeterGrill I do not think it got officially deprecated. However, it can cause issues on curved paths, see e.g. here. So I do not see that it is better than the yshift solution, rather it can backfire. –  Nov 29 '18 at 19:56
4

And a third option, without yshift and without outer sep but shorten option in line between nodes:

\documentclass[tikz, margin=3.141592mm]{standalone}
\usetikzlibrary{positioning}

\begin{document}
    \begin{tikzpicture}[
node distance = 12mm,
   dot/.style = {circle, fill=black, fill opacity=0.1,
                 inner sep=3pt, %No outer sep like in Zarko's answer
                 node contents={}}
                        ]
\node (n1) [dot, label=left:2016, label={[xshift=3mm]right:bar}, label=below right:foo];
\node (n2) [dot, label=left:2017, label={[xshift=3mm]right:bar}, label=below right:foo,
            below=of n1];
\draw[shorten >=2pt, shorten <=2pt] (n1) -- (n2); %No `yshift` like in Marmot's answer
    \end{tikzpicture}
\end{document}

enter image description here

Ignasi
  • 136,588
3

as complement to nice @marmot answer:

\documentclass[tikz, margin=3.141592mm]{standalone}
\usetikzlibrary{positioning}

\begin{document}
    \begin{tikzpicture}[
node distance = 12mm,
   dot/.style = {circle, fill=black, fill opacity=0.1,
                 inner sep=3pt, outer sep=2pt,
                 node contents={}}
                        ]
\node (n1) [dot, label=left:2016, label={[xshift=3mm]right:bar}, label=below right:foo];
\node (n2) [dot, label=left:2017, label={[xshift=3mm]right:bar}, label=below right:foo,
            below=of n1];
\draw (n1) -- (n2);
    \end{tikzpicture}
\end{document}

enter image description here

Zarko
  • 296,517