4

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"?
  • Also check out \pgfmathsetlength and \pgfmathsetlengthmacro. – John Kormylo Aug 14 at 4:07
7

\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

  • Thanks, I get it now. Still somewhat confusing that you would need to add pt if the arithmetic is done in pt, however. – user1362373 Aug 14 at 5:04
2

Very simple: divide or multiply by 1cm. That way you can work in any units you like, such as cm. (I do not recommend using \rm for a macro name, though.)

\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)/2cm} % compute mean diameter
  \typeout{\rm}
  \draw[red,line width=\dr] (0:\rm*1cm) arc (0:360:\rm*1cm);
  \draw[black] (0,0) circle (\ro) circle (\ri);
\end{tikzpicture}

\end{document}

enter image description here

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.