2

I have a problem drawing a circle and then filling a part of it using tikz.

My code (hopefully I usepackage'd all the necessary ones for MWE, the header is huge and badly managed):

\documentclass{book}

\usepackage[left=3.0cm, right=2.5cm, top=2.5cm,bottom=2.0cm,includeheadfoot]{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,calc,decorations.markings,arrows,positioning,intersections,patterns,scopes,datavisualization}
\usepackage{tikzscale}

\begin{document}

\begin{figure}
\centering
\includegraphics[width=\textwidth]{fig.tikz}
\end{figure}
\end{document}

Contents fig.tikz:

\begin{tikzpicture}[x=0.1mm,y=2.0000mm]
   \draw ($(   0.000,   8.138)+(0,3.00)$) coordinate (pnt9) circle (2mm);
   \draw (   0.000,   8.138) -- (pnt9);
   \fill[fill=black] (pnt9) -- ++(0,2mm) arc (90:0.0000:2mm) -- cycle;
   \fill[fill=black] (pnt9) -- ++(0,-2mm) arc (270:173.6690:2mm) -- cycle;
\end{tikzpicture}

What I get is the following:

What I get from that, is that the circle's centre is not where all the other elements have it. Most of the code is generated by another program.

I tried to search for a similar question, but to no avail.

  • 3
    I obtain a correct figure with your code if I change tikzfigure with tikzpicture. – Ignasi Dec 12 '16 at 11:07
  • 1
    With standalone class you don't use geometry nor figure environments and with \includegraphics you can not include a TiKZ file. I think you should test your code before posting it to be sure it works as you want. – Ignasi Dec 12 '16 at 12:01
  • 2
    @PjancMatuzl that works because of the tikzscale package. I tend to make sure my images match my document manually. Then for example I can make sure all line width are the same through out the doc. – daleif Dec 12 '16 at 12:33
  • 1
    Say you draw a vertical line if width 10pt, and scale this to 0.8, then this line is now 8pt. In a different fig you might have scaled a similar fig by 0.9, giving another line width. – daleif Dec 12 '16 at 13:19
  • 1
    daleif is pointing out that @Ignasi 's comment doesn't apply since you are loading tikzscale which uses \includegraphics{} to load TikZ pictures from external files. This is not the only way. I tend to \input{} them and load the standalone package so I can also compile the files standalone. But tikzscale is a nice-seeming package. – cfr Dec 13 '16 at 0:33
2

The original picture is minute. TikZ is not very accurate, by default. Hence, when you scale something minute to be many, many, many times its natural size, inaccuracies creep in and rounding errors inevitably degrade the output.

The solution is not to make the original picture so absurdly tiny in the first place. The x= and y= options were not doing much except in the first line of the picture since specific dimensions were specified everywhere else, so I just eliminated that and multiplied the 3 by 2 in the first line. (Originally, it would be 6mm. I then removed all the mm so that we get something more sensible in terms of natural size. Then it scales up fine.

\begin{filecontents}{\jobname-fig.tikz}
\begin{tikzpicture}
   \draw (0,6) coordinate (pnt9) circle (2);
   \draw (0,0) -- (pnt9);
   \fill[fill=black] (pnt9) -- ++(0,2) arc (90:0:2) -- cycle;
   \fill[fill=black] (pnt9) -- ++(0,-2) arc (270:173.669:2) -- cycle;
\end{tikzpicture}

\end{filecontents}
\documentclass{book}
\usepackage[left=3.0cm, right=2.5cm, top=2.5cm,bottom=2.0cm,includeheadfoot]{geometry}
\usepackage{tikz}
\usepackage{tikzscale}

\begin{document}
\begin{figure}
  \centering
  \includegraphics[width=\textwidth]{\jobname-fig}
\end{figure}
\end{document}

bit bigger

It also looks fine if you don't scale the original but just include it either with \input{} or \includegraphics{}. Of course, it is a bit on the titchy side of tiny, but it looks fine if you zoom by 1,600% or so - still a bit small, but perfect for all that.

  • Thank you for your answer. The x=..,y=.. options are there because I use some big coordinates (as already mentioned, the picture is compiled by another program). I am still accepting your answer, for your efforts :) – Pjanc Matuzl Dec 13 '16 at 11:35
  • @PjancMatuzl I only removed them really because they were not doing much in the MWE. The x= has no effect because the shift is constant and, so, essentially equivalent to zero. So the only effect was on the 3 for the y. – cfr Dec 13 '16 at 17:46
1

After playing around some more by myself, I found that separating the coordinate definition from the drawing of the circle solves the problem too.

\begin{tikzpicture}[x=0.1mm,y=2.0000mm]
  \coordinate (pnt9) at ($(   0.000,   8.138)+(0,3.00)$);
  \draw (pnt9) circle (2mm);
  \draw (   0.000,   8.138) -- (pnt9);
  \fill[fill=black] (pnt9) -- ++(0,2mm) arc (90:0.0000:2mm) -- cycle;
  \fill[fill=black] (pnt9) -- ++(0,-2mm) arc (270:173.6690:2mm) -- cycle;
\end{tikzpicture}
  • Interesting. I assume the inaccuracies affect everything uniformly in this case because there will be some difference in the procedure used to define coordinates implicitly versus explicitly. – cfr Dec 13 '16 at 17:48

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.