0

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 ?

enter image description here

\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}
Hibou
  • 591
  • 2
    Do not nest tikzpictures... https://tex.stackexchange.com/questions/47377/proper-nesting-of-tikzpicture-environments-reset-all-pgf-values-to-their-defaul. Better learn how to use pic: https://tex.stackexchange.com/questions/241719/name-tikz-pic-as-if-it-was-a-node – Rmano May 31 '20 at 09:44
  • 1
    BTW, just adding at the top of your code \documentclass{article} makes it a quite nice minimal working example (MWE)... – Rmano May 31 '20 at 10:02

1 Answers1

1

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}

enter image description here

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

Rmano
  • 40,848
  • 3
  • 64
  • 125
  • 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
  • 1
    ++ 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