# Connect different newcommands made with tikz

I'm starting to manipulate \newcommand, so far everything is okay except for connecting two commands made with tikz. As shown on the picture below, there's a gap between the lines. How to fix that ?

\usepackage{tikz}
\newcommand{\motor}{
\begin{tikzpicture}
\node [circle,draw,inner sep=0.5cm] at (0,0) (a) {M};
\draw (a.north) -- (0,1.5);
\end{tikzpicture}
}

\begin{document}
\begin{tikzpicture}
\node at (0,0) (m1) {\motor};
\node at (4,0) (m2) {\motor};
\draw (m1.east) -- (m2.west);
\draw [red] (m1.north) -- (0,2) -- (4,2) -- (m2.north);
\end{tikzpicture}
\end{document}


You should never nest tikzpictures...

If you want to use macros, it is much better to put in the macro part of the path (and using relative coordinates, so that you can move it around):

\documentclass[]{article}
\usepackage{tikz}
\newcommand{\motor}[1]{
node [circle,draw,inner sep=0.5cm](#1) {M} (#1.north) -- ++(0,1.5)
coordinate (#1-top);
}

\begin{document}
\begin{tikzpicture}
\draw (0,0) \motor{m1};
\draw (4,0) \motor{m2};
\draw (m1.east) -- (m2.west);
\draw [red] (m1-top) -- ++(0,2) -| (m2-top);
\end{tikzpicture}
\end{document}


Otherwise, you can learn how to use pics: Name tikz pic as if it was a node

• It's more clear for me now. A question, (may be a disappointing one) , what the commands ++ and -| mean ? – Hibou May 31 '20 at 10:49
• ++ is to use a relative coordinate, and -| is draw a line first horizontally then vertically. You should really read (and do) at least the first tutorial in the TikZ manual... – Rmano May 31 '20 at 10:50
• Roger that, thank you! – Hibou May 31 '20 at 10:51