32

I want to draw Olympic rings and used the code from here, which is,

% Olympic rings
% Author: Paul Gaborit
\documentclass[a4paper]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
 \definecolor{r1}{RGB}{0,129,188}
 \definecolor{r2}{RGB}{252,177,49}
 \definecolor{r3}{RGB}{35,34,35}
 \definecolor{r4}{RGB}{0,157,87}
 \definecolor{r5}{RGB}{238,50,78}
 \begin{scope}
   \clip (-6,2) rectangle (6,-.9);
   \foreach \col/\xp/\yp in {
     r5/4/0, r4/2/-1.8, r3/0/0,
     r2/-2/-1.8, r1/-4/0
   } {
     \path[draw=white,line width=.08cm,
     fill=\col,even odd rule]
     (\xp, \yp) circle (1.9cm)
     (\xp, \yp) circle (1.5cm);
   }
 \end{scope}
 \begin{scope}
   \clip (-6,-.9) rectangle (6,-3.8);
   \foreach \col/\xp/\yp in {
     r1/-4/0, r2/-2/-1.8, r3/0/0,
     r4/2/-1.8, r5/4/0
   } {
     \path[draw=white,line width=.08cm,
     fill=\col,even odd rule]
     (\xp, \yp) circle (1.9cm)
     (\xp, \yp) circle (1.5cm);
   }
 \end{scope}
\end{tikzpicture}
\end{document}

But I am getting a faint straight line in the middle of the PDF file. You can see the line in the lower half of the black ring properly. How to remove this?

enter image description here

I have tried the solution suggested by Paul Gaborit in Improve PSTricks code for drawing of the Olympic flag, but the problem is still there. I am running pdflatex on my code. By any chance is it creating the faint line in the middle of the image?

Edit

Here is the output I am getting by using Heiko Oberdiek's suggestion and running Latex + dvips + ps2pdf on the following code,

\nofiles % .aux file is not needed
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{hobby}

\newcommand*{\xshift}{169.5538bp}
\newcommand*{\yshift}{-71.7013bp}
\newcommand*{\radius}{71.6821bp}
\newcommand*{\whitelinewidth}{3.762bp}
\newcommand*{\colorlinewidth}{12.9041bp}

\definecolor{color@A}{cmyk}{1, .25, 0, 0}
\definecolor{color@B}{cmyk}{0, .342, .91, 0}
\definecolor{color@C}{cmyk}{0, 0, 0, 1}
\definecolor{color@D}{cmyk}{1, 0, .91, .06}
\definecolor{color@E}{cmyk}{0, .94, .65, 0}

