2

I want to draw a Z-Curve in LaTeX using TikZ and the Lindenmayer system. But I did not manage to position the curves central over each bullet point. Furthermore I do not find any elegant way such that all curves have the same size.

Here is what I get:

enter image description here

Here is my code:

\documentclass[a4paper,11pt]{article}

\usepackage{graphicx}          
\usepackage{caption}
\usepackage{subcaption} 
\usepackage{tikz}


\begin{document}
\usetikzlibrary{lindenmayersystems}
\pgfdeclarelindenmayersystem{z-curve}{
\symbol{I}{\pgftransformscale{+0.25\pgflsystemstep}}
\symbol{p}{%
  \pgfpathmoveto{\pgfqpoint{1pt}{3pt}}\pgfpathlineto{\pgfqpoint{3pt}{3pt}}%
  \pgfpathlineto{\pgfqpoint{1pt}{1pt}}\pgfpathlineto{\pgfqpoint{3pt}{1pt}}%
}
\symbol{q}{%
  \pgfpathlineto{\pgfqpoint{1pt}{3pt}}\pgfpathlineto{\pgfqpoint{3pt}{3pt}}%
  \pgfpathlineto{\pgfqpoint{1pt}{1pt}}\pgfpathlineto{\pgfqpoint{3pt}{1pt}}%
}
\symbol{S}{\pgftransformscale{+0.5pt}}
\symbol{A}{\pgftransformshift{\pgfqpoint{0pt}{4pt}}}
\symbol{B}{\pgftransformshift{\pgfqpoint{4pt}{4pt}}}
\symbol{C}{\pgftransformshift{\pgfqpoint{4pt}{0pt}}}
\symbol{[}{\bgroup}
\symbol{]}{\egroup}
\rule{Z -> Ip}
\rule{p -> S[Ap][Bq][q][Cq]}
\rule{q -> S[Aq][Bq][q][Cq]}
}

\begin{figure}[ht]
\centering
\subcaptionbox{}
{\begin{tikzpicture}[scale=2]
\draw [l-system={z-curve, step=1cm, axiom=Z, order=1}] l-system;
\end{tikzpicture}}
\hspace{1cm}
\subcaptionbox{}
{\begin{tikzpicture}[scale=2]
\draw [l-system={z-curve, step=1cm, axiom=Z, order=2}] l-system;
\end{tikzpicture}}
\hspace{1cm}
\subcaptionbox{}
{\begin{tikzpicture}[scale=2]
\draw [l-system={z-curve, step=1cm, axiom=Z, order=3}] l-system;
\end{tikzpicture}}
\hspace{1cm}
\subcaptionbox{}
{\begin{tikzpicture}[scale=2]
\draw [l-system={z-curve, step=1cm, axiom=Z, order=4}] l-system;
\end{tikzpicture}}
\end{figure}

\end{document}

Any suggestion what I should change to get what I want?

1 Answer 1

4

1. Manage to position the curves central over each bullet point:

I changed the symbols p and q (horizontal domain: 0pt to 2pt; instead of 1pt to 3pt) to achive a horizontal centering. Vertically it also start from 0pt instead of 1pt. With \draw(<x>,<y>)... you could place the curves later arbitrary. For example with \draw(0,0).

\symbol{p}{%
  \pgfpathmoveto{\pgfqpoint{0pt}{2pt}}\pgfpathlineto{\pgfqpoint{2pt}{2pt}}%
  \pgfpathlineto{\pgfqpoint{0pt}{0pt}}\pgfpathlineto{\pgfqpoint{2pt}{0pt}}%
}
\symbol{q}{%
  \pgfpathlineto{\pgfqpoint{0pt}{2pt}}\pgfpathlineto{\pgfqpoint{2pt}{2pt}}%
  \pgfpathlineto{\pgfqpoint{0pt}{0pt}}\pgfpathlineto{\pgfqpoint{2pt}{0pt}}%
}

2. Same size of all curves:

With \resizebox{<horizontal size>}{<vertical size>}{...} (see: 4.3.2 Scaling to a requested size, p.8 in the manual ["Packages in the ‘graphics’ bundle"]) you could set the horizontal and vertical size of the tikzpicture. If you want the tikzpicture to be scaled proportionally, you can give one of the sizes and put ! in the other.

\resizebox{!}{2cm}{
\begin{tikzpicture}
\draw[black](0,0) l-system [l-system={z-curve, step=1cm, axiom=Z, order=1}];
\end{tikzpicture}
}

3. Note

I used \subfloat and reduced the import of packages, because TikZ loads graphicx automatically.


Result:

enter image description here


MWE:

\documentclass[a4paper,11pt]{article}
\usepackage{tikz}
\usepackage{subfig}
\begin{document}
\usetikzlibrary{lindenmayersystems}
\pgfdeclarelindenmayersystem{z-curve}{
\symbol{I}{\pgftransformscale{+0.25\pgflsystemstep}}
\symbol{p}{%
  \pgfpathmoveto{\pgfqpoint{0pt}{2pt}}\pgfpathlineto{\pgfqpoint{2pt}{2pt}}%
  \pgfpathlineto{\pgfqpoint{0pt}{0pt}}\pgfpathlineto{\pgfqpoint{2pt}{0pt}}%
}
\symbol{q}{%
  \pgfpathlineto{\pgfqpoint{0pt}{2pt}}\pgfpathlineto{\pgfqpoint{2pt}{2pt}}%
  \pgfpathlineto{\pgfqpoint{0pt}{0pt}}\pgfpathlineto{\pgfqpoint{2pt}{0pt}}%
}
\symbol{S}{\pgftransformscale{+0.5pt}}
\symbol{A}{\pgftransformshift{\pgfqpoint{0pt}{4pt}}}
\symbol{B}{\pgftransformshift{\pgfqpoint{4pt}{4pt}}}
\symbol{C}{\pgftransformshift{\pgfqpoint{4pt}{0pt}}}
\symbol{[}{\bgroup}
\symbol{]}{\egroup}
\rule{Z -> Ip}
\rule{p -> S[Ap][Bq][q][Cq]}
\rule{q -> S[Aq][Bq][q][Cq]}
}

\begin{figure}[h!]
\centering
\subfloat[]{\label{fig:a}
\resizebox{!}{2cm}{
\begin{tikzpicture}
\draw[black](0,0) l-system [l-system={z-curve, step=1cm, axiom=Z, order=1}];
\end{tikzpicture}
}
}
\hfill
\subfloat[]{\label{fig:a}
\resizebox{!}{2cm}{
\begin{tikzpicture}
\draw [red](0,0) l-system  [l-system={z-curve, step=1cm, axiom=Z, order=2}];
\end{tikzpicture}
}
}
\hfill
\subfloat[]{\label{fig:a}
\resizebox{!}{2cm}{
\begin{tikzpicture}
\draw [blue](0,0) l-system  [l-system={z-curve, step=1cm, axiom=Z, order=3}];
\end{tikzpicture}
}
}
\hfill
\subfloat[]{\label{fig:a}
\resizebox{!}{2cm}{
\begin{tikzpicture}
\draw [green](0,0) l-system  [l-system={z-curve, step=1cm, axiom=Z, order=4}];
\end{tikzpicture}
}
}
\hfill
\caption{Z-Curve using TikZ and the Lindenmayer system}\label{fig:LabelFigOne}
\end{figure}
\end{document}
0

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.