1

I need to use \pgfmathparse{floor(\textwidth/2)} as one of x-coordinates of a path but I get an error.

\documentclass[margin=5mm,varwidth=100mm]{standalone}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \path[draw](0,0)--(\pgfmathparse{floor(\textwidth/2)},0);
    \end{tikzpicture}
\end{document}
  • You can't use \pgfmathparse there. You are getting a bunch of errors. Put \pgfmathparse outside of the \draw command (before) then use \pgfmathresult. – Phelype Oleinik May 11 at 0:57
  • There is no need to call \pfmathparse, \documentclass[margin=5mm,varwidth=100mm]{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} \path[draw](0,0)--({floor(\textwidth/2)},0); \end{tikzpicture} \end{document} works. – user121799 May 11 at 0:58
3

One of the reasons why I really like TikZ is because of its parser. It parses the coordinates automatically. All you need to do is

\documentclass[margin=5mm,varwidth=100mm]{standalone}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \path[draw](0,0)--({floor(\textwidth/2)},0);
    \end{tikzpicture}
\end{document}

The only thing you have to watch out for are things like the brackets, which you have to put in a braces, i.e. \path[draw](0,0)--(floor(\textwidth/2),0); will give an error because TikZ does not know which brackets delimit the coordinates. Another thing you may want to consider is that TikZ will convert these units to points. So floor may or may not give you what you want. However, you can always convert things to the units you want to work in, e.g. \path[draw](0,0)--({floor((\textwidth/2)*1pt/1cm)*1cm},0);.

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.