2

I am trying to efficiently draw two concentric ellipses with four equally spaced dots at the cardinal directions using TikZ. Currently, I have the following code which generates the following image.

\documentclass[tikz,border=2mm]{standalone}

\usetikzlibrary{decorations.markings}

\begin{document}
\begin{tikzpicture}
    \foreach \s in {1,0.3} {
        \draw [blue,thick,scale=\s,decoration={markings,
            mark=at position 0    with {\fill circle (2pt);},
            mark=at position 0.25 with {\fill circle (2pt);},
            mark=at position 0.5  with {\fill circle (2pt);},
            mark=at position 0.75 with {\fill circle (2pt);}
        },postaction={decorate}] ellipse (2cm and 1.4cm);
    }
\end{tikzpicture}
\end{document}

enter image description here

I figured out that a \foreach loop could be used to reduce hassle and draw both ellipses at the same time. However, I was wondering if it's possible to do something similar for the four mark arguments since the only thing that changes between them is the position value.

I have tried \foreach as follows, but it does not seem to work.

\documentclass[tikz,border=2mm]{standalone}

\usetikzlibrary{decorations.markings}

\begin{document}
\begin{tikzpicture}
    \foreach \s in {1,0.3} {
        \draw [blue,thick,scale=\s,decoration={markings,
            \foreach \x in {0,0.25,0.5,0.75} {
                mark=at position \x with {\fill circle (2pt);}
            }
        },postaction={decorate}] ellipse (2cm and 1.4cm);
    }
\end{tikzpicture}
\end{document}

Is there a way to make such a thing work?

3 Answers 3

2

Absolutely. This is what the /.list key is for. You only need to define a style that takes the position as an argument, cmark in the example below, and can apply it on the list of positions with cmark/.list={0,0.25,0.5,0.75}. It understands everything \foreach would understand, so cmark/.list={0,0.25,...,0.75} will also work (but not really buy us much in this very case).

\documentclass[tikz,border=2mm]{standalone}

\usetikzlibrary{decorations.markings}

\begin{document}
\begin{tikzpicture}[cmark/.style={decoration={markings,
              mark=at position #1 with {\fill circle (2pt);}
            },postaction={decorate}}]
    \foreach \s in {1,0.3} {
      \draw [blue,thick,scale=\s,cmark/.list={0,0.25,0.5,0.75}] ellipse (2cm and 1.4cm);
    }
\end{tikzpicture}
\end{document}

enter image description here

0
2

No need to reinvent the wheel, the marking library allows you to loop natively with the syntax described on page 639 of the 3.1.4b manual, I quote:

/pgf/decoration/mark=betweenpositions < start pos > and < end pos > step with < code > (no default)

\documentclass[tikz,border=2mm]{standalone}

\usetikzlibrary{decorations.markings}

\begin{document}
\begin{tikzpicture}
    \foreach \s in {1,0.3} {
        \draw [blue,thick,scale=\s,decoration={markings,
         mark= between positions 0 and .9 step 0.25
         with {\fill circle (2pt);}
        },postaction={decorate}] 
        ellipse (2cm and 1.4cm);
    }
\end{tikzpicture}
\end{document}

screenshot

2
  • This has various limitations. For instance, it only allows one step size.
    – user194703
    Nov 15, 2019 at 23:15
  • 1
    I did end up using this solution in my particular case. However, since the other answer will likely be more useful to other people with the same problem, I decided to accept it. Thanks much, regardless!
    – Shady Puck
    Nov 16, 2019 at 17:05
0

For fun, the pstricks solution, with the pst-eucl package:

 \documentclass[svgnames, border=6pt]{standalone}

\usepackage{pst-eucl}
 \usepackage{auto-pst-pdf} %% To compile with pdflatex --enablewrite18 (MiKTeX) or pdflatex --shell-escape (TeX Live, MacTeX)

\begin{document}
\psset{linecolor=RoyalBlue, linewidth=1.2pt, plotstyle=curve}
\begin{pspicture}(-5,-3.5)(5,3.5)
\multido{\na=2.4+2.1,\nb=1.6+1.4}{2}{\pstEllipse(0,0)(\na,\nb)%
\multido{\i=0+90}{4}{\pstEllipseRotNode[PointName=none, RotAngle=\i](0,0)(\na,\nb){A\i}}%
}%
\end{pspicture}

\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.