1

I have two problems. First I don't know why but Texmaker doesn't compile when I define this command:

\newcommand{\Axes}{\draw[-{Latex[length=5mm,width=2mm]}](-5,0)--(5,0)\node[below]{$x$};
\foreach \x in{-5,-4,-3,-2,-1,1,2,3,4}
\draw[xshift=\x cm](0pt,-2pt)--(0pt,2pt);
\draw[-{Latex[length=5mm,width=2mm]}](0,-5)--(0,5)node[left]{$y$};
\foreach \y in{-5,-4,-3,-2,-1,1,2,3,4}
\draw[yshift=\y cm](-2p,0pt)--(2pt,0pt);}

What's the problem with this? How to solve this problem? And my second question, how can I define this command with options? I want my \Axes command to look like this: \Axes[(x axis length),(y axis length)] Please help me, I rarely define commands and I don't have experience about it. Thank you.

The full code

\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz,fullpage,mathtools}
\usetikzlibrary{arrows.meta}

\newcommand{\Axes}{\draw[-{Latex[length=5mm,width=2mm]}](-5,0)--(5,0)\node[below]{$x$};
\foreach \x in{-5,-4,-3,-2,-1,1,2,3,4}
\draw[xshift=\x cm](0pt,-2pt)--(0pt,2pt);
\draw[-{Latex[length=5mm,width=2mm]}](0,-5)--(0,5)node[left]{$y$};
\foreach \y in{-5,-4,-3,-2,-1,1,2,3,4}
\draw[yshift=\y cm](-2p,0pt)--(2pt,0pt);}

\begin{document}
\tikz{\Axes}
\end{document}
  • 1
    -2p should be -2pt, most likely – user31729 Dec 17 '15 at 18:55
  • Oh yes, I just wrote quickly. – ostal123 Dec 17 '15 at 18:56
  • The \Axes[...] is unclear. Should it be an optional argument? – user31729 Dec 17 '15 at 18:59
  • Yes. You can set the length of axes. It is optional, like in the command <code> \draw[thick](0,0)--(1,0);</code>. – ostal123 Dec 17 '15 at 19:01
  • 1
    I've deleted the texmaker tag, since this is not related to the editor itself. – user31729 Dec 17 '15 at 19:14
2

The -2p should be -2pt and there is ; missing after the first \draw statement, in my point of view.

Update: I've added a new \AxesNew version with an optional argument and a splitter helper command. Caveat: You have to enclose the lengths in a {...} pair yet!

A better approach would use expl3 and its splitting features (i.e. \seq_set_split:Nnn but that's perhaps too much ado ;-))

\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz,fullpage,mathtools}
\usetikzlibrary{arrows.meta}

\newcommand{\Axes}{%
  \draw[-{Latex[length=5mm,width=2mm]}](-5,0)--(5,0);\node[below]{$x$};
  \foreach \x in{-5,-4,-3,-2,-1,1,2,3,4} 
    \draw[xshift=\x cm](0pt,-2pt)--(0pt,2pt);
    \draw[-{Latex[length=5mm,width=2mm]}](0,-5)--(0,5)node[left]{$y$};
    \foreach \y in{-5,-4,-3,-2,-1,1,2,3,4} 
      \draw[yshift=\y cm](-2pt,0pt)--(2pt,0pt);
}

    \def\ostalslittlehelper#1,#2{%
\def\ostalfirstarg{#1}%
\def\ostalsecondarg{#2}%
}

\newcommand{\AxesNew}[1][{5mm},{2mm}]{%
  \ostalslittlehelper#1%
    \draw[-{Latex[length=\ostalfirstarg,width=\ostalsecondarg]}](-5,0)--(5,0);\node[below]{$x$};
  \foreach \x in{-5,-4,-3,-2,-1,1,2,3,4} 
    \draw[xshift=\x cm](0pt,-2pt)--(0pt,2pt);
    \draw[-{Latex[length=\ostalfirstarg,width=\ostalsecondarg]}](0,-5)--(0,5)node[left]{$y$};
    \foreach \y in{-5,-4,-3,-2,-1,1,2,3,4} 
      \draw[yshift=\y cm](-2pt,0pt)--(2pt,0pt);
}

\begin{document}
\tikz{\AxesNew}

\tikz{\AxesNew[{8mm},{10mm}]}


\end{document}
  • You've answered this question magnificently! – ostal123 Dec 17 '15 at 19:17
  • 1
    @ostal123: Alright, glad I could help. Happy TeXing! – user31729 Dec 17 '15 at 19:24
  • Anyway I don't understand what these commands mean: \def\ostalslittlehelper#1,#2{% \def\ostalfirstarg{#1}% \def\ostalsecondarg{#2}% } – ostal123 Dec 17 '15 at 19:42
  • Can you explain me how these commands work and what do they mean? Or where can I find a document about them? – ostal123 Dec 17 '15 at 19:43
  • The argument is -- by your request -- length,width, so I have to split this into two separate lengths. \ostalslittlehelper#1,#2 does actually delimit the arguments by this , and stores the argument to two separate macros which are used then in the length=... etc. specifications of TikZ – user31729 Dec 17 '15 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.