General ⇒ repeat a drawing
repeat a drawing
Hello
I want to repeat a drawing in certain distances horizontally to form a group;
then move the whole group in certain distances vertically
such as that on page 1 of the included ions.tex
is there a more simple way, as for example using (for each), to make it look like that on pages 5-9 of the included ions.tex
How will I view your answer
Thank you
I want to repeat a drawing in certain distances horizontally to form a group;
then move the whole group in certain distances vertically
such as that on page 1 of the included ions.tex
is there a more simple way, as for example using (for each), to make it look like that on pages 5-9 of the included ions.tex
How will I view your answer
Thank you
- Attachments
-
- Ions.zip
- (42.39 KiB) Downloaded 420 times
NEW: TikZ book now 40% off at Amazon.com for a short time.
- Stefan Kottwitz
- Site Admin
- Posts: 10319
- Joined: Mon Mar 10, 2008 9:44 pm
repeat a drawing
Hello Hany,
welcome to the forum!
There are several possibilities. I can show some, perhaps I start with just one, maybe Johannes might add another or I do.
Here, I printed nodes in a loop. I simply placed TikZ pictures in the node - no problem. So I could keep the absolute coordinates here.
Stefan
welcome to the forum!
There are several possibilities. I can show some, perhaps I start with just one, maybe Johannes might add another or I do.
- classic macros storing a series of TikZ commands
-
\savebox
for a TikZ picture, then\usebox
to print this box as copies several times - a TikZ
\foreach
loop - TikZ
pic
objects
\foreach
loop solution:Code: Select all
\begin{tikzpicture}
\foreach \pos in {0,...,5} {
\node at (1.85*\pos,0) { \tikz\draw [thick,orange,fill=yellow]
(.25,.25) -- (.25,0) -- (.5,0) -- (.5,.25) -- (.75,.25) --
(.75,.5) -- (.5,.5) -- (.5,.75) -- (.25,.75) -- (.25,.5) --
(0,.5) -- (0,.25) -- (.25,.25) ;} ;}
\end{tikzpicture}
Stefan
LaTeX.org admin
- Stefan Kottwitz
- Site Admin
- Posts: 10319
- Joined: Mon Mar 10, 2008 9:44 pm
repeat a drawing
Now I wrote a pic solution:
Stefan
- globally define a pic style with a name, which you can use any time you want (well you could do it locally as well)
Code: Select all
\tikzset{ cross/.pic = { \draw [thick,orange,fill=yellow] (.25,.25) -- (.25,0) -- (.5,0) -- (.5,.25) -- (.75,.25) -- (.75,.5) -- (.5,.5) -- (.5,.75) -- (.25,.75) -- (.25,.5) -- (0,.5) -- (0,.25) -- (.25,.25) ;} }
- apply it in any TikZ drawing with a loop command or other commands, such as:
Code: Select all
\tikz \pic foreach \pos in {0,...,5} at (1.85*\pos,0) {cross};
Code: Select all
\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikzset{
cross/.pic = {
\draw [thick,orange,fill=yellow]
(.25,.25) -- (.25,0) -- (.5,0) -- (.5,.25) -- (.75,.25) --
(.75,.5) -- (.5,.5) -- (.5,.75) -- (.25,.75) -- (.25,.5) --
(0,.5) -- (0,.25) -- (.25,.25) ;}
}
\tikz \pic foreach \pos in {0,...,5} at (1.85*\pos,0) {cross};
\end{document}
LaTeX.org admin
- Stefan Kottwitz
- Site Admin
- Posts: 10319
- Joined: Mon Mar 10, 2008 9:44 pm
repeat a drawing
Here is an example showing how to place also the text node. It could be part of the
I used the
Stefan
pic
, or separate. I made it separate here.Code: Select all
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\tikzset{
cross/.pic = {
\draw [thick,orange,fill=yellow]
(.25,.25) -- (.25,0) -- (.5,0) -- (.5,.25) -- (.75,.25) --
(.75,.5) -- (.5,.5) -- (.5,.75) -- (.25,.75) -- (.25,.5) --
(0,.5) -- (0,.25) -- (.25,.25) ;},
formula/.style = {
font = \LARGE\bfseries\sffamily,
color = red,
}
}
\begin{tikzpicture}
\foreach \pos in {0,...,5} {
\pic at (1.85*\pos,0) {cross};
\node[formula] at ($(1.85*\pos,0)+(0.4,0.4)$) {Na};
}
\end{tikzpicture}
\end{document}
calc
library syntax to add an offset (0.4,0.4)
to the node position, so you need to load calc
as above. I always use styles for formatting in nodes.Stefan
LaTeX.org admin
Re: repeat a drawing
Thank you Stefan very much.
I am very sorry for the delay in my reply, I had some personal problems that prevented me from working on my drawing.
I am very sorry for the delay in my reply, I had some personal problems that prevented me from working on my drawing.
- Stefan Kottwitz
- Site Admin
- Posts: 10319
- Joined: Mon Mar 10, 2008 9:44 pm
Re: repeat a drawing
Hi Hany,
nice to read from you again! Great that you can continue your work on the document.
Stefan
nice to read from you again! Great that you can continue your work on the document.
Stefan