There is a handful of problems with your code snippets:
- You need a backslash in the command name:
\newcommand{\Box}[2]...
- You need to enclose the meaning of the command with braces:
\newcommand{\Box}[2]{\draw ... ;}
- LaTeX does not let you create a command called
\box because there is already one with this name (and it's a really bad idea to redefine it).
- Each argument to a command should be enclosed with braces, not comma-separated (if you want a comma-separated syntax, you can take a look at this thread).
The syntax to \newcommand is:
\newcommand{\<command>}[<nargs>][<default#1>]{<definition>}
Also, as Torbjørn T. noted, you don't need to define \pic because TikZ already has a macro \tikz.
That said, I'll call the commands \mybox and \myBox, then your definitions become:
\documentclass{article}
\usepackage{tikz}
\newcommand{\mybox}[1]{\draw (-2,4) -- ( #1 ,4) -- ( #1 ,1) -- (-2,1) -- (-2,4) ;}
\newcommand{\myBox}[2]{\draw ( #2 ,4) -- ( #1 ,4) -- ( #1 ,1) -- ( #2 ,1) -- ( #2 ,4) ;}
\begin{document}
\tikz\mybox{0};
\tikz\myBox{0}{-2};
\end{document}
A much more TikZ-y approach would be, as Kpym suggested, to use pic, which allows us to provide options to the drawing command as well as useput two boxes in a single command (which you cannot do easily with the approach above). You can also use the rectancle path command to make things more clear:
\documentclass{article}
\usepackage{tikz}
\tikzset{%
pics/mybox/.style 2 args={%
code={\draw (#1,4) rectangle (#2,1);}%
}%
}
\begin{document}
\tikz\draw (0,0) pic{mybox={0}{-2}};
\tikz\draw (0,0) pic{mybox={0}{-2}} (2,1) pic[red]{mybox={0}{-3}};
\end{document}
another improvement would be to make all the coordinates as optional arguments with some default value, but I'll leave this for another day :)
\newcommand{\Box}[2].... I'm surprised the first one worked .-. Also, you need to enclose the whole\draw ... ;in braces:{\draw ... ;}– Phelype Oleinik Jun 21 '18 at 22:17\boxnotbox). If you fix that you get an error because\boxis already defined. If you fix that (choose a different name) you get an error because you haven't wrapped the contents of the macro in{}. – Torbjørn T. Jun 21 '18 at 22:25tikzpictureenvironment,\tikz, you can use that instead of your\pic. – Torbjørn T. Jun 21 '18 at 22:27