\begin{document}
  \begin{tikzpicture}[
    use Hobby shortcut,
    every path/.style = {
      line width      = \whitelinewidth,
      double distance = \colorlinewidth,
      white,
    },
  ]
    % define center coordinates for the five rings
    \path
      (0,0) coordinate (center@A)
      ++(\xshift, 0) coordinate (center@C)
      ++(\xshift, 0) coordinate (center@E)
      ($(center@A)!.5!(center@C)$) ++(0, \yshift) coordinate (center@B)
      ++(\xshift, 0) coordinate (center@D)
    ;
    % \drawring draws the first part of the ring with blank parts
    \newcommand*{\drawring}[4]{%
      \draw[
        save Hobby path = {path@#1},
        double          = {color@#1},
        overlay,
      ]
        (center@#1)
        +([closed] 225:\radius) .. +([#2] 315:\radius) ..
        +([#3] 45:\radius) .. +([#4] 135:\radius)
      ;
    }
    % \finishring draws the blank parts of the rings
    \newcommand*{\finishring}[1]{%
      \draw[
        double                     = {color@#1},
        restore and use Hobby path = {path@#1}{
          disjoint, 
          invert soft blanks
        },
      ];
    }
    \drawring{A}{}{blank=soft}{}
    \drawring{B}{}{}{blank=soft}
    \drawring{C}{}{blank=soft}{}
    \finishring{B}
    \finishring{A}
    \drawring{E}{blank=soft}{}{}
    \drawring{D}{}{}{}
    \finishring{C}
    \finishring{E}
    % set calculated bounding box
    \useasboundingbox
      let \n{r} = {\radius + \colorlinewidth/2}
      in
        (center@A) +(-\n{r}, \n{r}) % upper left corner
        (center@B -| center@E) +(\n{r}, -\n{r}) % lower right corner
    ;
  \end{tikzpicture}
\end{document}

The output I get is,

enter image description here

Please notice that each ring contains two faint lines except the green one.

9
  • 3
    See the last answer here: Improve PSTricks code for drawing of the Olympic flag – CarLaTeX Aug 7 '16 at 7:04
  • @CarLaTeX I tried that but the faint line in the middle is still there. I am running pdflatex on my code. By any chance is it creating the line? – ddas Aug 7 '16 at 7:08
  • A short LaTeX/TikZ version: Olympic Games Logo - Free Style Edition – Heiko Oberdiek Aug 7 '16 at 7:10
  • @HeikoOberdiek Thanks. The solution is not working. I am getting two faint lines in each ring except the green one. – ddas Aug 7 '16 at 7:22
  • 1
    @HeikoOberdiek, ddas, congratulations for your sharp eyes, I had to enlarge the picture 10 times to be able to see the faint lines! Unfortunately I can't help ddas, I hope some expert will do it! – CarLaTeX Aug 7 '16 at 8:02
35

Version, which uses uninterrupted circles (without faint lines), where the gaps are created by clipping. This solution does not use any white background color. The measurements and colors are taken from my answer in Programming Puzzles & Code Golf.

\nofiles % .aux file is not needed
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand*{\xShift}{169.5538bp}
\newcommand*{\yShift}{-71.7013bp}
\newcommand*{\Radius}{71.6821bp}
\newcommand*{\WhiteLineWidth}{3.762bp}
\newcommand*{\ColorLineWidth}{12.9041bp}

\definecolor{color@A}{cmyk}{1, .25, 0, 0}    % blue
\definecolor{color@B}{cmyk}{0, .342, .91, 0} % yellow
\definecolor{color@C}{cmyk}{0, 0, 0, 1}      % black
\definecolor{color@D}{cmyk}{1, 0, .91, .06}  % green
\definecolor{color@E}{cmyk}{0, .94, .65, 0}  % red

% Radii
\pgfmathsetlengthmacro{\InnerColorRadius}{\Radius - \ColorLineWidth/2}
\pgfmathsetlengthmacro{\OuterColorRadius}{\Radius + \ColorLineWidth/2}
\pgfmathsetlengthmacro{\InnerWhiteRadius}{\InnerColorRadius - \WhiteLineWidth}
\pgfmathsetlengthmacro{\OuterWhiteRadius}{\OuterColorRadius + \WhiteLineWidth}

% Positions of circle centers
\newcommand{\xA}{0pt}
\newcommand{\yA}{0pt}
\pgfmathsetlengthmacro{\xC}{\xA + \xShift}
\let\yC\yA
\pgfmathsetlengthmacro{\xE}{\xC + \xShift}
\let\yE\yC
\pgfmathsetlengthmacro{\xB}{(\xA + \xC)/2}
\pgfmathsetlengthmacro{\yB}{\xA + \yShift}
\pgfmathsetlengthmacro{\xD}{(\xC + \xE)/2}
\let\yD\yB

% Bounding Box
\pgfmathsetlengthmacro\llxBB{\xA - \OuterColorRadius}
\pgfmathsetlengthmacro\llyBB{\yB - \OuterColorRadius}
\pgfmathsetlengthmacro\urxBB{\xE + \OuterColorRadius}
\pgfmathsetlengthmacro\uryBB{\yE + \OuterColorRadius}

% Clipping box (a little larger as bounding box)
\pgfmathsetlengthmacro\llxClip{\llxBB - 1pt}
\pgfmathsetlengthmacro\llyClip{\llyBB - 1pt}
\pgfmathsetlengthmacro\urxClip{\urxBB + 1pt}
\pgfmathsetlengthmacro\uryClip{\uryBB + 1pt}

\begin{document}
  \begin{tikzpicture}[
    line width=\ColorLineWidth,
    radius=\Radius,
  ]
    \def\ClipBoxArea{%
      (\llxClip, \llyClip) rectangle (\urxClip, \uryClip)
    }
    \def\Gap(#1,#2)(#3:#4){%
      ($(#1,#2) + (#3:\OuterWhiteRadius)$)
      arc(#3:#4:\OuterWhiteRadius) --
      ($(#1,#2) + (#4:\InnerWhiteRadius)$)
      arc(#4:#3:\InnerWhiteRadius) --
      cycle
    }
    % Upper rings
    \begin{scope}[even odd rule]
      \clip
        \ClipBoxArea
        % Gap by yellow ring in blue ring
        \Gap(\xB,\yB)(225:135)
        % Gap by yellow ring in black ring
        \Gap(\xB, \yB)(90:45)
        % Gap by green ring in black ring
        \Gap(\xD, \yD)(225:135)
        % Gap by green ring in red ring
        \Gap(\xD, \yD)(90:45)
      ;
      \draw[color@A] (\xA, \yA) circle;
      \draw[color@C] (\xC, \yC) circle;
      \draw[color@E] (\xE, \yE) circle;
    \end{scope}
    % Lower rings
    \begin{scope}[even odd rule]
      \clip
        \ClipBoxArea
        % Gap by blue ring in yellow ring
        \Gap(\xA, \yA)(45:-45)
        % Gap by black ring in yellow ring
        \Gap(\xC, \yC)(225:270)
        % Gap by black ring in green ring
        \Gap(\xC, \yC)(45:-45)
        % Gap by red ring in green ring
        \Gap(\xE, \yE)(225:270)
      ;
      \draw[color@B] (\xB, \yB) circle;
      \draw[color@D] (\xD, \yD) circle;
    \end{scope}
    \useasboundingbox (\llxBB, \llyBB) rectangle (\urxBB, \uryBB);
  \end{tikzpicture}
\end{document}

Result

6
  • 1
    When you zoom in the PNG image, you can see a ton of green around the yellow circle. Is this normal/expected? Is this present in the "original" output (PDF/rendered on screen)? – Ismael Miguel Aug 7 '16 at 13:48
  • 1
    @IsmaelMiguel It's an artifact from the conversion from PDF to PNG (maybe some anti-aliasing effect with the alpha channel). The updated answer now uses an image, generated by GIMP. The PDF file is fine, as you can also see from the TeX source. – Heiko Oberdiek Aug 7 '16 at 15:11
  • To be very honest, I know nearly nothing about TeX. It looks like beautiful Chinese to me. I just noticed the weird green around the yellow circle and though it could have slipped through the cracks and be a tiny bug on your code or some glitch on the output. The new image is "fixed" (it wasn't broken before, just the weird green) and seems fine by me. – Ismael Miguel Aug 7 '16 at 15:44
  • @IsmaelMiguel The problem seems to be caused by Ghostscript. The image with the wrong colors at the borders of the rings was created by Ghostscript 9.19 under Cygwin. On a Linux machine the colors looked right with Ghostscript 9.16. – Heiko Oberdiek Aug 7 '16 at 17:14
  • Maybe you should file a bug? – Ismael Miguel Aug 7 '16 at 17:16
17

This seems like a perfect problem to use the tikz library knots on! The current solution is improved from the original, which had some minor "clipping issues" (for more info, see here). I consider this solution quite elegant because the code is very simple, clear and short.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{knots}
\begin{document} 
\begin{tikzpicture}
\definecolor{blue1}{RGB}{0,129,188}
\definecolor{yellow1}{RGB}{252,177,49}
\definecolor{black1}{RGB}{35,34,35}
\definecolor{green1}{RGB}{0,157,87}
\definecolor{red1}{RGB}{238,50,78}
\begin{knot}[clip width=1.4,clip radius=0.3cm]
    \strand[line width=0.25cm,red1] (3,0) circle(1.25cm);
    \strand[line width=0.25cm,green1] (1.5,-1.375) circle(1.25cm);
    \strand[line width=0.25cm,black1] (0,0) circle(1.25cm);
    \strand[line width=0.25cm,yellow1] (-1.5,-1.375) circle(1.25cm);
    \strand[line width=0.25cm,blue1] (-3,0) circle(1.25cm);
    \flipcrossings{2,4,6,8}
\end{knot}
\end{tikzpicture}
\end{document}

The result is:

enter image description here

9

A PSTricks solution with the latest pstricks-add http://comedy.dante.de/~herbert/texnik/tex/generic/pstricks-add/pstricks-add.tex Available in a couple of days on CTAN: Run with xelatex:

\documentclass{article}
\usepackage{pstricks-add}
\pagestyle{empty}
\begin{document}

\begin{pspicture}(-4.5,-3)(4.5,1.5)
\psOlympicRings(0,0)
\psOlympicRings[psscale=0.2](1.5,-1.5)
\psOlympicRings[psscale=0.2](-1.5,-1.5)
\end{pspicture}

\end{document}

enter image description here

4
  • Did you notice the blue and black borders, where the yellow circle is lying on top of those circles? I think it appears at every intersection but these intersections provide the highest contrast. – Stefan Aug 9 '16 at 8:44
  • @Stefan I think that's an artefact of the conversion to bitmap rather than fundamental to the vector graphic. – andybuckley Aug 9 '16 at 19:45
  • 1
    @andybuckley Actually, it is a problem in the PDF renderer (I tested texstudio and evince). The rims are created as two thin white circles/arcs to hide the lower circle. Although the coordinates are correct, the renderer leaves a tiny optical gap, where the underlying circle is visible. – Stefan Aug 10 '16 at 5:30
  • Ahh, I see: didn't go through the code in that detail. Awkward! – andybuckley Aug 10 '16 at 18:59

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.