How to "add" units to results of \pgfmathsetmacro?

by user1362373   Last Updated August 13, 2019 20:23 PM

This question is inspired by Jake's answer to this question. The problem is illustrated by this MWE:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}

\def\ri{1.0cm} % define inner diameter
\def\ro{2.0cm} % define outer diameter

\begin{tikzpicture}
  \pgfmathsetmacro{\dr}{\ro-\ri} % compute width
  \pgfmathsetmacro{\rm}{(\ri+\ro)/2} % compute mean diameter
  \draw[red,line width=\dr] (0:\ro) arc (0:360:\ro);
  \draw[black] (0,0) circle (\ro) circle (\ri);
\end{tikzpicture}

\end{document}

This produces the following output:

enter image description here

But this is not what I want, which is that the red strip should be between the two black circles. I expected that I could achieve this by replacing the first \draw command by

\draw[red,line width=\dr] (0:\rm) arc (0:360:\rm);

but then I get a non-sensical result in which the red strip is far too large:

enter image description here

This is not surprising given the remarks in Section 94.1.2 "Considerations concerning units" in the pgf manual (v3.1.2). What does surprise me is that simply adding cm like this

\draw[red,line width=\dr] (0:\rm cm) arc (0:360:\rm cm);

does not work - in the sense that there is no change in the output. However, in Jake's answer, he adds pt which seems to work (as witnessed by his comment Somehow, the [...] units got lost, so we add 'pt' at the end. Not nice...).

So my questions are:

  1. Why does adding pt work in Jake's answer but not when I add cm?
  2. I cannot make sense of Section 94.1.2. There seem to be commands for checking whether units are "declared" but there do not seem to be any commands for "adding them back" to a result. What good is it to be able to check for units but not "adding them back"?
Tags : tikz-pgf pgfmath


Answers 1


\pgfmathsetmacro calculates a length and gives back the result in pt but without the unit pt attached - the macro contains simply a number. So if you want to use this number in a sensible way you should reattach the pt:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}

\def\ri{1.0cm} % define inner diameter
\def\ro{2.0cm} % define outer diameter

\begin{tikzpicture}
  \pgfmathsetmacro{\dr}{\ro-\ri} % compute width
  \pgfmathsetmacro{\rm}{(\ri+\ro)/2} % compute mean diameter
  \show\rm %<--- shows > \rm=macro: ->42.67912.
  \draw[red,line width=\dr] (0:\rm pt) arc (0:360:\rm pt);
  \draw[black] (0,0) circle (\ro) circle (\ri);
\end{tikzpicture}

\end{document}

Instead of \pdfmathsetmacro you can use \pgfmathsetlengthmacro

\documentclass{standalone}
\usepackage{tikz}
\begin{document}

\def\ri{1.0cm} % define inner diameter
\def\ro{2.0cm} % define outer diameter

\begin{tikzpicture}
  \pgfmathsetlengthmacro{\dr}{\ro-\ri} % compute width
  \pgfmathsetlengthmacro{\rm}{(\ri+\ro)/2} % compute mean diameter
  \show\rm % > \rm=macro: ->42.67912pt.
  \draw[red,line width=\dr] (0:\rm) arc (0:360:\rm);
  \draw[black] (0,0) circle (\ro) circle (\ri);
\end{tikzpicture}

\end{document}

enter image description here

Ulrike Fischer
Ulrike Fischer
August 13, 2019 19:24 PM

Related Questions


Updated December 13, 2016 08:09 AM

Updated December 23, 2016 08:09 AM

Updated June 09, 2017 21:23 PM

Updated July 21, 2017 11:23 AM