5

Edit: I am interested in how this problem could or could not be solved using pgf decorations, not in how to draw this specific shape.


I am trying to use pgf line decorations to draw dotted lines, where the dots are squares, as in:

Correct line behaviour

However pgf automatically rotates the environment in which the squares are drawn as in the following code:

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{calc,decorations}

\pgfdeclaredecoration{Squares}{initial}{
  \state{initial}[width=10pt]{
    \fill (0cm,0cm) rectangle (5pt,5pt);
  }
  \state{final}{
    \pgfpathlineto{\pgfpointdecoratedpathlast}
  }
}

\begin{document}
\begin{center}
  \begin{tikzpicture}
    \draw[decorate,decoration=Squares] (0,0) -- (5,5);
  \end{tikzpicture}
\end{center}
\end{document}

which outputs this:

enter image description here

Is there a way to either compensate for the automatic rotation, or alternately disable it?

4

Rotate your \fill decoration command by the opposite of the rotation upon entering the decoration code (given by \pgfdecoratedangle):

\documentclass[tikz]{standalone}

\usetikzlibrary{calc,decorations}

\pgfdeclaredecoration{Squares}{initial}{
  \state{initial}[width=10pt]{
    \fill[rotate=-\pgfdecoratedangle] (0cm,0cm) rectangle (5pt,5pt);
  }
  \state{final}{
    \pgfpathlineto{\pgfpointdecoratedpathlast}
  }
}

\begin{document}
  \begin{tikzpicture}
    \draw[decorate,decoration=Squares] (0,0) -- (1,5);
    \draw[decorate,decoration=Squares] (0,0) -- (5,5);
    \draw[decorate,decoration=Squares] (0,0) to[out=0,in=-90] (5,5);
    \draw[decorate,decoration=Squares] (0,0) -- (5,1);
  \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.