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.

ddas
  • 1,205

3 Answers3

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

Heiko Oberdiek
  • 271,626
  • 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 07 '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 07 '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 07 '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 07 '16 at 17:14
  • Maybe you should file a bug? – Ismael Miguel Aug 07 '16 at 17:16
  • Finally, a use for Code Golf outside of Stack Overflow! – muddyfish Aug 08 '16 at 11:08
19

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

Danu
  • 968
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

  • 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 09 '16 at 08:44
  • @Stefan I think that's an artefact of the conversion to bitmap rather than fundamental to the vector graphic. – andybuckley Aug 09 '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 05:30
  • Ahh, I see: didn't go through the code in that detail. Awkward! – andybuckley Aug 10 '16 at 18:59