3

I tried to do some calculations in TeX to help me draw a picture and I can't get over this error:

 ! Missing \endcsname inserted.
<to be read again> 
\numexpr 
l.12 \draw (0,0)--(\stepsize:\radius)
                                   ;

This is the minimal code:

 \documentclass{article} 
    \usepackage{tikz} 
    \usepackage[active,tightpage]{preview}
    \setlength\PreviewBorder{2pt}
    \begin{document} 
    \begin{preview}
    \def\radius{10}
    \def\step{5}
    \begin{tikzpicture}
    \def\stepsize{\numexpr180/\step\relax}
    \draw (0,0)--(\stepsize:\radius);
    \end{tikzpicture}
    \end{preview}
    \end{document}

Looking at random example on the internet it should work, but I'm probably overlooking something very trivial or don't understand something basic.

  • isn't (\radius:\stepsize)? – touhami Aug 2 '15 at 16:06
  • @touhami No, it's (angle:radius). – skevin93 Aug 2 '15 at 16:13
  • 2
    @fbence The problem in your code is \numexpr. TikZ is able to calculate, so you can simple write \draw(0,0) -- (180/\step:\radius);. – skevin93 Aug 2 '15 at 16:18
  • Good to know, although it might not always be convenient (or readable) to do it in each place it occurs. – fbence Aug 2 '15 at 19:09
4

I had a similar problem, and solved it with the use of \number:

\documentclass{article} 
\usepackage{tikz} 
\usepackage[active,tightpage]{preview}
\setlength\PreviewBorder{2pt}
\begin{document} 
\begin{preview}
\def\radius{10}
\def\step{5}
\begin{tikzpicture}
\def\stepsize{\number\numexpr180/\step\relax}%% <<<---
\draw (0,0)--(\stepsize:\radius);
\end{tikzpicture}
\end{preview}
\end{document}

With \def\stepsize{\numexpr180/\step\relax} TikZ was seeing the unexpanded macro, not the number itself. \number causes \numexpr to be evaluated and yields the actual number that TikZ needs.

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.